Dagger 2 : compilation fail sometimes - android

after many hours getting headaches... i'm resigned to ask you guys ^^
got two simple Dagger 2 components but both of them doesn't compile. But sometimes (i don't understand why) it does ...
this is really annoying.
So i think i should miss something, here is the error :
Error:(18, 17) error: ....InstanceContextService cannot be provided without an #Provides- or #Produces-annotated method.
home.HomeActivity.mInstanceContextService
[injected field of type: ....services.InstanceContextService mInstanceContextService]
So my compoments is like this :
#Component(modules = {InstanceContextModule.class})
#Singleton
public interface InstanceContextComponent {
#Singleton
void inject(HomeActivity activity);
}
My module :
#Module
public class InstanceContextModule {
#Provides
#Singleton
public InstanceContextService provideInstanceContext() {
return new FileInstanceService();
}
}
and my config in gradle is
apply plugin: 'com.android.application'
apply plugin: 'me.tatarka.retrolambda'
apply plugin: 'com.neenbedankt.android-apt'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
// compileOptions.incremental = false
defaultConfig {
applicationId "com.kayentis.ediary"
minSdkVersion 17
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
sourceSets {
main {
res.srcDirs = [
'src/main/res',
'src/main/res-main',
'src/main/res-message',
'src/main/res-account'
]
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile 'com.android.support:support-v13:23.1.+'
compile 'com.google.code.gson:gson:2.1'
//compile 'com.google.protobuf:protobuf-java:2.6.1'
//Dagger 2
// apt 'com.google.dagger:dagger-compiler:2.3'
// compile 'com.google.dagger:dagger:2.3'
apt 'com.google.dagger:dagger-compiler:2.0.2'
compile 'com.google.dagger:dagger:2.0.2'
provided 'org.glassfish:javax.annotation:10.0-b28'
//Retrofit 2.0
compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4'
//RxJava
compile 'io.reactivex:rxjava:1.1.0'
compile 'io.reactivex:rxandroid:1.1.0'
//ButterKnife
// compile 'com.jakewharton:butterknife:7.0.1'
}
I'm looking everywhere but i think i miss something...
Thanks in advance ^^

Found the Solution.
Had two component with #singleton scoped modules, so this can't work. Now it's building successfull.

Related

Incorrectly typed data found for annotation element public abstract int[] butterknife.OnClick.value()

I was following an Android tutorial; here is my previous error I faced while following the tutorial.
As I reached almost the end of the tutorial, I got the following error:
Caused by: java.lang.annotation.AnnotationTypeMismatchException: Incorrectly typed data found for annotation element public abstract int[] butterknife.OnClick.value() (Found data of type int[])
I don't know what I say more about it, so I will paste the app Gradle file here:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
android.applicationVariants.all { variant ->
variant.outputs.all { output ->
def fileName = "${project.name}_${output.baseName}-${variant.versionName}.apk"
outputFileName = new File(output.outputFile.parent, fileName).getName()
}
}
lintOptions {
abortOnError false
}
defaultConfig {
applicationId "com.tomsapp.mulberry"
minSdkVersion 17
targetSdkVersion 23
versionCode 1
versionName "1.0.0"
javaCompileOptions {
annotationProcessorOptions {
includeCompileClasspath true
}
}
}
buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
repositories {
mavenCentral()
jcenter()
maven { url "https://jitpack.io" }
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation project(path: ':licensing')
implementation('com.github.afollestad.material-dialogs:core:0.8.5.3#aar') {
transitive = true
}
implementation('com.github.afollestad:drag-select-recyclerview:0.3.1#aar') {
transitive = true
}
implementation 'com.github.afollestad:bridge:3.1.1'
implementation 'com.github.afollestad:sectioned-recyclerview:0.2.0'
implementation 'com.github.afollestad:assent:0.2.0'
implementation 'com.github.afollestad:inquiry:2.0.1'
//noinspection GradleCompatible
implementation 'com.android.support:appcompat-v7:23.1.1'
implementation 'com.android.support:recyclerview-v7:23.1.1'
implementation 'com.android.support:support-v4:23.1.1'
implementation 'com.android.support:design:23.1.1'
implementation 'com.android.support:cardview-v7:23.1.1'
implementation 'com.android.support:palette-v7:23.1.1'
implementation 'com.github.bumptech.glide:glide:3.7.0'
implementation 'com.github.florent37:glidepalette:1.0.6#aar'
implementation 'com.jakewharton:butterknife:7.0.1'
implementation 'com.google.android.apps.muzei:muzei-api:2.0'
implementation 'com.makeramen:roundedimageview:2.2.1'
}
I referred this link for a solution, but the mentioned solutions in it created new errors in my code - ButterKnife 8.0.1 not working

android cannot find symbol class RequiresApi after removing ACRA

I need to remove ACRA for removing any vulnerabilities.
I had removed all imports/compiles in app.gradle and MyApplication.java. However, when I tried to build the project, the IDE gives below error:
error: cannot find symbol class RequiresApi
These are my app.gradle and MyApplication.java
app.gradle (I could not build after I removed compile 'ch.acra:acra:4.9.2' in dependencies)
buildscript {
(omitted)
}
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion '27.0.3'
defaultConfig {
applicationId "******"
minSdkVersion 17
targetSdkVersion 22
versionCode 6
versionName "1.0.3"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
lintOptions {
disable 'RestrictedApi'
}
}
dependencies {
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:22.2.1'
compile 'com.android.support:design:22.2.1'
compile 'com.android.support:recyclerview-v7:22.2.1'
compile 'com.journeyapps:zxing-android-embedded:3.0.1#aar'
compile 'com.google.zxing:core:3.2.0'
compile 'com.jakewharton:butterknife:8.0.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.0.1'
compile 'net.zetetic:android-database-sqlcipher:3.5.7#aar'
compile 'com.google.code.gson:gson:2.8.2'
//compile 'ch.acra:acra:4.9.2' <- REMOVED ACRA
compile "io.reactivex.rxjava2:rxjava:2.1.9"
compile "io.reactivex.rxjava2:rxandroid:2.0.2"
}
MyApplication.java
package ******;
import android.app.Application;
public class MyApplication extends Application {
private static MyApplication myApplication;
public static MyApplication getInstance() {
return myApplication;
}
public void onCreate() {
super.onCreate();
myApplication = this;
}
}
I had also tried cleaning my project and rebuild, but did not work. When I put ACRA back, the project builds as usual. Is it the problem of IDE?
#RequiresApi is part of com.android.support:support-annotations. You have to add that as a dependency
e.g.
dependencies {
com.android.support:support-annotations:27.1.1
}

"cannot find symbol method metafactory" using Lambda

I'm using java 8 and lambda expressions. When I use lambda expressions with OnClickListeners everything is fine, but when I use that in this animate method:
public void configureFragmentToolbar(String title, boolean displayHomeAsUpEnabled) {
//..
this.toolbar.animate().translationY(-50).setDuration(300).withEndAction(() -> { //ERROR
toolbar.animate().translationY(0).setDuration(300);
});
}
I´m getting this error:
"error: cannot find symbol method
metafactory(Lookup,String,MethodType,MethodType,MethodHandle,MethodType)"
gradle:
apply plugin: 'com.android.library'
apply plugin: 'com.neenbedankt.android-apt'
apply plugin: 'org.sonarqube'
def junitVersion = '4.12'
def mockitoVersion = '1.10.19'
def daggerVersion = '2.5'
android {
compileSdkVersion 27
buildToolsVersion "27.0.3"
lintOptions {
abortOnError false
}
defaultConfig {
minSdkVersion 14
targetSdkVersion 27
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
targetCompatibility 1.8
sourceCompatibility 1.8
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':commons')
compile 'com.android.support:appcompat-v7:27.0.2'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:design:27.0.2'
testCompile "junit:junit:$junitVersion"
testCompile "org.mockito:mockito-core:$mockitoVersion"
apt "com.google.dagger:dagger-compiler:$daggerVersion"
}
Can you tell me why I'm getting that error? Thank you
In $ANDROID_SDK/build-tools/27.0.3/, missing the file : core-lambda-stubs.jar. You just need reinstall the build tool.
Try to downgrade android build tools to:
classpath 'com.android.tools.build:gradle:3.1.4'

Dagger classes not being generated

I have been trying to include Dagger latest version dagger-android:2.11 in my app. But, Daggerclaases like DaggerAppComponent is not being generated.
following is my app level gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "net.maskery.android.lab.lawyerapp"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
configurations.all {
resolutionStrategy.force 'com.google.code.findbugs:jsr305:1.3.9'
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:design:25.3.1'
testCompile 'junit:junit:4.12'
compile 'com.google.dagger:dagger-android:2.11'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.11'
}
my component interface
#Component(modules = {AppModule.class})
public interface AppComponent {
void inject(MainActivity mainActivity);
}
my module class
#Module
public class AppModule {
private LawyerApp app;
public AppModule(LawyerApp app) {
this.app = app;
}
#Provides public Context providesContext(){
return app;
}
#Provides public Resources providesResources(){
return app.getResources();
}
}
the app is properly building but when am trying to create AppComponent object, DaggerAppComponent is just not availbale.
I have enabled annotation processing in the settings and created this app only after that.
I tried 'com.neenbedankt.android-apt' plugin too,. This also didnt work for me.
This is like hit-end for me over any options to try here. Please help.

