I am trying to import Dagger 2 into a brand new project on Android Studio and having a look at the various guides and documentation, I am unable to use DaggerAppComponent
My Gradle settings are as follows:
build (Project)
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
build (Module: app)
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "com.raywenderlich.todolist"
minSdkVersion 19
targetSdkVersion 25
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:design:25.3.1'
//Dagger 2
compile 'com.google.dagger:dagger:2.11'
compile 'com.google.dagger:dagger-android:2.11'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.11'
annotationProcessor 'com.google.dagger:dagger-compiler:2.11'
}
The in my TodolistApplication.java class: I have this:
#Override
public void onCreate() {
super.onCreate();
DaggerAppComponent
.builder()
.application(this)
.build()
.inject(this);
}
However Android Studio shows the following error:
Error:(21, 5) error: cannot find symbol variable DaggerAppComponent
I have tried rebuilding the project and importing various dagger files, however nothing seems to have worked.
Error:(21, 5) error: cannot find symbol variable DaggerAppComponent
The above error is not a dependency error.
AppComponent must be first created before using it like below
#Module public class ApplicationModule {
//all dependency provides here
}
#Component(modules = ApplicationModule.class)
public interface ApplicationComponent {
}
Check this reference for complete understanding https://medium.com/#isoron/a-friendly-introduction-to-dagger-2-part-1-dbdf2f3fb17b
Related
I could not able to run the local unit tests in Module which is using android Data binding library.
First let me tell about the project structure how its configured.
project
| app
-MainLauncherActivity
| myLibrary
-CommonModuleActivity
I have created a new project, after that have added a new module "myLibrary".
the main "app" depends on "myLibrary" module. I have added one activity in "myLibrary" it supports databinding. I called module specific activity from main "app" activity on button click in it. it just works, could able to run the app.
But, Getting the below errors when i add a test case for the Module Activity.
AndroidStudio : 2.3
Gradle build tools version 2.3.0 -->
Error:java.lang.NoClassDefFoundError: android/databinding/DataBinderMapper
Gradle build tools version 2.2.3 -->
Error:java.lang.NoClassDefFoundError: android/databinding/ViewDataBinding
PROJECT IDE SCREENSHOT
project root gradle file:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
//classpath 'com.android.tools.build:gradle:2.3.0'
classpath 'com.android.tools.build:gradle:2.2.3'
// 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
}
below is "app" build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.example.bindingtest"
minSdkVersion 21
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'
}
}
dataBinding {
enabled true
}
}
dependencies {
compile 'com.android.support:appcompat-v7:25.1.0'
//compile 'com.android.support.constraint:constraint-layout:1.0.0-beta4'
testCompile 'junit:junit:4.12'
compile project(':mylibrary')
}
below myLibrary build.gradle:
apply plugin: 'com.android.library'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
minSdkVersion 21
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'
}
}
dataBinding {
enabled 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 'com.android.support:appcompat-v7:25.1.0'
testCompile 'junit:junit:4.12'
testCompile "org.mockito:mockito-core:1.10.19"
}
LibraryActivity:
public class MyLibraryActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMyLibraryBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_my_library);
//set data to binding
}
}
Corresponding test case can be found in the attached screenshot.
Could some one tell me what am i doing wrong here to get it tested.
APP IS WORKING FINE, ONLY UNIT-TESTS ARE FAILING!!!
I know it's too late to answer that question , But I say to friends who may face this problem.
Any time You used to dataBinding and at unit test faced with NoClassDefFoundError
first add this lines in build.gradle(Module)
testOptions {
unitTests{
isIncludeAndroidResources=true
}
}
then add this library to the dependencies at build.gradle(Module)
kaptTest("androidx.databinding:databinding-compiler:7.0.4")
I think this is a known issue which you can review here. I've been tracking this issue since February. Doesn't seem to be fixed yet.
added android.enableExperimentalFeatureDatabinding=true in gradle.properties . According to the name of this property, I think it could work for sometimes but not always, you can try.
Dagger2 is not generating any component classes in android studio i know its a known problem while i have gone through almost all ways to implement in my android studio and have tried on various tutorials but every time i got struck here, it fails to build the daggercomponent class . I have also tried to rebuild ,clean gradles and invalidate caches but it does not help .
Here is my one of sample project build.gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}
}
apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'
android {
compileSdkVersion 25
buildToolsVersion "25.0.0"
defaultConfig {
applicationId "com.example.g.daggerillkillu"
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'
}
}
}
repositories{
maven{url "https://jetpack.io"}
}
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 'com.android.support:appcompat-v7:25.1.0'
testCompile 'junit:junit:4.12'
compile 'com.google.dagger:dagger:2.0.2'
apt 'com.google.dagger:dagger-compiler:2.0.2'
provided 'javax.annotation:jsr250-api:1.0'
}
vehiclemodule.java
#Module
public class vehiclemodule {
#Provides
#Singleton
Motor providesMotor(){
return new Motor();
}
#Provides
#Singleton
Vehicle provideVehicle(){
return new Vehicle(new Motor());
}
}
vehicleComponent.java
#Singleton
#Component(modules = {vehiclemodule.class})
public interface VehicleComponent {
Vehicle provideVehicle();
}
Is there any problem in the android studio or i am doing something wrong?
Is there any problem in the android studio or i am doing something wrong?
If nothing is being generated then you most likely do not have annotation processing enabled:
You also need to have modules, and build them.
Please check this full tutorial
https://github.com/codepath/android_guides/wiki/Dependency-Injection-with-Dagger-2
I am quite a .NET guy, developing in Xamarin. However, I got my hands on a piece of Java code for Android I would like to implement in C#. The problem is that the project was developed in Eclipse, and then ported to Android Studio and now cannot be compiled.
I got through all the issues with Gradle, but now I am stuck with some generated annotations and .put() and .get() methods, that cannot be referenced. There are many things similar to the following:
A normal interface like MySharedPreferences has its generated sealed (final) class counterpart MySharedPreferences_. This is then used in the code:
import com.someproject.MySharedPreferences_;
...
public class SomeAndroidClass {
public MySharedPreferences_ prefs;
public SomeMethod() {
String x = prefs.someValue().get;
...
prefs.someValue().put("abc");
}
}
Now, this cannot be compiled, because the MySharedPreferences_class is not generated by Android studio. I tried to get rid of the underscore and use the MySharedPreferences interface instead. But then I had problem referencing the .get() and .put() methods. Please, can somebody help me how to deal with this problem?
EDIT
Adding the build.gradle file:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.0.0'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}
}
repositories {
maven { url "http://dl.bintray.com/populov/maven" }
mavenCentral()
mavenLocal()
}
apply plugin: 'com.android.application'
apply plugin: 'android-apt'
def AAVersion = '4.0.0'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.testproject.smartconfig"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
apt "org.androidannotations:androidannotations:$AAVersion"
compile "org.androidannotations:androidannotations-api:$AAVersion"
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'fr.avianey.com.viewpagerindicator:library:2.4.1.1#aar'
}
apt {
arguments {
androidManifestFile variant.outputs[0]?.processResources?.manifestFile
}
}
MySharedPreferences_ is most likely class generated by Android Annotations library - androidannotations.org, check if Your build.gradle contains needed config: https://github.com/excilys/androidannotations/wiki/Building-Project-Gradle
Then reimport Your project using gradle.
My app was working fine - then today it wasn't. I'm getting this error and I've tried all of the answers (Except creating a new project and copying over).
Any ideas on what could have happened?
java.lang.RuntimeException: Unable to get provider com.facebook.FacebookContentProvider: java.lang.ClassNotFoundException: Didn't find class "com.facebook.FacebookContentProvider" on path: DexPathList[[zip file "/data/app/com.cleanercoding.myapp-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
It's like facebook just happened to uninstall itself? Not sure what to do at this point.
Here's my build.gradle file. Not sure if it's even correct:
buildscript {
repositories {
mavenCentral()
maven { url 'https://maven.fabric.io/public' }
}
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
classpath 'io.fabric.tools:gradle:1.+'
}
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
repositories {
mavenCentral()
maven { url 'https://maven.fabric.io/public' }
}
dependencies {
compile 'com.facebook.android:facebook-android-sdk:[4,5)'
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.google.android.gms:play-services-appindexing:8.1.0'
compile files('libs/activation.jar')
compile('com.twitter.sdk.android:twitter:1.12.1#aar') {
transitive = true;
}
compile('com.twitter.sdk.android:tweet-composer:1.0.3#aar') {
transitive = true;
}
}
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.cleanercoding.myapp"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
This happens when you havent initialized the SDK. Make sure it is initialized in your activity to address the issue.
I too got same issue , I solved this issue by changing Facebook version in dependency.
From
compile 'com.facebook.android:facebook-android-sdk:4.14.0' to compile 'com.facebook.android:facebook-android-sdk:4.15.0'
I have tried to import multiple libary for project in android studio.But it always shows error like
For this, I tried multiple solution like sdk update , studio update , build.gradle update and so on.Eventhough, i could not get any result.anyone give solution please.
Herewith I mentioned my gradle sepcification too.
app - build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.abof.android"
minSdkVersion 17
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.google.android.gms:play-services:7.5.0'
compile files('libs/volley.jar')
compile 'com.android.support:appcompat-v7:22.2.0'
compile project(':libraries:facebookV2')
compile files('libs/httpcore-4.3-beta1.jar')
compile files('libs/httpmime-4.3.5.jar')
compile project(':libraries:gson-2.2.4')
compile project(':libraries:citruslibrary')
}
project - build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}