Android 具有代码覆盖率的 BindingAdapter
Android BindingAdapter with code coverage
在Android模块中启用代码覆盖时,构建模块时出现以下错误(构建项目成功):
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 ****
违规属性来自自定义绑定适配器
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'
}
有人 运行 以前有过这种行为吗?
看起来这是一个错误,在 this post 的评论中,一个可能的解决方法是将 com.android.library
更改为 com.android.application
到 运行 测试。
在Android模块中启用代码覆盖时,构建模块时出现以下错误(构建项目成功):
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 ****
违规属性来自自定义绑定适配器
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'
}
有人 运行 以前有过这种行为吗?
看起来这是一个错误,在 this post 的评论中,一个可能的解决方法是将 com.android.library
更改为 com.android.application
到 运行 测试。