Search code examples
androidandroid-testingandroid-databinding

Android BindingAdapter with code coverage


When enabling code coverage in an Android module, I get the following error when building the module (building the project succeeds):

Error:Execution failed for task ':mylibrary:compileDebugAndroidTestJavaWithJavac'. java.lang.RuntimeException: Found data binding errors. ****/ data binding error ****msg:Cannot find the setter for attribute 'app:mainAngle' with parameter type float on android.widget.ImageView. file:D:\code\Android\TestApplication\mylibrary\build\intermediates\bundles\debug\res\layout\mylayout.xml loc:15:29 - 15:52 ****\ data binding error ****

The offending attribute is from a custom 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
    }
}

And the layout

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

This works fine when debugging the app that references this module. It also works fine until I turn on testCoverageEnabled in the module's gradle file.

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

Anybody run into this behavior before?


Solution

  • Looks like it's a bug, in the comment in this post a possible workaround is change com.android.library to com.android.application to run the tests.