I have and Android module which contains a Gradle JavaExec task. When running the JavaExec task, I would like it to use the classpath of the module.
The JavaExec task executes a Kotlin main function which uses some 3rd party library (kotlinpoet). But when running the Gradle task, I'm getting a java.lang.ClassNotFoundException due to kotlinpoet library not being included in the classpath.
I've found similar issues in StackOverflow, and tried many variants for the classpath parameter in the myTask, but nothing that worked.
Here's is the build.gradle file:
plugins {
id 'com.android.library'
id 'kotlin-android'
}
apply plugin: 'kotlinx-serialization'
android {
compileSdkVersion 30
defaultConfig {
...
}
buildTypes {
...
}
compileOptions {
...
}
kotlinOptions {
jvmTarget = '1.8'
}
}
task myTask(type: JavaExec) {
classpath += (files('build/tmp/kotlin-classes/debug', "${android.sdkDirectory}/tools/lib/kotlin-stdlib-1.1.3-2.jar", getBuildDir().toString() + "/intermediates/classes/debug"))
main = 'com.foo.app.home.parser.MainKt'
}
tasks.named('build') { dependsOn('configGeneratorTask') }
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation "androidx.core:core-ktx:$androidx_core_ktx"
implementation "org.jetbrains.kotlinx:kotlinx-serialization-core:$kotlinx_serialization_version"
implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:$kotlinx_serialization_version"
implementation 'com.squareup:kotlinpoet:1.10.2'
}
You can get the classpath from each variant. This should do the trick:
Also see this answer here: https://stackoverflow.com/a/37268008/3968618