Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.NoClassDefFoundError when using Kotlin AAR in Android Java application

I have to make a library(aar) in Kotlin and have to use that in Android application made using Java. Kotlin library has a class with companion functions to use statically in Java application. When I use that static method in my Android application in Java, I do get "ClassNotFoundException". Can anyone help me how to use companion methods in aar from Android Java application. Please find my code snippets and exception Im getting while running my application. And I do get this error only when using the Kotlin AAR in Android Java Project but not with Android Kotlin Project. Is there something Im missing in the Kotlin library or Android Java project. Kotlin Library snippet:

class LogUtil {
    companion object {
        private const val LOG_TAG = "KOTLIN_LIBRARY"
        @JvmStatic
        fun log(message: String) {
            Log.i(LOG_TAG, message)
        }
    }
}

Android Activity snippet:

public class MainActivity extends AppCompatActivity {

    private HaloSDK haloSDK;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        testKotlinSDK();
    }


    private void testKotlinSDK() {
        LogUtil.log("Hello");
    }

    public void showKotlinWebView(View view) {
        Intent intent = HaloWebActivity.Companion.createIntent(this, "www.abc.com");
        startActivity(intent);
    }
}

Exception:

java.lang.NoClassDefFoundError: Failed resolution of: Lkotlin/jvm/internal/Intrinsics;
        at com.abcd.kotlinlibrary.utils.LogUtil$Companion.log(Unknown Source:2)
        at com.abcd.kotlinlibrary.utils.LogUtil.log(Unknown Source:2)
        at com.abcd.thirdpartyapp.MainActivity.testKotlinSDK(MainActivity.java:25)
        at com.abcd.thirdpartyapp.MainActivity.onCreate(MainActivity.java:20)
        at android.app.Activity.performCreate(Activity.java:7174)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2910)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6940)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "kotlin.jvm.internal.Intrinsics" on path: DexPathList[[zip file "/data/app/com.abcd.thirdpartyapp-k2I71L3n7Qa6TEETe1GejQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.abcd.thirdpartyapp-k2I71L3n7Qa6TEETe1GejQ==/lib/arm64, /system/lib64, /system/vendor/lib64]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:93)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at com.abcd.kotlinlibrary.utils.LogUtil$Companion.log(Unknown Source:2) 
        at com.abcd.kotlinlibrary.utils.LogUtil.log(Unknown Source:2) 
        at com.abcd.thirdpartyapp.MainActivity.testKotlinSDK(MainActivity.java:25) 
        at com.abcd.thirdpartyapp.MainActivity.onCreate(MainActivity.java:20) 
        at android.app.Activity.performCreate(Activity.java:7174) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2910) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696) 
        at android.os.Handler.dispatchMessage(Handler.java:105) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6940) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374) 
like image 777
MaheshGupta Avatar asked Jun 14 '18 05:06

MaheshGupta


1 Answers

The missing class is part of the Kotlin Standard Library. You have to depend on it manually if you're not using Kotlin in your project:

implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.2.50'

Also you can make sure to export the dependency from your library as:

api 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.2.50'
like image 77
tynn Avatar answered Nov 10 '22 10:11

tynn