Java – Android data binding test failed in module NoClassDefFoundError
I cannot run local unit tests in a module that uses the Android data binding library
First, let me talk about how to configure the project structure
project | app -MainLauncherActivity | myLibrary -CommonModuleActivity
I created a new project and then added a new module "MyLibrary"
The main "app" depends on the "MyLibrary" module I added an activity in "MyLibrary" that supports data binding I called a module specific activity from the main "app" activity when I clicked the button It just works and can run applications
However, when I add test cases for module activities, I get the following error
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 }
Here 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') }
In MyLibrary build Below 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 } }
The corresponding test cases can be found in the attached screenshot
Someone can tell me what I did wrong here to test it
App works normally, only the unit test fails!
Solution
I think this is a known problem, you can check here I have been following this issue since February It doesn't seem to have been repaired