Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dingyi222666/groovyinandroid
Run Groovy In Android
https://github.com/dingyi222666/groovyinandroid
Last synced: 11 days ago
JSON representation
Run Groovy In Android
- Host: GitHub
- URL: https://github.com/dingyi222666/groovyinandroid
- Owner: dingyi222666
- License: apache-2.0
- Created: 2022-05-20T19:25:10.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-10-17T10:12:50.000Z (about 2 years ago)
- Last Synced: 2024-10-11T09:46:45.049Z (27 days ago)
- Language: Kotlin
- Size: 3.9 MB
- Stars: 12
- Watchers: 2
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# GroovyInAndroid
Run Groovy in Android, compile groovy code to dex using D8 and run it in Android.
## Quick Start
```groovy
implementation "io.github.dingyi222666:groovy-android:1.0.7-beta4"
```### ~~Using ScriptFactory~~
```kotlin
val scriptFactory = GroovyScriptFactory()
scriptFactory
.evaluate(
"""
import android.widget.Toast
import xx.xx.YouApplication
Toast.makeText(YouApplication.instance, "Hello World for groovy", Toast.LENGTH_SHORT).show()
""".trimIndent()
)
```### Using DynamicGrooidClassLoader
```kotlin
val classLoader = DynamicGrooidClassLoader(this.classLoader)
val scriptClass =
kotlin.runCatching { classLoader.parseClass(groovyCode) as Class }
.onFailure {
//do something in parseClass failure
it.printStackTrace()
}
.getOrNull() ?: error("parseClass failure")scriptClass.newInstance().run()
```or use java
```java
DynamicGrooidClassLoader classLoader = new DynamicGrooidClassLoader(this.getClass().getClassLoader(), null, false);try {
//your groovyCode here
String groovyCode = "";Class<Script> scriptClass = classLoader.parseClass(groovyCode);
scriptClass.newInstance().run();
} catch (Exception e) {
//do something in parseClass failure
}```
### TODO
- [x] Support caching dex
- [x] always compile the dex into memory, we don't need to be concerned about repeatedly compiling the dex causing much cache dex