Injected objects became null after upgrading to Ro

2019-02-22 10:55发布

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.

3条回答
家丑人穷心不美
2楼-- · 2019-02-22 11:13

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"/>
查看更多
别忘想泡老子
3楼-- · 2019-02-22 11:17

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

查看更多
该账号已被封号
4楼-- · 2019-02-22 11:22

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

查看更多
登录 后发表回答