Gradle project is not created when adding RoboBlender
I added the RoboGuice 3 dependency to my gradle build file, which it compiles and runs, however the app crashes due to NoClassDefFoundError: AnnotationDatabaseImpl. Was there any research that suggested that RoboBlender is needed to create the definition (I'm familiar with RoboGuice 2, which doesn't require RoboBlender), but when I add RoboBlender, the project is no longer built.
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.android.support:cardview-v7:21.0.+'
compile 'com.android.support:recyclerview-v7:21.0.+'
compile 'com.koushikdutta.urlimageviewhelper:urlimageviewhelper:1.0.4'
compile 'de.hdodenhof:circleimageview:1.2.1'
compile 'com.getbase:floatingactionbutton:1.4.0'
compile 'de.hdodenhof:circleimageview:1.2.1'
compile 'org.twitter4j:twitter4j-core:4.0.2'
compile files('libs/json-me.jar')
compile files('libs/twitter_api_me-1.9.jar')
compile('ch.acra:acra:4.5.0') {
exclude group: 'org.json'
}
compile 'org.roboguice:roboguice:3.0.1'
provided 'org.roboguice:roboblender:3.0.1'
}
Build error:
Error:Execution failed for task ':app:compileDebugJava'.
java.lang.ClassCastException: com.sun.tools.javac.code.Type cannot be passed to javax.lang.model.type.DeclaredType l>
Gradle Dependency Cache can get corrupted (this sometimes happens after network connection timeout). Reload dependencies and sync project (requires network). The build process state (daemon) may be corrupted. Stopping all gradle daemons can fix this problem. Stop gradle build processes (restart required) In case of corrupt gradle processes, you can also try closing the IDE and then killing all Java processes.
What is causing this and how can I fix it?
source to share
This error can be caused by misuse @Inject
, as in the following example:
public class Foo {
@Inject
public Foo(Context context, int code) {
//this won't work because of the primitive in the constructor
//and the @Inject annotation are being used together
}
}
RoboBlender will not be able to create a database due to the inability to use a primitive as a declared type.
Hence your error message
java.lang.ClassCastException: com.sun.tools.javac.code.Type cannot be cast to javax.lang.model.type.DeclaredType
means that the primitive (com.sun.tools.javac.code.Type)
cannot be converted to a reference typejavax.lang.model.type.DeclaredType
Instead, you need to write the provider:
public class FooProvider implements Provider<Foo> {
Context context;
private static int CODE = 1;
@Inject
public FooProvider(Context context) {
this.context = context;
}
@Override
public Foo get() {
return new Foo(context, CODE);
}
}
and bind Foo
to this provider in the module
binder.bind(Foo.class).toProvider(FooProvider.class);
and remove @Inject
from constructor Foo
.
I suggest you go through your object graph and look for annotations @Inject
for constructors with primitives in them. Remove annotations and specify vendors for them as above. RoboBlender will build correctly AnnotationsDatabaseImpl
and your project will compile.
source to share
I had the same problem, and in my case, having a class with 2 constructors:
@Inject
public PaymentSelectionDialog(Context context) {
this.context = context;
}
@Inject
public PaymentSelectionDialog(Context context, PaymentSelectable paymentSelectable) {
this.context = context;
this.paymentSelectable = paymentSelectable;
I had no problem using the first constructor, but when I instantiated an object using the second constructor, I have this problem. So the problem is that Roboguice is trying to inject an object that implements the PaymentSelectable interface, but that object is not defined in any module.
You may be using a constructor with a link that you do not define in any of your modules.
Hope it helps!
source to share