Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 to figure out why.

First of all there's the gradle build file, tried both Proguard on and off and it didn't make a difference. I believe we are currently using Roboguice 3.0.1, but I tried 3.0 and still had the problem.

compile ('org.roboguice:roboguice:3.+') {
    exclude module: 'asm'
}
provided 'org.roboguice:roboblender:3.+

And we do have some custom bindings in a Module file, so here's how I'm specifying it according to the wiki:

<meta-data
  android:name="roboguice.modules"
  android:value="com.some.CustomModule"/>

Just for the record I've also tried to specify it in the Application class like this and it didn't work:

RoboGuice.getOrCreateBaseApplicationInjector(
                    this,
                    RoboGuice.DEFAULT_STAGE,
                    RoboGuice.newDefaultRoboModule(this),
                    new CustomModule(this));

That's about it for the setup, we didn't change anything and if I use Roboguice 2, everything works.

A couple other things that I've also tried:

  1. Also tried without Roboblender and annotation db RoboGuice.setUseAnnotationDatabases(false); it didn't make a difference.
  2. Ln.d("Test" + Strings.toString(0)); this logs prints out just fine so I think the actual library is packaged right.
  3. Instead of injecting a Provider of a POJO, I tried to use manual injection like this RoboGuice.getInjector(this).getInstance(SharedPreferencesHelper.class); and it throws the error about Could not find a suitable constructor in some.path.SharedPreferencesHelper. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private. The weird thing is that in SharedPreferencesHelper class we do have a public constructor with @Inject annotated, I guess somehow it's not taken into consideration? Maybe this whole problem is due to annotation not being considered?

I've been banging my head against it for a couple days now and would really appreciate any input or more stuff to try.

like image 491
victoria0217 Avatar asked Sep 30 '22 06:09

victoria0217


1 Answers

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

like image 129
mjstam Avatar answered Oct 03 '22 02:10

mjstam