我有一个已迁移到AndroidX的Android项目。在某些时候,我想添加一个新的库。该库使用带有数据绑定的支持库。
我在gradle.properties中启用了Android Jetifier。我使用的是Android Gradle构建工具v.3.3.2和Gradle v.4.10.1。
这是我的gradle.properties:
org.gradle.jvmargs=-Xmx1536m
kotlin.code.style=official
android.useAndroidX=true
android.enableJetifier=true
这是我的build.gradle:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.test"
minSdkVersion 17
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
dataBinding {
enabled = true
}
}
configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
def requested = details.requested
if (requested.group == 'com.android.support') {
if (!requested.name.startsWith("multidex")) {
details.useVersion '28.0.0'
}
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.0.0-beta01'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation <library with AndroidX and data binding>
}
我在编译时遇到以下错误。
Task :app:compileDebugJavaWithJavac FAILED
GallerypickerBinding.java:22: error: package android.support.constraint does not exist
private final android.support.constraint.ConstraintLayout mboundView0;
GallerypickerBinding
是从新添加的库的数据绑定生成的类。当我检查这个文件时,它使用来自AndroidX的androidx.databinding.ViewDataBinding
,但是在同一个文件中,它仍然使用来自支持库的android.support.constraint.ConstraintLayout
。
我希望Android Jetifier能够转换所有支持库,包括AndroidX,但似乎无法将数据绑定生成的ConstraintLayout转换为AndroidX。
您必须在java文件和xml文件中更改包名称。
com.android.support.constraint to androidx.constraintlayout
对于Kotlin,替换此依赖项:
implementation "androidx.appcompat:appcompat:1.0.0-beta01"
这些(不确定第二个是否需要,但它是非常必要的):
implementation "androidx.appcompat:appcompat:1.0.2"
implementation "androidx.core:core-ktx:1.0.1"
并且对于那个数据绑定错误......要么清理项目 - 要么尝试将旧的com.android.support.constraint
包添加到依赖项中一次,以使其停止抱怨(仅对于测试,它将重写它的命名空间)。如果这没有帮助,请在问题中添加Gallerypicker.java
及其XML,以供进一步审核。
@Suraj Singh可能对资源是正确的 - 如果是这样,他的回答应该是被接受的。