7

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.

pawegio
  • 1,722
  • 3
  • 17
  • 29

6 Answers6

13

This is a bug in kotlin-gradle plugin for Android and it will be fixed in 0.9.488 or higher.

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
  • 3
    We've already fixed it, so you can simply use snapshot kotlin plugin. – Natalia Selezneva Nov 06 '14 at 10:50
  • I am seeing this error in kotlin-gradle 0.12.1230, not sure how to fix.. I've tried Jayson's answer below adding my /src/debug folder to main.java.srcDirs or debug.java.srcDirs but it didn't affect anything. Plus I only have a res folder in /src/debug, no Kotlin files, so I don't see why it should make a difference. – Damien Diehl Jul 31 '15 at 18:41
  • Note for non Android users, Intellij 16 EAP has added support for multiple source sets now aligned with the Gradle model, this fixes most of these types of issue related to Gradle across the board. – Jayson Minard Dec 31 '15 at 00:11
  • still get classnotfoundexception on plugin version 1.0.5 – filthy_wizard Dec 20 '16 at 15:26
9

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

Nguyen Minh Binh
  • 23,891
  • 30
  • 115
  • 165
  • 1
    I am able to launch into MainActivityKotlin.kt if I add the line: `apply plugin: "kotlin-android"` to the app `build.gradle` file. Without this line, I get the same error. Note that the "configure Kotlin for project" action in Android Studio adds a kotlin plugin to the top level `build.gradle` file with the line: `apply plugin: 'kotlin'`. Kotlin version level as of this writing is also 1.1.1. – Jim Andreas Apr 05 '17 at 08:45
2

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'   
}
Jayson Minard
  • 84,842
  • 38
  • 184
  • 227
  • Thanks, tried but not working as well. I'd like to compile myflavor/kotlin, myflavor2/kotlin, myflavor3/kotlin to separate apk files, where main/kotlin has core sources for each apk. – pawegio Nov 04 '14 at 12:41
  • And these can't be separate modules (Gradle subprojects)? – Jayson Minard Nov 04 '14 at 15:13
  • This is actually not what I want to achieve - I need independent flavors with specified packeges and sources, but as Natalia wrote - this is a bug, so now I'm waiting for a fix. – pawegio Nov 05 '14 at 20:51
  • 1
    Note for non Android users, Intellij 16 EAP has added support for multiple source sets now aligned with the Gradle model, this fixes most of these types of issue related to Gradle across the board. – Jayson Minard Dec 31 '15 at 00:11
2

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'
}
cookiemonster
  • 55
  • 1
  • 9
  • This answer solved my issue. But I found .kt files are put side by side with other .java files. So change `main.java.srcDirs` from `src/main/kotlin` to `src/main/java` does make sense. – Robert Jan 03 '17 at 02:14
0

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'
}
GoRoS
  • 5,183
  • 2
  • 43
  • 66
0

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"
...
}
Matthew Cawley
  • 2,828
  • 1
  • 31
  • 42