【问题标题】:Android databinding tests fails in Module NoClassDefFoundErrorAndroid 数据绑定测试在模块 NoClassDefFoundError 中失败
【发布时间】:2017-08-05 18:30:16
【问题描述】:

我无法在使用 android 数据绑定库的 Module 中运行本地单元测试。

首先让我介绍一下项目结构是如何配置的。

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

PROJECT IDE SCREENSHOT

项目根 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: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
}

下面是“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"
}

图书馆活动:

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

对应的测试用例见附件截图。

有人能告诉我我在这里做错了什么来测试它吗?

应用程序运行良好,只有单元测试失败!!!

【问题讨论】:

  • 我仍在寻找解决方案:(
  • 我也遇到了同样的问题,还有人遇到同样的问题吗?

标签: java android unit-testing data-binding android-databinding


【解决方案1】:

我认为这是一个已知问题,您可以查看 here。自二月份以来,我一直在跟踪这个问题。似乎还没有修复。

【讨论】:

    【解决方案2】:

    gradle.properties 中添加了android.enableExperimentalFeatureDatabinding=true。根据这个属性的名称,我认为它有时会起作用,但并非总是如此,你可以试试。

    【讨论】:

    • @Ballem 但是,它只能在 android studio 的 beta 或 alpha 版本中工作,你应该小心。
    【解决方案3】:

    我知道现在回答这个问题为时已晚,但我对可能面临这个问题的朋友说。

    任何时候你使用 dataBinding 并且在单元测试中遇到 NoClassDefFoundError

    首先在 build.gradle(Module) 中添加这一行

    testOptions {
            unitTests{
                isIncludeAndroidResources=true
            }
        }
    

    然后将此库添加到 build.gradle(Module) 的依赖项中

    kaptTest("androidx.databinding:databinding-compiler:7.0.4")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-11
      • 2014-10-26
      • 1970-01-01
      • 2018-02-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多