13

Every second run of our android app, we get a crash that says

java.lang.NoClassDefFoundError: Failed resolution of: Lin/blahapp/xxx/BlahUtil

BlahUtil is a kotlin object with @JvmStatic annotations in it. I call these static methods from the rest of the android app(All in java) .

We use multidex 1.0.1.

I am on android studio 2.1.2, using JDK 7.

Relevant gradle configs:

compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
        minSdkVersion 16
        targetSdkVersion 23
}
dexOptions {
        incremental true
        dexInProcess true
        javaMaxHeapSize "10g"
        preDexLibraries true
}
buildscript {
    ext.kotlin_version = '1.0.3'

    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}
apply plugin: 'kotlin-android'
dependencies {
  compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
  compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
}

Trace:

at in.blahapp.xxx.OurActivity 
at android.app.Activity.performCreate(Activity.java:6251)
at ndroid.app.Instrumentation.callActivityOnCreate
at android.app.ActivityThread.performLaunchActivity
at android.app.ActivityThread.handleLaunchActivity
at android.app.ActivityThread.-wrap11
at android.app.ActivityThread$H.handleMessage
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Caused by: java.lang.ClassNotFoundException: Didn't find class "in.blahapp.xxx.BlahUtil" on path: DexPathList[[dex file ....

logcat output

letronje
  • 9,002
  • 9
  • 45
  • 53
  • Does this always happen or only on every second build (or, in another variation, only on the first clean build)? – AndroidEx Aug 01 '16 at 06:30
  • Usually never happens on the first run that follows a clean build. crashes on the 2nd run when I make some changes and run again. 3rd run works. if i make a change again, it crashes on the immediately next run. then works on the run after that ..... and so on. – letronje Aug 01 '16 at 10:13
  • 3
    This sounds a lot like [this bug](https://youtrack.jetbrains.com/issue/KT-10733). I've had another variant of this bug when a Kotlin class wasn't found (=deleted) on the first clean compilation but appeared on the second incremental build. It's been discussed on the google tracker too, and the outcome was that we have to wait for the android gradle plugin 2.3 to be released. This issue prevented me from introducing Kotlin in my team and, unfortunately, I don't have any recommendations to share with you. – AndroidEx Aug 01 '16 at 20:42
  • Just curious - why did you make **javaMaxHeapSize "10g"**? – IgorGanapolsky Jul 18 '17 at 13:40
  • I have the same issue but only happens in certain devices. I build and .apk, and after installing it runs perfectly in some devices but crashes in a few ones (all version 7.x), throwing the exception NoClassDefFoundError for the Kotlin activity. – manuel Sep 13 '18 at 08:56

4 Answers4

3

You should turn off 'Instant Run'. Android Studio -> Preferences -> Build, Execution, Deployment -> Instant Run. Turn off everything.

BurtK
  • 1,016
  • 13
  • 12
1

java.lang.ClassNotFoundException is a fun exception to debug. Notably because it can occur from any number of reasons. In this case, due to the every other launch behavior, it's most likely occurring due to being unable to initialize the class. If you have resources that you load statically that are singleton in nature, open files, or any "exclusive" resources on class creation in the JVM, when it goes to initialize it the second time, as the class is already loaded into the JVM, regardless of whether or not you've restarted the application. When the second instance of loading the class occurs, it clashes with the existing one and both instances are removed from the JVM, making the third execution run just fine.

tl;dr I'd have to see your code to be positive, but it's most likely (especially with @JvmStatic annotations), that you're failing on the second static loading of the class. When it fails, all instances are removed from the JVM and the process repeats.

1

The only workaround I've found is to set android.compileOptions.incremental = false

See this issue for details.

Dmide
  • 6,422
  • 3
  • 24
  • 31
0

Try clean build. There may be some changes cached from another branch. Solved the problem when I was facing the same error

Giedrius Šlikas
  • 1,073
  • 12
  • 12