ClassNotFoundException in custom taste using kotlin

I defined my own taste 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:

enter image description here

but ... I get the 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 would appreciate your help.

+5
source share
6 answers

This is a bug in the kotlingradle plugin for Android, and it will be fixed in 0.9.488 or higher. Thanks for the report!

+11
source

In my case, I forgot to apply plugin: 'kotlin-android' in the gradle file. Although my project was successfully created, it raises a java.lang.ClassNotFoundException at runtime. I am using Kotlin 1.1.1

+8
source

When I converted the source to kotlin, I also got an error. My fix was changing sourceSets in gradle.

Before

 sourceSets { main.java.srcDirs += 'src/main/kotlin' } 

Now

 sourceSets { main.java.srcDirs += 'src/main/java' } 
+2
source

Try adding both to main.java.srcDirs because you want them both to compile yes?

 sourceSets { main.java.srcDirs += 'src/main/kotlin' main.java.srcDirs += 'src/myflavor/kotlin' } 
+1
source

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 various options:

 sourceSets { androidTest { java.srcDir file('src/androidTest') } test.java.srcDirs += 'src/test/kotlin' main.java.srcDirs += 'src/main/kotlin' flavor1.java.srcDirs += 'src/flavor1/kotlin' flavor2.java.srcDirs += 'src/flavor2/kotlin' } 
0
source

I had the same problem. In my case, I created a library and a test application to access the library.

When I converted the project to kotlin, it added the corresponding kotlin dependencies to the gradle file for the library, but not for the main application.

Make sure the following lines are in all of your respective gradle files:

 apply plugin: "kotlin-android" dependencies { ... implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" ... } 
0
source

Source: https://habr.com/ru/post/1206152/


All Articles