Android databinding tests fails in Module NoClassDefFoundError - java

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.

Related

Cannot resolve module dependency after updating to Android Studio 3.2.1

After upgrading to AS 3.2.1, I get this gradle sync error:
Unable to resolve dependency for ':app#debug/compileClasspath': Could not resolve project :androidlib_abc.
my app gradle.build:
apply plugin: 'com.android.application'
allprojects {
repositories {
// The order in which you list these repositories matter.
google()
jcenter()
maven {
url "https://maven.google.com"
}
}
}
android {
compileSdkVersion 28
defaultConfig {
applicationId "de.gpx.android.rtk"
minSdkVersion 24
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags ""
}
}
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
buildToolsVersion '28.0.3'
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
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'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
compile project(path: ':androidlib_abc')
}
my androidlib_abc gradle.build file:
apply plugin: 'com.android.library'
android {
compileSdkVersion 28
buildToolsVersion '28.0.3'
defaultConfig {
minSdkVersion 24
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
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'
testCompile 'junit:junit:4.12'
}
The "Open File" link in the error message points to the app gradle.build file. When I remove the line
compile project(path: ':androidlib_abc')
Then I can sync the grade project, but I cannot compile and use any package from that library (cannot resolve classes etc.)
I wish I didn't do the update... ("Never change a running system" -_-)
Any ideas how to fix?
UPDATE:
I removed the module from the project and then reimported it. Then everything worked fine! BUT: The module directory is copied into my project directory (usual behaviour). However, I need the module directory to be one level higher, in the same directory as the project directory is. This way, I could share the module with different projects without having duplicates and synchronizing work which saves me a lot of time (in fact I cannot work with multiple projects otherwise).
I used to use this settings.gradle file:
include ':app', ':androidlib_abc'
project(':androidlib_abc').projectDir = new File(rootProject.projectDir, '../androidlib_abc ')
which worked perfectly before AS 3.2.1
Now, after I successfully imported the module again and moved the module directory one level up and changed the settings.gradle file to this again, I get the unable-to-resolve-dependency error again :'-(
UPDATE2:
Using another constructor without the argument rootProject.projectDir allowed me to do the gradle sync.
include ':app', ':androidlib_abc'
project(':androidlib_abc').projectDir = new File('../androidlib_abc')
The library is finally listed on the left side!
However, a new issue came up. No classes/methods/fields etc. defined in the modul can be resolved. Not even in classes within the module itself although they are even in the same directory (package)... Cleaning and Rebuilding didn't help. Strange thing is, that building succeeds and I can apparently even install the app on my device and have it run without any exceptions...!? So is this just an IDE bug maybe?
UPDATE3
I deleted the .gradle and .idea directory and restarted AS. Everything's fine now B-)
try building androidlib_abc once .are you able to see this library in the side menu ?.
It could be that this particular library is not imported properly

Dagger2 not generating Daggercomponent classes

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

Could not resolve com.google.gms:google-services:3.0.0

I read many posts on this topic, but no one helped me.
I'm using Android Studio and following this guide
This is the project gradle:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
classpath 'com.google.gms:google-services:3.0.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
this is the app gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.1"
defaultConfig {
applicationId "com.xyv.appname"
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'
}
}
}
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.0.1'
compile 'com.android.support:design:25.0.1'
testCompile 'junit:junit:4.12'
compile 'com.google.firebase:firebase-core:10.0.1'
}
apply plugin: 'com.google.gms.google-services'
Here you can see where I added google-services.json inside my app:
These are my SDK tools:
Does anybody understand what's my error?
Thank you in advance
You may need to add
compile 'com.google.android.gms:play-services:9.6.1'
in your app build.gradle also. ref: Upgrade to Google Play Services:9.0.0 Error Failed to resolve: com.google.android.gms:play-services-measurement:9.0.0
If you recently bumped the version to 3.0.0 you should doublecheck that you have installed the correct version on your machine in the Android SDK manager.
Open the stand alone SDK Manager, in the Extras section the you will find "Google Play Services" update it .
I resolve my issue by updating android studio to 2.3 beta 2
I did just update an android studio and all is ruining well

Android google play services duplicate entry causing issue with productFlavors

Creating an app that has two productFlavors paid and free. Now since I use Google play services for Ads, its giving a very hard time throwing errors one by one, I've been searching on this one for the whole day, and it seems the root cause is the difference in version of play services specified in my gradle.
At the moment, this is the error I obtain:
Error:Execution failed for task
':app:transformClassesWithJarMergingForPaidRelease'.
com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry:
com/google/android/gms/internal/zzkb.class
Answers to similar question point to cross check if similar .jars exist. However I tried my best and I am helpless.
This is my app level build.gradle file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
lintOptions {
abortOnError false
}
defaultConfig {
applicationId "com.udacity.gradle.builditbigger"
minSdkVersion 17
targetSdkVersion 23
versionCode 1
versionName "1.0"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
free {
applicationId = "com.udacity.gradle.builditbigger.free"
}
paid {
applicationId = "com.udacity.gradle.builditbigger.paid"
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
// Added for AdMob
compile 'com.google.android.gms:play-services:8.4.0'
compile 'com.android.support:appcompat-v7:23.0.0'
compile 'com.google.firebase:firebase-ads:9.0.2'
compile project(path: ':backend', configuration: 'android-endpoints')
compile project(path: ':joketeller')
}
apply plugin: 'com.google.gms.google-services'
And this is my top level build.gradle file :
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.0'
classpath 'com.google.gms:google-services:2.1.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
Desperate to fix this, any help will be highly appreciated, Thanks!

Android Studio - Failed to resolve library in gradle

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()
}
}

Categories