1

我有一个已迁移到 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.databinding.ViewDataBindingAndroidX,但在同一个文件中,它仍然使用android.support.constraint.ConstraintLayout支持库。

我希望 Android Jetifier 能够将所有支持库转换为 AndroidX,但它似乎无法将数据绑定生成的 ConstraintLayout 转换为 AndroidX。

4

2 回答 2

0

对于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对资源的看法可能是正确的——如果是这样,他的答案应该是被接受的。

于 2019-04-01T06:47:50.290 回答
0

您必须在 java 文件和 xml 文件中更改包名称。

com.android.support.constraint to androidx.constraintlayout
于 2019-04-01T06:25:59.373 回答