Skip to content Skip to sidebar Skip to footer

Injected Objects Became Null After Upgrading To Roboguice 3

I've just upgraded our project to use Roboguice 3 and all of a sudden all the injected objects became null, that includes POJO, Providers, Views, Resources etc. And I'm struggling

Solution 1:

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 :)

Solution 2:

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"/>

Solution 3:

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

Post a Comment for "Injected Objects Became Null After Upgrading To Roboguice 3"