具有代码覆盖率的Android BindingAdapter

时间:2017-01-13 17:08:52

标签: android android-testing android-databinding

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

  

错误:任务执行失败':mylibrary:compileDebugAndroidTestJavaWithJavac'。    java.lang.RuntimeException:发现数据绑定错误。   **** /数据绑定错误****消息:在android.widget.ImageView上找不到参数类型为float的属性'app:mainAngle'的setter。   文件:d:\代码\的Android \ TestApplication \在MyLibrary \建立\中间体\束\调试\ RES \布局\ 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'
}

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

1 个答案:

答案 0 :(得分:0)

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

相关问题