我不能让Guice Multibindings与RoboGuice一起使用。
当我的活动扩展android Activity时,多绑定工作正常。 当我的活动扩展RoboActivity时,多重绑定不起作用, 并抛出以下异常:
3450-3450/com.example.guicetest E/AndroidRuntime﹕ FATAL EXCEPTION: main
com.google.inject.ProvisionException:
Unable to provision, see the following errors:
1) Multibinder is not initialized
at com.example.guicetest.MainActivity$1$1.configure(MainActivity.java:35)
(via modules: com.example.guicetest.MainActivity$1$1 ->
com.google.inject.multibindings.Multibinder$RealMultibinder)
while locating java.util.Set<com.example.guicetest.MyInterface>
for parameter 0 at com.example.guicetest.MyFooService.<init>(Unknown Source)
while locating com.example.guicetest.MyFooService
1 error
at com.google.inject.internal.InjectorImpl$2.get(InjectorImpl.java:1018)
at com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1044)
at com.example.guicetest.MainActivity$1.onClick(MainActivity.java:41)
我创建的Android Studio项目遵循Gradle依赖项:
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.google.guava:guava:18.0'
compile 'com.android.support:appcompat-v7:22.0.0'
compile('com.google.inject.extensions:guice-multibindings:4.0-beta5') {
exclude group: 'com.google.inject', module: 'guice'
}
compile('org.roboguice:roboguice:3.0.1') {
exclude group: 'com.google.inject', module: 'guice'
}
provided('org.roboguice:roboblender:3.0.1') {
exclude group: 'com.google.inject', module: 'guice'
}
}
该模块如下(MainActivity的一部分):
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(MyFooService.class).in(Singleton.class);
Multibinder<MyInterface> processors = Multibinder.newSetBinder(binder(), MyInterface.class);
processors.addBinding().to(MyBarImpl.class);
processors.addBinding().to(MyQuxImpl.class);
}
});
MyFooService fooService =injector.getInstance(MyFooService.class);
Log.d("TEST", String.valueOf(fooService.implementations.size()));
}
});
如果我调用'gradle -q:app:dependencies',我会收到:
compile - Classpath for compiling the main sources.
+--- com.android.support:appcompat-v7:22.0.0
| \--- com.android.support:support-v4:22.0.0
| \--- com.android.support:support-annotations:22.0.0
+--- com.google.inject:guice:3.0
| \--- javax.inject:javax.inject:1
+--- com.google.inject.extensions:guice-multibindings:3.0
\--- org.roboguice:roboguice:3.0.1
但我仍然有dalvik错误:
com.android.dex.DexException: Multiple dex files define Lcom/google/inject/Module;
当我测试'guice-3.0-no_aop'库时,'com.google.inject.Module'类都在'guice-3.0-no_aop'和'roboguice-3.0.1'中。
我非常困在这里:)