注入对象在升级到Roboguice 3后变为null [英] Injected objects became null after upgrading to Roboguice 3

查看:186
本文介绍了注入对象在升级到Roboguice 3后变为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚升级了我们的项目以使用Roboguice 3,突然间所有注入的对象都变为null,包括POJO,提供商,视图,资源等。我正在努力找出原因。 >

首先有毕业生构建文件,尝试了Proguard开启和关闭,并没有什么不同。我相信我们目前正在使用Roboguice 3.0.1,但是我尝试了3.0,仍然有问题。

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

我们在模块文件中确实有一些自定义绑定,所以这里是如何根据wiki指定的:

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

只是为了记录我也试图在这样的应用程序类中指定它,它没有不要工作:

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

这是关于它的设置,我们没有改变任何东西,如果我使用Roboguice 2,一切



我还试过的其他一些事情:


  1. 也尝试没有Roboblender和注释db RoboGuice.setUseAnnotationDatabases(false); 它没有任何区别。

  2. Ln.d(Test+ Strings.toString(0)); 这个日志打印出来很好,所以我认为实际的库是打包的。

  3. 而不是注入一个POJO的提供者,我试图使用手动注入这样的 RoboGuice.getInjector(this).getInstance(SharedPreferencesHelper.class); ,它会抛出关于的错误在some.path.SharedPreferencesHelper中找不到合适的构造函数。类必须有一个(而且只有一个)使用@Inject注释的构造函数或非私有的零参数构造函数。奇怪的是,在SharedPreferencesHelper类中,我们确实有一个public $ public c $ c> @Inject 注释,我猜不知何故没有考虑到?也许这整个问题是由于注释不被考虑?

我一直在打我的头几天,真的很感激任何输入或更多的东西尝试。

解决方案

我使用gradle和Android Studio时遇到相同的错误



我将以下内容添加到build.gradle文件中:

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

将以下内容添加到AndroidManifest.xml中:

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


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.

解决方案

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

这篇关于注入对象在升级到Roboguice 3后变为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