Dagger2 do not generate Dagger* classes - android

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

Related

unable to identify statement 'import com.google.android.maps.MapActivity; '

I have recently cloned an already running code in android studio. I am stuck in this error for couple of days now, I am using a class AbstractMapAcivity which extends MapActivity class from "com.google.android.maps". Android studio gives the below error:
error: package com.google.android.maps does not exist
please help android/android studio experts, I am new to android development. Have a look at the supporting classes and project settings
build. gradle : app
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
repositories {
maven { url 'https://maven.fabric.io/public' }
}
android {
compileSdkVersion "Google Inc.:Google APIs:23"
buildToolsVersion '23.0.3'
useLibrary 'org.apache.http.legacy'
defaultConfig {
applicationId "com.nccatalog"
minSdkVersion 14
targetSdkVersion 23
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
packagingOptions {
exclude 'LICENSE.txt'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/DEPENDENCIES'
}
}
dependencies {
compile "com.android.support:support-v4:${supportLibVersion}"
compile "com.android.support:appcompat-v7:${supportLibVersion}"
compile "com.android.support:mediarouter-v7:${supportLibVersion}"
compile "com.google.android.gms:play-services-gcm:${gcmLibVersion}"
compile "com.google.code.gson:gson:${gsonLibVersion}"
compile files('libs/FlurryAnalytics-4.2.0.jar')
compile files('libs/gcm.jar')
compile files('libs/com.radaee.view.jar')
compile files('libs/httpcore-4.3.jar')
compile files('libs/org.apache.commons.io.jar')
compile files('libs/universal-image-loader-1.9.3-SNAPSHOT.jar')
compile('com.crashlytics.sdk.android:crashlytics:2.5.5#aar') {
transitive = true;
}
}
build.gradle : project
buildscript {
repositories {
google()
jcenter()
maven { url 'https://maven.fabric.io/public' }
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.0'
classpath 'io.fabric.tools:gradle:1.30.0'
classpath 'com.google.gms:google-services:4.2.0'
}
}
allprojects {
repositories {
google()
jcenter()
}
}
ext {
supportLibVersion = '23.4.0'
gcmLibVersion = '8.3.0'
gsonLibVersion = '2.7'
}
gradle-wrapper.properties
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
AbstractMapActivity.java
package com.utils.ui;
import android.app.Activity;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import com.google.android.maps.MapActivity;
import com.utils.application.ApplicationController;
public class AbstractMapAcivity extends MapActivity implements ActivityHelper {
private ActivityHelper ah = new ActivityHelperImpl(this);
#Override
protected void onCreate(Bundle savedInstanceState) {
ApplicationController.activityStack.add(this);
super.onCreate(savedInstanceState);
}
#Override
protected void onDestroy() {
ApplicationController.activityStack.remove(this);
super.onDestroy();
}
#Override
public void hideKeyboard(View view) {
ah.hideKeyboard(view);
}
#Override
public Typeface createTypeFace(String fontName) {
return ah.createTypeFace(fontName);
}
#Override
public Drawable createRepeatableDrawable(int imageId) {
return ah.createRepeatableDrawable(imageId);
}
#Override
public boolean isNetworkAvailable(Activity activity) {
return ah.isNetworkAvailable(activity);
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
#Override
public void switchToActivity(Activity current,
Class<? extends Activity> otherActivityClass, Bundle extras) {
ah.switchToActivity(current, otherActivityClass, extras);
}
#Override
public void goToActivity(Activity current,
Class<? extends Activity> otherActivityClass, Bundle extras) {
ah.goToActivity(current, otherActivityClass, extras);
}
#Override
public void initUI() {
}
}
In your build.gradle (app)
dependencies {
//Your other compile dependencies (replace compile with implementation on them)
implementation 'com.google.android.gms:play-services-maps:17.0.1'
}
Seems like you are at least missing this line which should import maps, maybe other things are also missing that I can't see right now.
Take a look at this guide if you haven't already:
https://developers.google.com/maps/documentation/android-sdk/start
There is also a guide for existing projects (I assume you need information from both of them)
https://developers.google.com/maps/documentation/android-sdk/config
Open the Gradle you see in the picture.
Once it is open you will find the next image-like page, in dependencies add your dependency. And sync your file as well.

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.

Dagger 2 not generating components

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.

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