Dagger2 do not generate Dagger classes

Dagger2 does not create classes Dagger*. I created the interface MyContainerComponent, but the class DaggerMyContainerComponent not created!
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
classpath "com.android.databinding:dataBinder:1.0-rc4"
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
classpath 'com.frogermcs.androiddevmetrics:androiddevmetrics-plugin:0.4'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.0.6"
}
}
allprojects {
repositories {
jcenter()
mavenCentral()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
in build.gradle
apply plugin: 'com.android.application'
apply plugin: 'android-apt'
apply plugin: 'com.android.databinding'
apply plugin: 'kotlin-android'
apply plugin: 'com.frogermcs.androiddevmetrics'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.tegra.module.profile"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:25.1.1'
testCompile 'junit:junit:4.12'
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
kapt 'com.android.databinding:compiler:1.0-rc4'
//dagger
compile 'com.google.dagger:dagger:2.8'
apt 'com.google.dagger:dagger-compiler:2.8'
provided 'javax.annotation:jsr250-api:1.0'
//glide
compile 'com.github.bumptech.glide:glide:3.7.0'
//rxAndroid
compile 'io.reactivex:rxandroid:1.2.1'
compile 'io.reactivex:rxjava:1.1.6'
//autoFactory
compile group: 'com.google.auto.factory', name: 'auto-factory', version: '1.0-beta3'
//retrofit
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile group: 'com.squareup.retrofit2', name: 'adapter-rxjava', version: '2.1.0'
//OkHttp
compile 'com.squareup.okhttp3:logging-interceptor:3.3.1'
//timber
compile 'com.jakewharton.timber:timber:4.5.1'
//stetho
compile 'com.facebook.stetho:stetho:1.4.2'
compile 'com.facebook.stetho:stetho-okhttp3:1.4.2'
//constraint-layout
compile 'com.android.support.constraint:constraint-layout:1.0.0-beta4'
}
kapt {
generateStubs = true
}
MyContainerComponent.kt
package com.tegra.module.profile.ioc
#Component(modules = arrayOf(ProfileModule::class))
#Singleton
public interface MyContainerComponent {
fun inject(profileActivity: ProfileActivity)
}
ProfileModule.kt
package com.tegra.module.profile.bl.di
#Module
class ProfileModule(val application: Application) {
#Provides
#Singleton
fun provideApplication(): Application {
return application
}
#Provides
#Singleton
fun provideContext(): Context {
return application
}
}
Any solutions? Already tried to: change gradle version, Clean, Rebuild, Change dependecy versions, also deleting the Gradle cache and reinstalling Android Studio.
In order to use Dagger annotations in Kotlin files you have to use kapt for Dagger compiler:
kapt 'com.google.dagger:dagger-compiler:2.8'
Also, delete all apt dependencies because kapt handles annotations in Java too.
Official docs

Categories

Resources