Kotlin启动应用程序崩溃

时间:2017-07-30 08:18:25

标签: android kotlin

我的应用程序是用kotlin构建的,这是我的build.gradle文件:

compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

和我的工作区的build.gradle文件:

ext.kotlin_version = '1.1.3-2'
ext.kotlin_plugin_version = '1.1.1'
dependencies {
    classpath "io.realm:realm-gradle-plugin:3.4.0"
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_plugin_version"
}

在我的项目中,我已经使用java8和jack,所以gradle-plugin版本只能是1.1.1

当我启动我的应用程序时,它会随机崩溃,这是日志:

FATAL EXCEPTION: main                                                                                
Process: com.ss.android.caijing.stock.debug, PID: 31962                                                                                 
java.lang.NoClassDefFoundError: Failed resolution of: Lkotlin/jvm/internal/Intrinsics;                                                                                      
   at com.ss.android.caijing.stock.main.MainActivity.initData(MainActivity.kt:33)                                                                                      
   at com.ss.android.caijing.stock.base.BaseActivity.onCreate(BaseActivity.java:25)                                                                                      
   at android.app.Activity.performCreate(Activity.java:6367)                                                                                      
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)                                                                                      
   at com.bytedance.frameworks.plugin.hook.InstrumentationHook$PluginInstrumentation.callActivityOnCreate(InstrumentationHook.java:248)                                                                                      
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2404)                                                                                      
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2511)                                                                                      
   at android.app.ActivityThread.access$900(ActivityThread.java:165)                                                                                      
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1375)                                                                                      
   at android.os.Handler.dispatchMessage(Handler.java:102)                                                                                      
   at android.os.Looper.loop(Looper.java:150)                                                                                      
   at android.app.ActivityThread.main(ActivityThread.java:5621)                                                                                      
   at java.lang.reflect.Method.invoke(Native Method)                                                                                      
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)                                                                                      
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)                                                                                      
Caused by: java.lang.ClassNotFoundException: Didn't find class "kotlin.jvm.internal.Intrinsics" on path: DexPathList[[zip file "/data/app/com.ss.android.caijing.stock.debug-1/base.apk"],nativeLibraryDirectories=[/data/app/com.ss.android.caijing.stock.debug-1/lib/arm64, /data/app/com.ss.android.caijing.stock.debug-1/base.apk!/lib/arm64-v8a, /vendor/lib64, /system/lib64]]                                                                                      
   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 com.ss.android.caijing.stock.main.MainActivity.initData(MainActivity.kt:33)                                                                                       
   at com.ss.android.caijing.stock.base.BaseActivity.onCreate(BaseActivity.java:25)                                                                                       
   at android.app.Activity.performCreate(Activity.java:6367)                                                                                       
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)                                                                                       
   at com.bytedance.frameworks.plugin.hook.InstrumentationHook$PluginInstrumentation.callActivityOnCreate(InstrumentationHook.java:248)                                                                                       
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2404)                                                                                       
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2511)                                                                                       
   at android.app.ActivityThread.access$900(ActivityThread.java:165)                                                                                       
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1375)                                                                                       
   at android.os.Handler.dispatchMessage(Handler.java:102)                                                                                       
   at android.os.Looper.loop(Looper.java:150)                                                                                       
   at android.app.ActivityThread.main(ActivityThread.java:5621)                                                                                       
   at java.lang.reflect.Method.invoke(Native Method)                                                                                       
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)                                                                                       
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)                                                                                 
Suppressed: java.lang.ClassNotFoundException: kotlin.jvm.internal.Intrinsics not found                                                                                      
   at com.bytedance.frameworks.plugin.core.DelegateClassLoader.findClass(DelegateClassLoader.java:137)                                                                                      
   at java.lang.ClassLoader.loadClass(ClassLoader.java:511)                                                                                      
   at java.lang.ClassLoader.loadClass(ClassLoader.java:504)                                                                                                         
Suppressed: java.lang.ClassNotFoundException: kotlin.jvm.internal.Intrinsics                                                                                      
   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)                                                                                                          
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available                                                                                      
Caused by: java.lang.reflect.InvocationTargetException                                                                                      
   at java.lang.reflect.Method.invoke(Native Method)                                                                                      
   at com.bytedance.frameworks.plugin.core.DelegateClassLoader.findClass(DelegateClassLoader.java:60)                                                                                                            
Caused by: java.lang.ClassNotFoundException: Didn't find class "kotlin.jvm.internal.Intrinsics" on path: DexPathList[[zip file "/data/app/com.ss.android.caijing.stock.debug-1/base.apk"],nativeLibraryDirectories=[/data/app/com.ss.android.caijing.stock.debug-1/lib/arm64, /data/app/com.ss.android.caijing.stock.debug-1/base.apk!/lib/arm64-v8a, /vendor/lib64, /system/lib64]]                                                                                      
   at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)


 private var mPagerAdapter: MainFragmentAdapter? = null 

覆盖

fun initData() { mPagerAdapter = MainFragmentAdapter(supportFragmentManager, this) }

崩溃发生在mPageAdapter的init中,MainFragmentPagerAdapter的构造函数如下:

class MainFragmentAdapter : FragmentPagerAdapter { private val mContext:     Context constructor(fm: FragmentManager, context: Context) : super(fm) { mContext = context }
很可能有人帮帮我了,很多!

2 个答案:

答案 0 :(得分:2)

将以下内容添加为应用级依赖项,为我修复了它:

implementation "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"

---编辑---

现已被以下内容取代:

implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

答案 1 :(得分:0)

在您的应用程序gradle中添加以下两行并同步您的项目。

apply plugin: 'kotlin-android'
apply plugin: "kotlin-android-extensions"

在你的android studio中配置kotlin插件可能有问题。如果你仍然遇到同样的问题,请参考下面的链接它可能会有所帮助。

Setup tutorial 1

Setup tutorial 2

最好的运气