ClassNotFoundException和NoClassDefFoundError的解决办法

程序在其他手机都没有问题,但在刷到版本稍微较低的平板或手机上时就会直接闪退,并报出以下异常: 

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.simple/com.simple.activity.LoadingActivity}:
java.lang.ClassNotFoundException: Didn't find class "com.simple.activity.LoadingActivity" on path: DexPathList[[zip file "/data/app/com.winstars.petclient-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.simple-2, /vendor/lib, /system/lib]] at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2124) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2247) at android.app.ActivityThread.access$800(ActivityThread.java:139) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5028) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.ClassNotFoundException: Didn't find class "com.simple.activity.LoadingActivity" on path: DexPathList[[zip file "/data/app/com.winstars.petclient-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.winstars.petclient-2, /vendor/lib, /system/lib]] at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56) at java.lang.ClassLoader.loadClass(ClassLoader.java:497) at java.lang.ClassLoader.loadClass(ClassLoader.java:457) at android.app.Instrumentation.newActivity(Instrumentation.java:1061) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2115) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2247)  at android.app.ActivityThread.access$800(ActivityThread.java:139)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:136)  at android.app.ActivityThread.main(ActivityThread.java:5028)  at java.lang.reflect.Method.invokeNative(Native Method)  at java.lang.reflect.Method.invoke(Method.java:515) ... java.lang.NoClassDefFoundError: com.simple.service.SimpleService$1 at com.simple.service.SimpleService.<init>(SimpleService.java:29) at java.lang.Class.newInstanceImpl(Native Method) at java.lang.Class.newInstance(Class.java:1208) at android.app.ActivityThread.handleCreateService(ActivityThread.java:2554) at android.app.ActivityThread.access$1800(ActivityThread.java:139) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5028) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607) at dalvik.system.NativeStart.main(Native Method)

试过网上的一些改包名,check jar包的导入等,没反应。

 

我自己的解决办法是使用MultiDex——

如果你build.gradle中设置的minSdk >= 21,则只需在build.gradle中配置:

android {
    defaultConfig {
        ...
        minSdkVersion 21 
        targetSdkVersion 26
        multiDexEnabled true
    }
    ...
}

 

如果你build.gradle中设置的minSdk <= 20,就要使用multidex support library:

android {
    defaultConfig {
        ...
        minSdkVersion 20 
        targetSdkVersion 26
        multiDexEnabled true
    }
    ...
}

dependencies {
  compile 'com.android.support:multidex:1.0.1'
}

注意还没完,还需要配置一下Application:

如果没有重写Application,在AndroidManifest.xml中进行如下配置:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.simple.app">
    <application
            android:name="android.support.multidex.MultiDexApplication" >
        ...
    </application>
</manifest>

 

如果重写了Application,你可以直接用你的Application类继承MultiDexApplication,具体如下所示:

public class MyApplication extends MultiDexApplication { ... }

 

或者不继承MultiDexApplication,直接在你的Application类的初始化中对MultiDex进行初始化

public class MyApplication extends Application {
  @Override
  protected void attachBaseContext(Context context) {         
      super.attachBaseContext(context); 
      Multidex.install(this); 
  }
}

 

参考链接:https://blog.csdn.net/k1457047898/article/details/53467238

 

posted @ 2018-06-06 09:44  Sharley  阅读(2366)  评论(0编辑  收藏  举报