Java Android数据绑定测试在模块NoClassDefFoundError中失败
我无法在使用android数据绑定库的模块中运行本地单元测试 首先让我介绍一下项目结构及其配置Java Android数据绑定测试在模块NoClassDefFoundError中失败,java,android,unit-testing,data-binding,android-databinding,Java,Android,Unit Testing,Data Binding,Android Databinding,我无法在使用android数据绑定库的模块中运行本地单元测试 首先让我介绍一下项目结构及其配置 project | app -MainLauncherActivity | myLibrary -CommonModuleActivity 我已经创建了一个新项目,之后添加了一个新模块“myLibrary” 主“应用程序”依赖于“myLibrary”模块。我在“myLibrary”中添加了一个活动,它支持数据绑定。我从点击按钮的主
project
| app
-MainLauncherActivity
| myLibrary
-CommonModuleActivity
我已经创建了一个新项目,之后添加了一个新模块“myLibrary”
主“应用程序”依赖于“myLibrary”模块。我在“myLibrary”中添加了一个活动,它支持数据绑定。我从点击按钮的主“应用”活动中调用了模块特定活动。它只是工作,可以运行应用程序
但是,当我为模块活动添加测试用例时,会出现以下错误
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
项目根目录文件:
// 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
}
以下是“应用程序”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')
}
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"
}
在myLibrary 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')
}
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"
}
图书馆活动:
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
}
}
相应的测试用例可以在附带的屏幕截图中找到
有人能告诉我我在这里做了什么错事来测试它吗
应用程序运行良好,只有单元测试失败 我认为这是一个大家都知道的问题,你可以回顾一下。我从二月份开始跟踪这个问题。似乎尚未修复。在
gradle.properties
中添加了android.enableExperimentalFeatureDatabinding=true
。根据这个属性的名称,我认为它有时可以工作,但不总是可以,你可以尝试。m仍在寻找解决方案:(我也面临同样的问题,其他人也面临同样的问题?@Ballem但是,它只能在android studio的beta或alpha版本工作,你应该关心。