切换到 Android Studio 2.0 后使用 Dagger 2 时出现 NoClassDefFoundError
NoClassDefFoundError When Using Dagger 2 After Switching to Android Studio 2.0
在升级到 Android Studio 2.0 之前,我可以毫无问题地使用 Dagger 2。现在我收到 NoClassDefFoundError
这让我停了一天多的时间,我正在寻求帮助。
似乎 Gradle 不能在我的 AppModule
class 中使用,尽管很明显它在我的项目中。我什至包含了 set multiDexEnabled true
,即使我的项目只有几个文件。
我在网上能找到的所有内容都说您可以单击要导入的库。 Android工作室没有这样的奢侈。
如有任何帮助,我们将不胜感激,我将永远忠诚于您。
04-21 17:26:54.006 7875-7875/com.androidtitan.spotscoreapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.androidtitan.spotscoreapp, PID: 7875
java.lang.NoClassDefFoundError: com.androidtitan.spotscoreapp.main.injection.AppModule_ProvidesApplicationFactory
at com.androidtitan.spotscoreapp.main.injection.DaggerAppComponent.initialize(DaggerAppComponent.java:31)
at com.androidtitan.spotscoreapp.main.injection.DaggerAppComponent.<init>(DaggerAppComponent.java:23)
at com.androidtitan.spotscoreapp.main.injection.DaggerAppComponent.<init>(DaggerAppComponent.java:0)
at com.androidtitan.spotscoreapp.main.injection.DaggerAppComponent$Builder.build(DaggerAppComponent.java:66)
at com.androidtitan.spotscoreapp.App.onCreate(App.java:28)
at com.android.tools.fd.runtime.BootstrapApplication.onCreate(BootstrapApplication.java:326)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1020)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5010)
at android.app.ActivityThread.access00(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1482)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5835)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
package com.androidtitan.spotscoreapp.main.injection;
import android.app.Application;
import android.content.Context;
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
AppModule
@Module
public class AppModule {
private static Application application;
public AppModule(Application application) {
this.application = application;
}
@Provides @Singleton
Application providesApplication() {
return application;
}
@Provides @Singleton
Context providesApplicationContext() {
return application.getBaseContext();
}
}
应用组件
package com.androidtitan.spotscoreapp.main.injection;
import android.app.Application;
import android.content.Context;
import com.androidtitan.spotscoreapp.main.ui.activity.LoginActivity;
import com.androidtitan.spotscoreapp.main.ui.fragment.LoginFragment;
import com.androidtitan.spotscoreapp.main.ui.fragment.SignUpFragment;
import javax.inject.Singleton;
import dagger.Component;
@Singleton
@Component(
modules = { AppModule.class,
LoginPresenterModule.class }
)
public interface AppComponent {
Application getApplication();
Context getApplicationContext();
void inject(LoginFragment activity);
}
应用程序扩展了应用程序
import android.app.Application;
import android.support.multidex.MultiDexApplication;
import com.androidtitan.spotscoreapp.main.injection.AppComponent;
import com.androidtitan.spotscoreapp.main.injection.AppModule;
import com.androidtitan.spotscoreapp.main.injection.DaggerAppComponent;
import com.firebase.client.Firebase;
import timber.log.Timber;
public class App extends Application {
private static AppComponent appComponent;
@Override
public void onCreate() {
super.onCreate();
Firebase.setAndroidContext(this);
appComponent = DaggerAppComponent.builder()
.appModule(new AppModule(this))
.build();
if (BuildConfig.DEBUG) {
Timber.plant(new Timber.DebugTree());
}
}
public static AppComponent getAppComponent(){
return appComponent;
}
}
build.gradle(模块:应用程序)
apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'
android {
compileSdkVersion 23
buildToolsVersion "24.0.0 rc1"
defaultConfig {
applicationId "com.androidtitan.spotscoreapp"
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "1.0"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
packagingOptions {
exclude 'META-INF/DEPENDENCIES.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
apt 'com.google.dagger:dagger-compiler:2.0'
compile 'com.android.support:multidex:1.0.0'
provided 'com.google.dagger:dagger-compiler:2.0'
provided 'org.glassfish:javax.annotation:10.0-b28'
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.jakewharton.timber:timber:4.1.2'
compile 'com.android.support:design:23.3.0'
compile 'com.jakewharton:butterknife:7.0.1'
compile 'com.firebase:firebase-client-android:2.3.1'
}
build.gradle(项目:spotscoreapp)
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.0.0'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Android清单
`
<uses-permission android:name="android.permission.INTERNET" />
<application
android:name=".App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity
android:name=".main.ui.activity.LoginActivity"
android:label="@string/title_activity_login"
android:theme="@style/AppTheme.NoActionBar" />
<activity android:name=".main.ui.activity.MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
`
我有一个非常相似的问题,并通过在 Android Studio 中做一些非常平常的事情来解决它。你基本上需要
Invalidate Caches and then Clean Project
您知道,Dagger2 在编译时生成大量代码,有时事情会变得一团糟,尤其是当您使用由 Android Studio 2.0
提供的 Instant 运行 时
您的 Application
没有从 MultiDexApplication
延伸,也没有
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
并且您应该禁用 Instant 运行。
尝试将 class 路径 "com.google.dagger:dagger:2.4" 添加到主项目的 build.gradle。
注意:保持版本号与模块的 build.gradle
相同
在升级到 Android Studio 2.0 之前,我可以毫无问题地使用 Dagger 2。现在我收到 NoClassDefFoundError
这让我停了一天多的时间,我正在寻求帮助。
似乎 Gradle 不能在我的 AppModule
class 中使用,尽管很明显它在我的项目中。我什至包含了 set multiDexEnabled true
,即使我的项目只有几个文件。
我在网上能找到的所有内容都说您可以单击要导入的库。 Android工作室没有这样的奢侈。
如有任何帮助,我们将不胜感激,我将永远忠诚于您。
04-21 17:26:54.006 7875-7875/com.androidtitan.spotscoreapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.androidtitan.spotscoreapp, PID: 7875
java.lang.NoClassDefFoundError: com.androidtitan.spotscoreapp.main.injection.AppModule_ProvidesApplicationFactory
at com.androidtitan.spotscoreapp.main.injection.DaggerAppComponent.initialize(DaggerAppComponent.java:31)
at com.androidtitan.spotscoreapp.main.injection.DaggerAppComponent.<init>(DaggerAppComponent.java:23)
at com.androidtitan.spotscoreapp.main.injection.DaggerAppComponent.<init>(DaggerAppComponent.java:0)
at com.androidtitan.spotscoreapp.main.injection.DaggerAppComponent$Builder.build(DaggerAppComponent.java:66)
at com.androidtitan.spotscoreapp.App.onCreate(App.java:28)
at com.android.tools.fd.runtime.BootstrapApplication.onCreate(BootstrapApplication.java:326)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1020)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5010)
at android.app.ActivityThread.access00(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1482)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5835)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
package com.androidtitan.spotscoreapp.main.injection;
import android.app.Application;
import android.content.Context;
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
AppModule
@Module
public class AppModule {
private static Application application;
public AppModule(Application application) {
this.application = application;
}
@Provides @Singleton
Application providesApplication() {
return application;
}
@Provides @Singleton
Context providesApplicationContext() {
return application.getBaseContext();
}
}
应用组件
package com.androidtitan.spotscoreapp.main.injection;
import android.app.Application;
import android.content.Context;
import com.androidtitan.spotscoreapp.main.ui.activity.LoginActivity;
import com.androidtitan.spotscoreapp.main.ui.fragment.LoginFragment;
import com.androidtitan.spotscoreapp.main.ui.fragment.SignUpFragment;
import javax.inject.Singleton;
import dagger.Component;
@Singleton
@Component(
modules = { AppModule.class,
LoginPresenterModule.class }
)
public interface AppComponent {
Application getApplication();
Context getApplicationContext();
void inject(LoginFragment activity);
}
应用程序扩展了应用程序
import android.app.Application;
import android.support.multidex.MultiDexApplication;
import com.androidtitan.spotscoreapp.main.injection.AppComponent;
import com.androidtitan.spotscoreapp.main.injection.AppModule;
import com.androidtitan.spotscoreapp.main.injection.DaggerAppComponent;
import com.firebase.client.Firebase;
import timber.log.Timber;
public class App extends Application {
private static AppComponent appComponent;
@Override
public void onCreate() {
super.onCreate();
Firebase.setAndroidContext(this);
appComponent = DaggerAppComponent.builder()
.appModule(new AppModule(this))
.build();
if (BuildConfig.DEBUG) {
Timber.plant(new Timber.DebugTree());
}
}
public static AppComponent getAppComponent(){
return appComponent;
}
}
build.gradle(模块:应用程序)
apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'
android {
compileSdkVersion 23
buildToolsVersion "24.0.0 rc1"
defaultConfig {
applicationId "com.androidtitan.spotscoreapp"
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "1.0"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
packagingOptions {
exclude 'META-INF/DEPENDENCIES.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
apt 'com.google.dagger:dagger-compiler:2.0'
compile 'com.android.support:multidex:1.0.0'
provided 'com.google.dagger:dagger-compiler:2.0'
provided 'org.glassfish:javax.annotation:10.0-b28'
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.jakewharton.timber:timber:4.1.2'
compile 'com.android.support:design:23.3.0'
compile 'com.jakewharton:butterknife:7.0.1'
compile 'com.firebase:firebase-client-android:2.3.1'
}
build.gradle(项目:spotscoreapp)
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.0.0'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Android清单
`
<uses-permission android:name="android.permission.INTERNET" />
<application
android:name=".App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity
android:name=".main.ui.activity.LoginActivity"
android:label="@string/title_activity_login"
android:theme="@style/AppTheme.NoActionBar" />
<activity android:name=".main.ui.activity.MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
`
我有一个非常相似的问题,并通过在 Android Studio 中做一些非常平常的事情来解决它。你基本上需要
Invalidate Caches and then Clean Project
您知道,Dagger2 在编译时生成大量代码,有时事情会变得一团糟,尤其是当您使用由 Android Studio 2.0
提供的 Instant 运行 时您的 Application
没有从 MultiDexApplication
延伸,也没有
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
并且您应该禁用 Instant 运行。
尝试将 class 路径 "com.google.dagger:dagger:2.4" 添加到主项目的 build.gradle。
注意:保持版本号与模块的 build.gradle
相同