在运行应用程序时集成Dagger 2以实现ImageDownloader库的依赖项注入后,我遇到了这个问题。
这就是我所做的:
在build.gradle中。我添加了以下几行:
implementation "com.google.dagger:dagger:2.16"
implementation "com.google.dagger:dagger-android-support:2.16"
annotationProcessor "com.google.dagger:dagger-compiler:2.16"
annotationProcessor "com.google.dagger:dagger-android-processor:2.16"
这是我的类API,我想在其中将依赖项与活动类分开:
public class ImageDownloaderApi {
@Inject
public ImageDownloaderApi(){
}
//some other methods here
}
在我的MainActivity
中,添加了以下几行:
public class MainActivity extends Activity {
@Inject
ImageDownloaderApi imageDownloaderApi;
@Override
protected void onCreate(Bundle savedInstanceState) {
AndroidInjection.inject(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
然后创建一个Application
对象:
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
}
}
然后通过清单清单注册Application
类:
<application
android:name=".MyApplication"
我创建了一个名为MyApplicationModule
的模块,如下所示:
@Module
public abstract class MyApplicationModule {
@ContributesAndroidInjector
abstract MainActivity contributeActivityInjector();
}
我创建了一个名为MyApplicationComponent
的接口:
@Component(modules = { AndroidInjectionModule.class, MyApplicationModule.class})
public interface MyApplicationComponent extends AndroidInjector<MyApplication> {
}
MyApplication
类:
public class MyApplication extends Application implements HasActivityInjector {
@Inject
DispatchingAndroidInjector<Activity> dispatchingAndroidInjector;
@Override
public void onCreate() {
super.onCreate();
DaggerMyApplicationComponent.create().inject(this);
}
@Override
public DispatchingAndroidInjector<Activity> activityInjector() {
return dispatchingAndroidInjector;
}
}
现在,在执行此操作后,当我在模拟器中运行应用程序时,该应用程序崩溃并显示以下日志:
--------- beginning of crash
11-20 14:51:03.201 6745-6745/com.my.app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.my.app, PID: 6745
java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/common/collect/ImmutableMap;
at com.my.app.di.DaggerMyApplicationComponent.getMapOfClassOfAndProviderOfFactoryOf(DaggerMyApplicationComponent.java:36)
at com.my.app.di.DaggerMyApplicationComponent.getDispatchingAndroidInjectorOfActivity(DaggerMyApplicationComponent.java:42)
at com.my.app.di.DaggerMyApplicationComponent.injectMyApplication(DaggerMyApplicationComponent.java:68)
at com.my.app.di.DaggerMyApplicationComponent.inject(DaggerMyApplicationComponent.java:62)
at com.my.app.di.DaggerMyApplicationComponent.inject(DaggerMyApplicationComponent.java:16)
at com.my.app.di.MyApplication.onCreate(MyApplication.java:19)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1012)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4553)
at android.app.ActivityThread.access$1500(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.common.collect.ImmutableMap" on path: DexPathList[[zip file "/data/app/com.my.app-1/base.apk"],nativeLibraryDirectories=[/data/app/com.my.app-1/lib/x86, /vendor/lib, /system/lib]]
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.my.app.di.DaggerMyApplicationComponent.getMapOfClassOfAndProviderOfFactoryOf(DaggerMyApplicationComponent.java:36)
at com.my.app.di.DaggerMyApplicationComponent.getDispatchingAndroidInjectorOfActivity(DaggerMyApplicationComponent.java:42)
at com.my.app.di.DaggerMyApplicationComponent.injectMyApplication(DaggerMyApplicationComponent.java:68)
at com.my.app.di.DaggerMyApplicationComponent.inject(DaggerMyApplicationComponent.java:62)
at com.my.app.di.DaggerMyApplicationComponent.inject(DaggerMyApplicationComponent.java:16)
at com.my.app.di.MyApplication.onCreate(MyApplication.java:19)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1012)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4553)
at android.app.ActivityThread.access$1500(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Suppressed: java.lang.ClassNotFoundException: com.google.common.collect.ImmutableMap
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)
... 18 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
有关其他信息。在上面发布的日志之前,我也收到了此警告行
11-20 14:51:03.172 6745-6745/? E/libprocessgroup: failed to make and chown /acct/uid_10058: Read-only file system
答案 0 :(得分:1)
@EpicPandaForce评论了番石榴版本不匹配。我知道Dagger 2需要Guava,所以我在build.gradle
文件中添加了Guava库,问题就解决了。
在build.gradle
中添加此行:
api 'com.google.guava:guava:24.1-android'