This is a simple library to run javascript, Maybe it will help you to run javascript more easily without using big library.
Using gradle
- Add the JitPack repository to your build file. Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
2.Add the dependency
dependencies {
implementation 'com.github.adi-itgg:AITJsEval:v1.2'
}
For less information, see implementation for maven/sbt/leiningen on jitpack.io Documentation
Initialize instance AITJsEval first. Context must using Application Context!
class App : Application() {
override fun onCreate() {
super.onCreate()
// Initialize library first
AITJsEval.initialize(applicationContext)
}
}
val sc = """
(function f() {
var d = "EvalExample:10"
var i = parseInt(d.split(":")[1]) + 2 * 88
return d.split(":")[0] + " " + i
}());
"""
AITJsEval.get().enqueue("sample", sc, object : OnJavaScriptResponseListener {
override fun onResponse(script: Script) {
rText.value = script.result
Toast.makeText(context, "Script executed", Toast.LENGTH_SHORT).show()
}
})
Result is EvalExample 186
If you are using Proguard minifyEnabled true
, Add this rules to your proguard-rules.pro file.
-keep public class me.phantomx.aitjseval.** { *; }
-keepattributes JavascriptInterface
-keepclassmembers class * {
@android.webkit.JavascriptInterface <methods>;
}