程序在其他手机都没有问题,但在刷到版本稍微较低的平板或手机上时就会直接闪退,并报出以下异常:
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:)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:)
at android.app.ActivityThread.access$(ActivityThread.java:)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:)
at android.os.Handler.dispatchMessage(Handler.java:)
at android.os.Looper.loop(Looper.java:)
at android.app.ActivityThread.main(ActivityThread.java:)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:)
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:)
at java.lang.ClassLoader.loadClass(ClassLoader.java:)
at java.lang.ClassLoader.loadClass(ClassLoader.java:)
at android.app.Instrumentation.newActivity(Instrumentation.java:)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:)
at android.app.ActivityThread.access$(ActivityThread.java:)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:)
at android.os.Handler.dispatchMessage(Handler.java:)
at android.os.Looper.loop(Looper.java:)
at android.app.ActivityThread.main(ActivityThread.java:)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:) ... java.lang.NoClassDefFoundError: com.simple.service.SimpleService$
at com.simple.service.SimpleService.<init>(SimpleService.java:)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:)
at android.app.ActivityThread.access$(ActivityThread.java:)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:)
at android.os.Handler.dispatchMessage(Handler.java:)
at android.os.Looper.loop(Looper.java:)
at android.app.ActivityThread.main(ActivityThread.java:)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:)
at dalvik.system.NativeStart.main(Native Method)
试过网上的一些改包名,check jar包的导入等,没反应。
我自己的解决办法是使用MultiDex——
如果你build.gradle中设置的minSdk >= 21,则只需在build.gradle中配置:
android {
defaultConfig {
...
minSdkVersion
targetSdkVersion
multiDexEnabled true
}
...
}
如果你build.gradle中设置的minSdk <= 20,就要使用multidex support library:
android {
defaultConfig {
...
minSdkVersion
targetSdkVersion
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