【问题标题】:Roboguice throws ClassNotFoundException: AnnotationDatabaseImplRoboguice 抛出 ClassNotFoundException: AnnotationDatabaseImpl
【发布时间】:2016-07-15 07:52:31
【问题描述】:

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

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 文件。检查它的源代码,我看到所有注入和“注入”类都被引用。

所以我不明白为什么在运行应用程序时会收到“ClassNotFoundException: AnnotationDatabaseImpl”。

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

Roboguice 是 3.0.1 版。

有什么想法吗?

【问题讨论】:

    标签: java android dependency-injection roboguice


    【解决方案1】:

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

    【讨论】:

    • Roboguice.setUseAnnotationDatabases(false) 是一个快速修复,因为它会从编译时切换到运行时反射,从而大大降低您的应用程序速度
    • 我知道,但是当我尝试时,这是很久以前的事了,我无法让 RoboBlender 工作。现在我使用 4.0。
    • 是的,我也没有,我仍然遇到 4.0 的 AnnotationDatabaseImpl 问题。但我发现apply plugin: 'com.neenbedankt.android-apt' 是罪魁祸首。
    • 4.0 要快很多,所以我不介意快速修复。但如果有人让它工作,请在此处发布。
    • 我还没有找到这方面的基准,你知道快多少吗?谢谢
    猜你喜欢
    • 1970-01-01
    • 2016-11-29
    • 1970-01-01
    • 2013-05-27
    • 2017-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多