Reputation: 31
I want to run kotlin script(in string form) within Android:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val engine = KotlinJsr223DefaultScriptEngineFactory().getScriptEngine()
engine.eval("val x =3")
val result = engine.eval("x+2")
Log.d("test", "onCreate: result=$result")
}
}
the dependency i added:
implementation "org.jetbrains.kotlin:kotlin-scripting-jsr223:1.4.0"
implementation "org.jetbrains.kotlin:kotlin-script-runtime:1.4.0"
implementation "org.jetbrains.kotlin:kotlin-compiler-embeddable:1.4.0"
But i cant compile the code,the error is "Duplicate class" :
Duplicate class org.jetbrains.kotlin.daemon.common.WallTotalProfiler found in modules jetified-kotlin-daemon-client-1.4.0 (org.jetbrains.kotlin:kotlin-daemon-client:1.4.0) and jetified-kotlin-daemon-embeddable-1.4.0 (org.jetbrains.kotlin:kotlin-daemon-embeddable:1.4.0)
If i remove the dependency of kotlin-compiler-embeddable, Duplicate class error is gone.But a new error occur:
Cannot access 'javax.script.ScriptEngineFactory' which is a supertype of 'kotlin.script.experimental.jsr223.KotlinJsr223DefaultScriptEngineFactory'. Check your module classpath for missing or conflicting dependencies
How can i make it?
Upvotes: 1
Views: 325
Reputation: 1290
As the error is saying, you have duplicated classes in your modules. It means a class is included in two or more of your added dependencies. You should check the dependencies of these modules and use exclude
to exclude duplicated dependencies from them so that only one of your dependencies contains that duplicated dependency.
You can read more about Gradle's exclude here.
Also, you can use Gradle's dependencies command to generate a tree of all of your project's dependencies.
Note that there are other ways to resolve this problem, Just search for 'Removing duplicated classes in gradle modules' or something like this :)
Upvotes: 0