Android data binding tests not run in NoClassDefFoundError module

I was unable to run local unit tests in a module that uses the Android data binding library.

First, let me talk about the structure of the project, how it is configured.

project
      | app
           -MainLauncherActivity
      | myLibrary
            -CommonModuleActivity

I created a new project, after which I added a new module "myLibrary".

the main "application" depends on the module "myLibrary". I added one activity to "myLibrary", it supports data binding. I called the module activity from the main activity of the application when I clicked a button in it. it just works, was able to run the application.

But, having received the following errors when adding a test case for 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

SCREENSHOT PROGRAM SCREEN

gradle project 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 the "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')
}

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

.

- , , .

, !!!

+4
2

, , . . , .

0

android.enableExperimentalFeatureDatabinding=true gradle.properties. , , , , .

0

Source: https://habr.com/ru/post/1672321/


All Articles