2016-03-28 50 views
0

我有一个多模块项目,其中有一个从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版本。

有什么想法?

回答

0

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

+0

'Roboguice.setUseAnnotationDatabases(false)'是一个快速修复,因为它从编译时间切换到运行时反射,使您的应用程序变慢很多 –

+0

我知道,但是当我尝试时,这是很久以前,让RoboBlender工作。现在我用4.0。 – Christine

+0

是啊,我也没有,我仍然有4.0的AnnotationDatabaseImpl问题。但是我发现'apply plugin:'com.neenbedankt.android-apt''是罪魁祸首。 –