I defined my own flavor and set sourceSet:
sourceSets {
main.java.srcDirs += 'src/main/kotlin' // WORKS
myflavor.java.srcDirs += 'src/myflavor/kotlin' // DOESN'T WORK
}
here is my project structure:
but... I receive following error:
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{xyz/xyz.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "xyz.MainActivity" on path: DexPathList[[zip file "/data/app/xyz/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.ClassNotFoundException: Didn't find class "xyz.MainActivity" on path: DexPathList[[zip file "/data/app/xyz/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at android.app.Instrumentation.newActivity(Instrumentation.java:1065)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2199)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Suppressed: java.lang.ClassNotFoundException: xyz.MainActivity
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
... 13 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
however, using main sourceSet works... I will be grateful for your help.
When I converted the source to kotlin I got the error also. My fix was changing the sourceSets in gradle.
Before:
Now:
This is a bug in kotlin-gradle plugin for Android and it will be fixed in 0.9.488 or higher. Thank you for report!
In my case, I forgot
apply plugin: 'kotlin-android'
in the gradle file. Although my project was built successfully, it throwjava.lang.ClassNotFoundException
at runtime. I'm using Kotlin 1.1.1I had the same issue. In my case I was building a library and a test app to access the library.
When I converted the project to kotlin it added the relevant kotlin dependencies to the gradle file for the library but not for the main app.
Ensure that the following lines are in all of your relevant gradle files:
Try adding both to main.java.srcDirs because you want them both to compile yes?
I can confirm that at least with the Kotlin plugin version 1.1.1 and 1.1.2 the bug is fixed and you can use kotlin folders (recommended instead of java folders) with different flavors: