我必须在Kotlin中创建一个库(aar),并且必须在使用Java制作的Android应用程序中使用它。 Kotlin库有一个带有伴随函数的类,可以在Java应用程序中静态使用。当我在Java中使用我的Android应用程序中的静态方法时,我得到“ClassNotFoundException”。任何人都可以帮助我如何在Android Java应用程序中使用伴侣方法。请在运行我的应用程序时找到我的代码片段和异常。 我只在Android Java项目中使用Kotlin AAR而在Android Kotlin Project中使用时才会出现此错误。在Kotlin库或Android Java项目中是否缺少某些东西。 Kotlin Library片段:
class LogUtil {
companion object {
private const val LOG_TAG = "KOTLIN_LIBRARY"
@JvmStatic
fun log(message: String) {
Log.i(LOG_TAG, message)
}
}
}
Android活动代码段:
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);
}
}
例外:
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)
答案 0 :(得分:1)
缺少的类是 Kotlin标准库的一部分。如果您未在项目中使用 Kotlin ,则必须手动依赖它:
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.2.50'
此外,您可以确保从库中导出依赖项:
api 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.2.50'