ClassNotFoundException in custom flavor using kotl

2019-06-20 05:43发布

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:

enter image description here

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.

6条回答
冷血范
2楼-- · 2019-06-20 06:02

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

Before:

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

Now:

sourceSets {     
    main.java.srcDirs += 'src/main/java'
}
查看更多
姐就是有狂的资本
3楼-- · 2019-06-20 06:05

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!

查看更多
放荡不羁爱自由
4楼-- · 2019-06-20 06:06

In my case, I forgot apply plugin: 'kotlin-android' in the gradle file. Although my project was built successfully, it throw java.lang.ClassNotFoundException at runtime. I'm using Kotlin 1.1.1

查看更多
Explosion°爆炸
5楼-- · 2019-06-20 06:08

I 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:

apply plugin: "kotlin-android"

dependencies {
...
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
...
}
查看更多
Evening l夕情丶
6楼-- · 2019-06-20 06:22

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'   
}
查看更多
ゆ 、 Hurt°
7楼-- · 2019-06-20 06:22

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:

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'
}
查看更多
登录 后发表回答