Dagger 2 not generating components - android

every time I try to build my project the following error occurs:
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkState(ZLjava/lang/String;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)V
Follows the classes responsible for the dependency injection:
ApplicationModule
#Module
public class ApplicationModule {
private static final String APP_ID = "id";
private static final String APP_SECRET = "secret";
private final Application mApplication;
public ApplicationModule(Application application) {
mApplication = application;
}
#Provides
Application provideApplication() {
return mApplication;
}
#Provides
#Singleton
Client provideClient(Application application) {
return new Client.Builder(APP_ID, APP_SECRET, application).build();
}
}
ApplicationComponent
#Singleton
#Component(modules = ApplicationModule.class)
public interface ApplicationComponent {
void inject(MainApplication application);
Application getApplication();
Client getClient();
}
MainApplication
public class MainApplication extends android.app.Application {
private ApplicationComponent component;
#Inject
Client client;
#Override
public void onCreate() {
super.onCreate();
...
component = DaggerApplicationComponent.builder()
.applicationModule(new ApplicationModule(this))
.build();
component.inject(this);
}
public ApplicationComponent getComponent() {
return component;
}
}
And my gradle.build (Module:app)
buildscript {
repositories {
jcenter()
maven {
url 'http://dl.bintray.com/amulyakhare/maven'
}
}
dependencies {
classpath 'me.tatarka:gradle-retrolambda:3.2.5'
}
}
apply plugin: 'com.android.application'
apply plugin: 'me.tatarka.retrolambda'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "br.mobi.santor.agora"
minSdkVersion 15
targetSdkVersion 25
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
}
}
repositories {
flatDir {
dirs 'libs'
}
}
final SUPPORT_LIBRARY_VERSION = '25.1.0'
final DAGGER_VERSION = '2.8'
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile(name: 'kinvey-android-2.10.6', ext: 'aar')
// AppCompat dependencies
compile "com.android.support:appcompat-v7:$SUPPORT_LIBRARY_VERSION"
compile "com.android.support:cardview-v7:$SUPPORT_LIBRARY_VERSION"
compile "com.android.support:recyclerview-v7:$SUPPORT_LIBRARY_VERSION"
compile "com.android.support:design:$SUPPORT_LIBRARY_VERSION"
// Dagger dependencies
compile "com.google.dagger:dagger:$DAGGER_VERSION"
annotationProcessor "com.google.dagger:dagger-compiler:$DAGGER_VERSION"
// Network dependencies
compile 'com.squareup.picasso:picasso:2.5.2'
// Utils dependencies
compile 'uk.co.chrisjenx:calligraphy:2.2.0'
compile 'joda-time:joda-time:2.9.7'
testCompile 'junit:junit:4.12'
}

The NoSuchMethodError almost certainly means that you have an older version of Guava on the classpath than was used to compile. Make sure that you have the latest version of Guava (almost always safe to use the latest given these compatibility guarnatees) and it should work.

Related

Getting error in DaggerApplicationComponent in MVP

