我有一个使用Dagger 2的项目,我无法在
Android 4上运行应用程序,它崩溃了.但是在Android 5上它运行得很好.所以这是我得到的错误:
08-05 05:03:38.076 25444-25444/app.xqute.com.xqute E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: app.xqute.com.xqute,PID: 25444 java.lang.NoClassDefFoundError: app.xqute.com.xqute.AppModule_ProvideProfileFactory at app.xqute.com.xqute.DaggerApp_AppComponent.initialize(DaggerApp_AppComponent.java:58) at app.xqute.com.xqute.DaggerApp_AppComponent.<init>(DaggerApp_AppComponent.java:50) at app.xqute.com.xqute.DaggerApp_AppComponent.<init>(DaggerApp_AppComponent.java:35) at app.xqute.com.xqute.DaggerApp_AppComponent$Builder.build(DaggerApp_AppComponent.java:151) at app.xqute.com.xqute.App.onCreate(App.java:63) at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1007) at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4328) at android.app.ActivityThread.access$1500(ActivityThread.java:135) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5001) 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:785) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) at dalvik.system.NativeStart.main(Native Method)
这是build.gradle文件,其中包含我正在使用的设置:
apply plugin: 'com.android.application' apply plugin: 'me.tatarka.retrolambda' apply plugin: 'com.neenbedankt.android-apt' android { compileSdkVersion 22 buildToolsVersion "22.0.1" defaultConfig { applicationId "app.xqute.com.xqute" multiDexEnabled = true minSdkVersion 15 targetSdkVersion 22 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } signingConfigs { release { storeFile file("xQute.jks") storePassword "saswat123" keyAlias "xQute" keyPassword "saswat123" } } buildTypes { release { minifyEnabled false shrinkResources true proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro' signingConfig signingConfigs.release } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } lintOptions { abortOnError false } } dependencies { compile fileTree(include: ['*.jar'],dir: 'libs') apt "com.google.dagger:dagger-compiler:2.0" provided 'javax.annotation:jsr250-api:1.0' androidTestCompile 'com.android.support.test:testing-support-lib:0.1' androidTestCompile 'org.assertj:assertj-core:1.6.1' androidTestCompile 'com.squareup.assertj:assertj-android:1.0.0' testCompile 'junit:junit:4.12' testCompile 'org.assertj:assertj-core:1.6.1' compile 'com.android.support:appcompat-v7:22.2.0' compile 'com.google.dagger:dagger:2.0' compile 'com.jakewharton.timber:timber:3.1.0' compile 'com.squareup.retrofit:retrofit:1.9.0' compile 'com.jakewharton:butterknife:6.1.0' compile 'com.facebook.fresco:fresco:0.5.0' compile 'com.google.android.gms:play-services:7.5.0' compile 'com.rengwuxian.materialedittext:library:2.1.3' compile 'com.github.rey5137:material:1.1.1' compile 'com.android.support:support-v4:22.2.0' compile 'com.android.support:recyclerview-v7:22.2.0' testCompile 'org.mockito:mockito-core:1.10.19' compile files('libs/Parse-1.9.2.jar') } configurations { compile.exclude module: 'support-annotations' }
任何想法为什么它适用于Android 5但不适用于Android 4?
解决方法
对于低于21的android API级别,您需要在构建gradle文件中添加以下依赖项,并且需要将Application类更改为MultiDexApplication,如下所示:
在app build.gradle文件中:
Implementation 'com.android.support:multidex:1.0.3'
在您的BaseApplication中将Application类更改为MultiDexApplication:
public class BaseApplication extends MultiDexApplication { @Override public void onCreate() { super.onCreate(); } }