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

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

问题描述

我刚刚升级了我们的项目中使用Roboguice 3和突然全部注入对象变成了空,包括POJO,供应商,视图,资源等,而我努力找出原因。

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.

首先还有的摇篮构建文件,都试过Proguard的开启和关闭,并没有发挥作用。我相信,我们目前正在使用Roboguice 3.0.1,但我想3.0,仍然出现了问题。

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

这就是它的设置中,我们没有改变任何东西,如果我用Roboguice 2,一切正常。

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. 也试过没有Roboblender和注释分贝 RoboGuice.setUseAnnotationDatabases(假); 并没有发挥作用
  2. Ln.d(测试+ Strings.toString(0)); 这会将打印出来就好了,所以我觉得实际的库打包权
  3. 相反注入POJO的提供者,我试图用手动进样像这样 RoboGuice.getInjector(本).getInstance(共享preferencesHelper.class); 和它抛出约找不到合适的构造函数some.path.Shared preferencesHelper。类必须有一个(且只有一个)构造标注@Inject或零参数的构造函数,是不是私人的。奇怪的是,在共享preferencesHelper类我们有一个公开构造与 @Inject 注释的,我想在某种程度上它没有考虑到?也许这整个问题是由于标注不被考虑?
  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?

我一直在敲打我的头反对了几天现在真的AP preciate任何输入或更多的东西去尝试。

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

推荐答案

将其加入应用程序类将解决眼前的问题。如果添加到默认启动的活动也应该工作。

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);
}

该AnnotationDatabaseImpl类是由Roboblender在编译时生成的。

The AnnotationDatabaseImpl class is generated by Roboblender at compile time.

获取标注数据库的工作:

Getting the annotations database working:

编译器参数guiceAnnotationDatabasePackageName决定什么打包生成的AnnoationsDatabaseImpl类被分配给。

The compiler argument "guiceAnnotationDatabasePackageName" decides what package the generated AnnoationsDatabaseImpl class is assigned to.

有关的Maven构建:

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

如果你做这些改变和正在使用的IntelliJ,然后重新导入你的Maven POM将应用这些更改。另外,在的IntelliJ您可以指定一个编译器参数去创建的注释。

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.

这会在其他命令行参数去设置/建造,处决,部署/ Java编译器

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

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

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