I am trying to learn about MVP pattern and following one tutorial where I got this source code.
ApplicationModule class
#Module
public class ApplicationModule {
private Application application;
public ApplicationModule(Application application) {
this.application = application;
}
#Provides
#Singleton
public Context provideContext() { return application; }
}
ApplicationComponent interface
#Singleton
#Component(modules = {ApplicationModule.class})
public interface ApplicationComponent
{ void inject (MainActivity target); }
App class
public class App extends Application {
private ApplicationComponent component;
#Override
public void onCreate() {
super.onCreate();
//the below line is showing error
component = DaggerApplicationComponent.builder()
.applicationModule(new ApplicationModule(this))
.build();
}
public ApplicationComponent getComponent() {
return component;
}
}
MainActivity
public class MainActivity extends AppCompatActivity
{
#Inject
Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((App) getApplication()).getComponent().inject(this);
}
}
module level build.gradle
android {
compileSdkVersion 28
buildToolsVersion '28.0.2'
defaultConfig {
applicationId "com.renegens.daggerexample"
minSdkVersion 16
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
implementation 'com.google.dagger:dagger:2.12'
annotationProcessor 'com.google.dagger:dagger-compiler:2.12'
compileOnly 'javax.annotation:jsr250-api:1.0'
}
build.gradle for project level
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.0'
}
}
allprojects {
repositories {
jcenter()
google()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
But its showing error in DaggerApplicationComponent in app class. I had to change some settings for importing the project- classpath 'com.android.tools.build:gradle:3.0.0' to 'com.android.tools.build:gradle:3.2.0' also changed minSDK ,buildTool, compileSDK versions and reagrding changes.
Why is it showing the error and how do I fix it? Please help me out or I am stuck.
update
I am getting the error messageFailed to resolve: com.google.dagger:dagger:2.12. in Build tab in toolbar. Below is a screen shot of the message.
use the latest dagger dependencies
implementation 'com.google.dagger:dagger:2.14.1'
annotationProcessor 'com.google.dagger:dagger-compiler:2.14.1'
Apart from this add one more line to ApplicationModule interface ie
Context provideContext();

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.

why does dagger 2 generated components gets deleted on project rebuild?

The problem exists when the code that uses the generated 'Dagger' prefixed on a component is not commented out. If it was commented out, the generated Dagger 2 files are generated.
BookModule
#Module
public class BookModule {
#Provides
public Book providesBook(){
return new Book();
}
}
BookComponent
#Component(modules = BookModule.class)
public interface BookComponent {
void inject(MainActivity activity);
public Book getBook();
}
MainApplication
public class MainApplication extends Application {
public static BookComponent mBookComponent;
#Override
public void onCreate() {
super.onCreate();
mBookComponent = DaggerBookComponent.builder().bookModule(new BookModule()).build();
}
}
Build.gradle(Application)
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.3'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Build.gradle(Project)
apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'
android {
compileSdkVersion 24
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.example.bryan.myapplication"
minSdkVersion 15
targetSdkVersion 24
versionCode 1
versionName "1.0"
jackOptions{
enabled = true
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.2.0'
compile 'com.android.support:support-v4:24.2.0'
compile group: 'com.google.dagger', name: 'dagger', version: '2.10-rc1'
apt group: 'com.google.dagger', name: 'dagger-compiler', version: '2.10-rc1'
compile group: 'javax.annotation', name: 'javax.annotation-api', version: '1.3'
}
Error Stacktrace
ERROR: C:\bin\AndroidProjects\MyApplication\app\src\main\java\com\example\lloyd\myapplication\MainApplication.java:12.26: DaggerBookComponent cannot be resolved
com.android.jack.api.v01.CompilationException: Failed to compile
at com.android.jack.api.v01.impl.Api01ConfigImpl$Api01CompilationTaskImpl.run(Api01ConfigImpl.java:144)
at com.android.builder.core.AndroidBuilder.convertByteCodeUsingJackApis(AndroidBuilder.java:1931)
at com.android.build.gradle.tasks.JackTask.doMinification(JackTask.java:148)
at com.android.build.gradle.tasks.JackTask.access$000(JackTask.java:73)
at com.android.build.gradle.tasks.JackTask$1.run(JackTask.java:112)
at com.android.builder.tasks.Job.runTask(Job.java:51)
at com.android.build.gradle.tasks.SimpleWorkQueue$EmptyThreadContext.runTask(SimpleWorkQueue.java:41)
at com.android.builder.tasks.WorkQueue.run(WorkQueue.java:223)
at java.lang.Thread.run(Thread.java:745)
Caused by: com.android.jack.frontend.FrontendCompilationException: Failed to compile
at com.android.jack.Jack.buildSession(Jack.java:1053)
at com.android.jack.Jack.run(Jack.java:540)
at com.android.jack.api.v01.impl.Api01ConfigImpl$Api01CompilationTaskImpl.run(Api01ConfigImpl.java:124)
... 8 more
mBookComponent = DaggerBookComponent.builder().bookModule(new BookModule()).build();
The code from above is in the MainApplication and it is the problem, if I commented it out, it rebuilds completely fine. But when I left it as it is, it got an error saying the 'DaggerBookComponent' cannot be resolved, by this time the generated DaggerBookComponent is deleted which results in cannot be resolved
When you do a rebuild, the build system looks at the Dagger module and component classes and generates the actual classes that make all of dagger work. When you do a normal incremental build, it doesn't (it assumes the old versions are still good). This means when you rebuild if there's some error in your program or in your dagger components/modules it will not be able to rebuild these files.
So basically, you have a bug somewhere, likely (although not necessarily) in your dagger setup. When you fix it, you'll stop having problems.

Dagger2 do not generate Dagger* classes

Dagger2 does not create classes Dagger*. I created the interface IDaggerTestModule, but the class DaggerIDaggerTestModule not created! All libraries have. Maybe "apt" is not triggered???
my test project: https://github.com/gc986/Dagger2Test
MyConfig:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0-beta2'
classpath 'me.tatarka:gradle-retrolambda:3.3.0-beta4'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'
apply plugin: 'me.tatarka.retrolambda'
android {
compileSdkVersion 24
buildToolsVersion "24.0.1"
defaultConfig {
applicationId "com.netelement.gc986.mpos_android"
minSdkVersion 15
targetSdkVersion 24
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
/*compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_8
}*/
}
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:24.2.0'
testCompile 'junit:junit:4.12'
compile 'io.reactivex:rxjava:1.1.9'
// Retrofit
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'me.tatarka:gradle-retrolambda:3.3.0-beta4'
compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
// Dagger
compile 'com.google.dagger:dagger:2.0'
apt 'com.google.dagger:dagger-compiler:2.0'
provided 'org.glassfish:javax.annotation:10.0-b28'
// Butterknife
compile 'com.jakewharton:butterknife:8.3.0'
apt 'com.jakewharton:butterknife-compiler:8.3.0'
}
User.java
public class User {
private String firstName;
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
DaggerUser.java
#Module
public class DaggerUser {
#Provides
public User provideUser(){
User user = new User();
user.setFirstName("name1");
user.setLastName("name2");
return user;
}
}
IDaggerFactory.java
#Singleton
#Component (
modules = {DaggerUser.class}
)
public interface IDaggerFactory {
void inject();
User user();
}
This not create...
public class DaggerApplication extends Application {
IDaggerFactory component;
public static IDaggerFactory component(Context context){
return ((DaggerApplication) context.getApplicationContext()).component;
}
#Override
public void onCreate(){
super.onCreate();
component = -->DaggerIDaggerFactory<--
.builder()
.daggerUser(new DaggerUser())
.build();
}
}
That's because this method in your component interface doesn't actually do anything. Remove it.
void inject();
EDIT:
Okay, just got home from vacation. So first thing, removing void inject() fixes the following error:
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
java.lang.IllegalArgumentException: not a valid component method: inject()
After that, it says "Cannot find symbol" in DaggerApplication because it hasn't been imported yet, so I added the import
import com.gc986.dagger2test.di.DaggerIDaggerFactory;
And then it compiled successfully
So in reality, you forgot to import the newly generated DaggerIDaggerFactory class in your Java file

What is the reason behind "unresolved reference" when using kotlin for FacebookLogin?

I keep getting a "unresolved reference: FacebookCallback" error when I am trying to implement the code attached in the picture.
I am trying to set up facebook login as instructed in this link: https://developers.facebook.com/docs/facebook-login/android#addbutton
I am new to Kotlin but I can't see what I'm doing wrong here.
EDIT:
Here are my gradle files:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'com.neenbedankt.android-apt'
android {
compileSdkVersion 24
buildToolsVersion "24.0.0"
defaultConfig {
applicationId "yetti.yetti"
minSdkVersion 23
targetSdkVersion 24
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'
androidTest.java.srcDirs += 'src/androidTest/kotlin'
}
dexOptions {
javaMaxHeapSize "2048M"
}
compileOptions.incremental = false
}
kapt {
generateStubs = true
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile 'com.android.support:appcompat-v7:24.0.0'
compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha4'
compile 'com.jakewharton:butterknife:8.2.1'
compile 'com.facebook.android:facebook-android-sdk:4.14.0'
compile 'com.google.firebase:firebase-core:9.2.1'
compile 'com.google.firebase:firebase-database:9.2.1'
compile 'com.google.firebase:firebase-auth:9.2.1'
testCompile 'junit:junit:4.12'
kapt 'com.jakewharton:butterknife-compiler:8.2.1'
}
// Add to the bottom of the file
apply plugin: 'com.google.gms.google-services'
and:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.0.3'
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0-alpha6'
classpath 'com.google.gms:google-services:3.0.0'
// the latest version of the android-apt plugin
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
futhermore, here is the equivalent code I am trying to execute but written in plain Java (which works just fine):
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.login.LoginResult;
public class Actv extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_actv);
FacebookCallback<LoginResult> facebookCallback = new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException error) {
}
};
}
}
A proper way to anonymously implement FacebookCallback in Kotlin is as follows:
val facebookCallback = object : FacebookCallback<LoginResult> {
override fun onSuccess(loginResult: LoginResult) {
}
override fun onCancel() {
}
override fun onError(error: FacebookException) {
}
}
Object Expressions and Declarations documentation provides all necesary details.

Categories

Resources