启动Kotlin项目崩溃报 java.lang.ClassNotFoundException异常

今天闲来无事学准备在Android Studio下编译一个Ktolin Demo 运行一下。以为环境配置好了直接运行,编译没有错误,结果运行直接报了这样一个错误:

31013-31013/com.yan.newbe E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.yan.newbe, PID: 31013
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.yan.newbe/com.yan.newbe.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.yan.newbe.MainActivity" on path: DexPathList[[zip file "/data/app/com.yan.newbe-2/base.apk"],nativeLibraryDirectories=[/data/app/com.yan.newbe-2/lib/arm, /vendor/lib, /system/lib]]
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3133)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3416)
at android.app.ActivityThread.access$1100(ActivityThread.java:229)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7407)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1321)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1211)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.yan.newbe.MainActivity" on path: DexPathList[[zip file "/data/app/com.yan.newbe-2/base.apk"],nativeLibraryDirectories=[/data/app/com.yan.newbe-2/lib/arm, /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:1096)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3123)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3416)
at android.app.ActivityThread.access$1100(ActivityThread.java:229)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7407)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1321)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1211)
Suppressed: java.lang.ClassNotFoundException: com.yan.newbe.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)
... 12 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available

项目很简单,就是以Kotlin写了一个MainActivity,布局中间一个TextView,然后后APP启动后显示一个给TextView显示Hello。结果启动后直接崩溃了,log如上。开始以为没有在清单文件注册,结果并不是。后来经过一系列排查发现是少在app目录下的build.gradle少添加了一行代码。

我的解决方法:

在app目录下的build.gradle配置文件中添加一行
apply plugin:'kotlin-android'

启动Kotlin项目崩溃报 java.lang.ClassNotFoundException异常_第1张图片

最后放出Demo地址

你可能感兴趣的:(启动Kotlin项目崩溃报 java.lang.ClassNotFoundException异常)