我有一个多模块的项目,其中有一个类派生自RoboActivity的一些注入pojo字段。
这些字段的类也有注入字段,而这些字段又有注入字段。其中一些类位于不同的模块中,所以我需要的是跨模块注入。
我按照Robo Blenders wiki中的说明操作(应该是这样的),但当我尝试运行这个应用程序时,得到的提示是'ClassNotFoundException: AnnotationDatabaseImpl‘,然后应用程序就停止了。
以下是类(未显示的接口):
@ContentView(R.layout.activity_about)
public class AboutActivityImpl extends AbstractActivityImpl implements AbstractActivity, AboutActivity {
@InjectView(R.id.app_name) private TextView app_name;
@InjectView(R.id.app_copyright) private TextView app_copyright;
@InjectView(R.id.app_version) private TextView app_version;
// MVP
@Inject
AboutPresenter presenter; //IS INJECTED
// MVP
@Override
protected MvpEvent setPresenter() {
setPresenter(presenter);
return new AboutActivityInitEvent();
}
.
.
.
}
public class AboutPresenterImpl extends AbstractPresenterImpl implements AboutPresenter {
@Inject
AppInfo appInfo; //IS INJECTED
@SuppressWarnings("unused")
@Inject @Config(Property.APP_COPYRIGHT) //IS INJECTED
private String appCopyright;
.
.
.
}
public class AppInfoImpl implements AppInfo {
@Inject
AppInfoApi appInfoApi; //IS NOT INJECTED!!
public AppInfoImpl() {
}
.
.
.
}
public class AppInfoApiImpl implements AppInfoApi {
@Inject
Application application; //IS NOT INJECTED!!
public AppInfoApiImpl() {
}
.
.
.
}Roboguice模块如下:
public class AppModule extends AbstractModule {
@Override
protected void configure() {
bind(AboutPresenter.class).to(AboutPresenterImpl.class);
}
}
public class DomainModule extends AbstractModule {
@Override
protected void configure() {
bind(AppInfo.class).to(AppInfoImpl.class);
}
}
public class PlatformModule extends AbstractModule {
@Override
protected void configure() {
bind(AppInfoApi.class).to(AppInfoApiImpl.class);
}
}清单如下:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ar.com.abimobileapps.androidapp">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data android:name="roboguice.modules"
android:value="ar.com.abimobileapps.platform.config.PlatformModule,
ar.com.abimobileapps.androidapp.configuration.ConfigurationModule,
ar.com.abimobileapps.androidapp.persistence.config.PersistenceModule,
ar.com.abimobileapps.androidapp.domain.config.DomainModule,
ar.com.abimobileapps.androidapp.config.AppModule" />
<meta-data android:name="roboguice.annotations.packages"
android:value="ar.com.abimobileapps.androidapp,
ar.com.abimobileapps.androidapp.domain,
ar.com.abimobileapps.androidapp.persistence,
ar.com.abimobileapps.platform" />
<activity
android:name=".ui.AboutActivityImpl">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
.
.
.
</application>
</manifest>以下是模块build.gradle文件(代码片段):
build.gradle(app模块):
dependencies {
.
.
.
// Roboguice
compile 'org.roboguice:roboguice:3.0.1'
provided 'org.roboguice:roboblender:3.0.1'
// Modules
compile project(':domain')
compile project(':platform')
compile project(':configuration')
}
project.tasks.withType(JavaCompile) { task ->
options.compilerArgs << "-AguiceAnnotationDatabasePackageName=ar.com.abimobileapps.androidapp"
}build.gradle (域模块):
dependencies {
.
.
.
// Roboguice
compile 'org.roboguice:roboguice:3.0.1'
provided 'org.roboguice:roboblender:3.0.1'
// Modules
compile project(':persistence')
compile project(':platform')
}
project.tasks.withType(JavaCompile) { task ->
options.compilerArgs << "-AguiceAnnotationDatabasePackageName=ar.com.abimobileapps.androidapp.domain"
}build.gradle (持久化模块):
dependencies {
.
.
.
// Roboguice
compile 'org.roboguice:roboguice:3.0.1'
provided 'org.roboguice:roboblender:3.0.1'
}
project.tasks.withType(JavaCompile) { task ->
options.compilerArgs << "-AguiceAnnotationDatabasePackageName=ar.com.abimobileapps.androidapp.persistence"
}build.gradle (平台模块):
dependencies {
.
.
.
// Roboguice
compile 'org.roboguice:roboguice:3.0.1'
provided 'org.roboguice:roboblender:3.0.1'
}
project.tasks.withType(JavaCompile) { task ->
options.compilerArgs << "-AguiceAnnotationDatabasePackageName=ar.com.abimobileapps.platform"
}为包ar.com.abimobileapps.androidapp、ar.com.abimobileapps.androidapp.domain、ar.com.abimobileapps.androidapp.persistence和ar.com.abimobileapps.platform生成AnnotationDatabaseImpl.class文件。检查它的源代码,我发现所有的注入和'to inject‘类都被引用了。
所以我不明白为什么在运行应用程序的时候会出现'ClassNotFoundException: AnnotationDatabaseImpl‘。
正确注入的类(AboutActivityImpl、AboutPresenterImpl和AppModule)在同一个项目模块中,而另外两个类(失败的类)在两个不同的项目模块中(一个模块中的AppInfo/DomainModule,另一个模块中的AppInfoApi/PlatformModule )。
Roboguice的版本是3.0.1。
有什么想法吗?
发布于 2016-04-22 03:33:02
您可以设置Roboguice.setUseAnnotationDatabases(false)来防止异常。另外,我建议你迁移到Roboguice 4.0。你可以在Github上的Roboguice项目中找到Roboguice4.0作为分支rg-4-final。RG4.0更轻量级,因此它有更好的性能。
https://stackoverflow.com/questions/36255832
复制相似问题