解决dataBinding引入造成的包冲突问题

MVVP dataBinding 在targetSdkVersion28 的使用

错误提示:

Error:Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'.
> java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex

错误代码块:

android{
	...
	dataBinding {
        enabled = true
    }
}
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    //noinspection GradleCompatible
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

错误原因:
dataBinding的jar包会自动依赖v4:21.0.3,版本与我们项目中的v7:28.0.0冲突
在terminal中输入gradlew -q app:dependencies 查看冲突的jar包
冲突列表
解决方案
手动配置v4包,加入与v7:28.0.0配合的v4:28.0.0

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    //noinspection GradleCompatible
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:support-v4:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

你可能感兴趣的:(Android环境配置)