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:
- Also tried without Roboblender and annotation db
RoboGuice.setUseAnnotationDatabases(false);
it didn't make a difference. Ln.d("Test" + Strings.toString(0));
this logs prints out just fine so I think the actual library is packaged right.- 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 aboutCould 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.
I get the same error while using it with gradle and Android Studio
I added the following to build.gradle file:
Added the following to AndroidManifest.xml:
Adding this to the application class will solve the immediate issue. It should also work if added to the default launch activity.
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:
Then in the application manifest, inside the application element add a meta data tag that references the generated class.
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 :)
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