Very annoying crash : java.lang.NoClassDefFoundError: Failed resolution of: <KotlinObject>


#1

Not sure if this is the right place to ask this but here goes,

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: https://gist.github.com/letronje/89990a0d705ceaf46d992db2352cb8dc


#2

Hi!

I could not reproduce your issue.
Am I right that by “every second run” you mean subsequent runs of Android Emulator?
Could you try to create a reproducible example project?


#3

I seem to have the same problem. It started happening after upgrading Android Build Tools from 2.1.2 to 2.1.3. This also forced me to upgrade Gradle from 2.10 to 2.14.1. I’m using multidex, multiple build flavors and am building on Windows atm.

It seems it is not just @JvmStatic, but all Kotlin classes (.kt) are not available.

free is one of the build flavors. The classes are not in build\intermediates\classes\free\debug, but I have found them in build\tmp\kotlin-classes\freeDebug. I think this was the case before the upgrade as well.

I tried to get the classes from the apk using dex2jar, but that resulted in an error and not all classes were exported. What tool would you suggest to check whether the class is actually in the apk? Or should I check something else to find the actual problem?


#4

I upgraded to android studio 2.2 beta 2 and kotlin 1.0.4-eap-62 . things are working now. Even instant run . Wooohooo!


#5

I believe I have similar issue with AS 2.3.1, gradle 3.3 and Kotlin 1.1.1.
The issue happens only on my windows machine. At work I use a mac and it works. My friends tried to reproduce it on his windows machine and he couldn’t.
All the project features (a lot of them) work properly and the app crashes only in one place.

I get:
java.lang.NoClassDefFoundError: com/project/name/SomeActivityKt$sam$Action1$22181393

For this code:

private fun getDataFor(id: String) {

    apiClient.somethingList(id)
            .map { response -> response.toSomethingList() }
            .lifecycle(this)
            .showProgress(this)
            .observeOnMainThread()
            .subscribe({
                LOG.info("Something list fetched ${it.size}")
                adapter.setItems(it)

            }, defaultOnError("Fetching something list failed"))
}

The defaultOnError function is:

fun <T> T.defaultOnError(message: String, vararg args: Any?): (Throwable) -> Unit where T : HasErrorHandler, T : Activity {
    val targetClass = javaClass
    val activity = this
    return { err ->
        val messageFormats = (args.toList() + err).toTypedArray()
        LoggerFactory.getLogger(targetClass).error(message, *messageFormats)
        errorHandler.default(err, activity)
    }
}

This function works in other activities but this one. Also, as I said, it works on a mac and on a friends machine.
If I remove this defaultOnError from subscribe it works OK.

Any ideas?


#6

I seem to have the same problem.My environment is Android Studio3.0 canary3 ,Android Build Tools 2.2.3,gradle 3.5 and i`m also using multidex.
the below is the log:
Caused by: java.lang.ClassNotFoundException: Didn’t find class “com.touguyun.activity.AboutUsActivity” on path: DexPathList[[zip file “/data/app/com.touguyun-2/base.apk”],nativeLibraryDirectories=[/data/app/com.touguyun-2/lib/x86, /system/fake-libs, /data/app/com.touguyun-2/base.apk!/lib/x86, /system/lib, /vendor/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at com.touguyun.activity.SetUpActivity.set_up_about_us(SetUpActivity.java:165)
at com.touguyun.activity.SetUpActivity_$2.onClick(SetUpActivity_.java:122)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)