我有一個多模塊項目,其中有一個從RoboActivity派生類與一些注入pojo字段。Roboguice拋出ClassNotFoundException:AnnotationDatabaseImpl
這些字段的類也有注入的字段,反過來也注入字段。其中一些類生活在不同的模塊中,所以我需要的是跨模塊注入。
我遵循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(應用模塊):
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文件。檢查它的源代碼,我看到所有注入和「注入」類都被引用。
所以我不明白爲什麼我運行應用程序時得到'ClassNotFoundException:AnnotationDatabaseImpl'。
正確注入的類(AboutActivityImpl,AboutPresenterImpl和AppModule)位於同一個項目模塊中,而其他兩個類(失敗的類)位於兩個不同的項目模塊中(一個模塊中的AppInfo/DomainModule,AppInfoApi/PlatformModule在其他模塊中)。
Roboguice是3.0.1版本。
有什麼想法?
'Roboguice.setUseAnnotationDatabases(false)'是一個快速修復,因爲它從編譯時間切換到運行時反射,使您的應用程序變慢很多 –
我知道,但是當我嘗試時,這是很久以前,讓RoboBlender工作。現在我用4.0。 – Christine
是啊,我也沒有,我仍然有4.0的AnnotationDatabaseImpl問題。但是我發現'apply plugin:'com.neenbedankt.android-apt''是罪魁禍首。 –