Injected objects became null after upgrading to Roboguice 3

痴心易碎 提交于 2019-12-01 17:41:41

Adding this to the application class will solve the immediate issue. It should also work if added to the default launch activity.

static {
    RoboGuice.setUseAnnotationDatabases(false);
}

The AnnotationDatabaseImpl class is generated by Roboblender at compile time.

Getting the annotations database working:

The compiler argument "guiceAnnotationDatabasePackageName" decides what package the generated AnnoationsDatabaseImpl class is assigned to.

For maven builds:

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven-compiler.version}</version>
                <configuration>
                    <compilerArgument>-AguiceAnnotationDatabasePackageName=some.package.name.here</compilerArgument>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <fork>true</fork>
                </configuration>

Then in the application manifest, inside the application element add a meta data tag that references the generated class.

<meta-data android:name="roboguice.annotations.packages" android:value="some.package.name.here"/>

If you make these changes and are using intellij, then re-importing your Maven pom will apply these changes. Alternatively, in Intellij you can assign a compiler argument to get the annotation to be created.

This would go under Additional command line parameters in Settings/Build,Executions,Deployment/Java Compiler

-AguiceAnnotationDatabasePackageName=some.package.name.here

Hope this helps and saves you some grief :)

I get the same error while using it with gradle and Android Studio

I added the following to build.gradle file:

allprojects {
    gradle.projectsEvaluated {
        tasks.withType(JavaCompile) {
            options.compilerArgs << "-AguiceAnnotationDatabasePackageName=com.android.app.sample"
        }
    }
}

Added the following to AndroidManifest.xml:

<meta-data android:name="roboguice.annotations.packages" android:value="com.android.app.sample"/>

The issue can be resolved by passing in the context properly. If you are extending the application class then Inject a Context using roboguice and then when you are injecting POJOs then pass context in them.

This worked for me.

--Shiv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!