【问题标题】:Android BindingAdapter with code coverage具有代码覆盖率的 Android BindingAdapter
【发布时间】:2017-01-13 17:08:51
【问题描述】:

在Android模块中启用代码覆盖时,在构建模块时出现以下错误(构建项目成功):

错误:任务 ':mylibrary:compileDebugAndroidTestJavaWithJavac' 执行失败。 java.lang.RuntimeException:发现数据绑定错误。 ****/ 数据绑定错误 ****msg:在 android.widget.ImageView 上找不到参数类型为 float 的属性“app:mainAngle”的设置器。 文件:D:\code\Android\TestApplication\mylibrary\build\intermediates\bundles\debug\res\layout\mylayout.xml 地点:15:29 - 15:52 ****\数据绑定错误****

违规属性来自自定义 bindingAdapter

public class MyViewModel extends BaseObservable {

    @Bindable
    public void setCurrentAngle(float f){
       notifyPropertyChanged(BR.currentAngle);
    }

    @Bindable
    public float getCurrentAngle(){
        return 0f;
    }

    @BindingAdapter("app:mainAngle")
    public static void setRotateCompass(View view, float currentAngle) {
        // Do stuff
    }
}

还有布局

<?xml version="1.0" encoding="utf-8"?>
<layout>
    <data>
        <variable
            name="myViewModel"
            type="com.example.mylibrary.MyViewModel" />
    </data>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:mainAngle="@{myViewModel.currentAngle}"
            />
    </LinearLayout>
</layout>

在调试引用此模块的应用程序时,这可以正常工作。在我打开模块的 gradle 文件中的 testCoverageEnabled 之前,它也可以正常工作。

apply plugin: 'com.android.library'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.1"

    defaultConfig {
        minSdkVersion 22
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }
    buildTypes {
        debug {
            testCoverageEnabled = true
        }
        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.0.1'
    testCompile 'junit:junit:4.12'
}

以前有人遇到过这种行为吗?

【问题讨论】:

    标签: android android-testing android-databinding


    【解决方案1】:

    看起来这是一个错误,在this post 的评论中,可能的解决方法是将com.android.library 更改为com.android.application 以运行测试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-06
      • 1970-01-01
      • 1970-01-01
      • 2018-10-14
      • 2016-10-23
      • 1970-01-01
      • 2016-02-28
      相关资源
      最近更新 更多