Roboguice抛出ClassNotFoundException:AnnotationDatabaseImpl

时间:2016-03-28 04:07:51

标签: java android dependency-injection roboguice

我有一个多模块项目,它有一个派生自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 module):

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文件。检查其源代码我看到所有注入和注入&#39;引用了类。

所以我无法理解为什么我会得到ClassNotFoundException:AnnotationDatabaseImpl&#39;在运行应用程序时。

正确注入的类(AboutActivityImpl,AboutPresenterImpl和AppModule)位于同一个项目模块中,而其他2个类(失败的类)位于两个不同的项目模块中(AppInfo / DomainModule在一个模块中,AppInfoApi / PlatformModule in其他模块)。

Roboguice是版本3.0.1。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您可以设置Roboguice.setUseAnnotationDatabases(false)以防止异常。另外,我建议你转到Roboguice 4.0。您可以在Github上的Roboguice项目中找到Roboguice 4.0作为分支rg-4-final。 Rg 4.0更轻巧,因此它具有更好的性能。