【问题标题】:Android Jetifier fails to convert data binding generated support library to AndroidXAndroid Jetifier 无法将数据绑定生成的支持库转换为 AndroidX
【发布时间】:2019-08-22 06:00:24
【问题描述】:

我有一个已迁移到 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。

【问题讨论】:

  • 在实现 'androidx.constraintlayout:constraintlayout:1.1.2' 时尝试 androidx.support.constraint.ConstraintLayout 而不是 android.support.constraint.ConstraintLayout
  • @AMaharaja 我的 build.gradle 中已经有 implementation 'androidx.constraintlayout:constraintlayout:1.1.3'。但我不能只在 GallerypickerBinding.java 中将 android.support.constraint.ConstraintLayout 更改为 androidx,因为该文件是从数据绑定自动生成的。
  • @cynw Gallerypicker.java 和它的 XML 资源有什么相似之处?
  • @MartinZeitler gallerypicker.xml 是第三方库中的资源文件。它有 support.constraint.ConstraintLayout 作为它的根视图组。 GallerypickerBinding 是从该库中使用的数据绑定生成的文件。

标签: android android-constraintlayout android-databinding androidx android-jetifier


【解决方案1】:

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

com.android.support.constraint to androidx.constraintlayout

【讨论】:

  • 不幸的是,com.android.support.constraint 用于第三方库而不是我的项目。我只能看到数据绑定生成的文件。
  • com.android.support.constraint 不是第三方库。将项目迁移到 androidx 后,com.android.support.constraint 这个包被更改为 androidx.constraintlayout 名称。迁移您的项目只是迁移您使用的库,而不是更改现有项目中使用的包名称。所以你手动更改 java 和 xml 文件中的导入库
  • @cynw 正如 Suraj Singh 所说,您需要将 java、kotlin 文件中的“import com.android.support.constraint”替换为“import androidx.constraintlayout”。并且还在 XML 布局文件中替换为“androidx.constraintlayout.widget.ConstraintLayout”。
【解决方案2】:

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

【讨论】:

  • 谢谢。正如你所说,我已经替换了依赖项。我还尝试将support.constraint 添加到我的依赖项中,但它仍然抱怨support.constraint 在数据绑定生成的文件中不存在。
  • gallerypicker.xml 是来自 3rd 方库的 XML 资源文件。它有support.constraint.Constraint.ConstraintLayout 作为它的根小部件。而且,GallerypickerBinding.java 是从这个 XML 文件的数据绑定生成的文件。
  • @cynw 为什么你一直引用第三方库...而不是简单地将that library 作为库模块添加到项目中并重构为androidx?这应该是直截了当的。
猜你喜欢
  • 2020-03-29
  • 2019-04-24
  • 2020-07-25
  • 2019-08-21
  • 2019-05-13
  • 1970-01-01
  • 2019-04-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多