AndroidX 与 Android 数据绑定不兼容

2023-12-23

好的,我的任务是将项目迁移到 AndroidX,以减少项目中支持库的混乱。我已经按照官方文档启用了 AndroidX,但现在在尝试通过相应的自动生成的 Binding 类(通过在模块 gradle 中启用数据绑定创建)来膨胀视图时遇到运行时错误。

深入研究自动生成的源代码,我发现了这种方法,它是导致代码抛出的方法:

   public List<DataBinderMapper> collectDependencies() {
        ArrayList<DataBinderMapper> result = new ArrayList(1);
        result.add(new com.android.databinding.library.baseAdapters.DataBinderMapperImpl());
        return result;
    }

正如您所看到的,自动生成的代码正在尝试从com.android.databinding包,但该包不存在于输出 APK 中,因为我已从我的 gradle 中删除了支持依赖项(因为 AndroidX 应该替换它们)。我可以看到 androidx 有一个数据绑定包,所以我假设上面的自动生成的代码应该引用 androidx.databinding 包,但事实并非如此。

这是工具中的错误还是我配置错误?

这是我的 gradle 文件(出于安全原因省略了一些位):

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'

//These variable refer to release builds so make sure they are correct. If you need to override them
//for some specific development needs then use variables that can be passed to gradle on command line.
String releaseVersionName = '1.0.0'
int releaseVersionCode = 1
int releaseMinSdk = 18
int releaseCompileSdkVersion = 28

android {
    //Added as separate variable so it can be overridden from IDE to speed up compilation time
    //Set minimum compilation sdk.
    int developMinSdk = rootProject.hasProperty('productMinSdk') ?
            rootProject.productMinSdk.toInteger() : releaseMinSdk
    String developProductVersionName = rootProject.hasProperty('productVersionName') ?
            rootProject.productVersionName : releaseVersionName
    int developProductVersionCode = System.getenv("BUILD_ID") as Integer ?: releaseVersionCode
    int developCompileSdk = rootProject.hasProperty('productCompileSdk') ?
            rootProject.productCompileSdk.toInteger() : releaseCompileSdkVersion

    defaultConfig {
        applicationId "..."
        compileSdkVersion developCompileSdk
        minSdkVersion developMinSdk
        targetSdkVersion developCompileSdk
        versionCode developProductVersionCode
        versionName developProductVersionName

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    signingConfigs {
       ...
    }

    sourceSets {
        ...
    }

    buildTypes {
        debug {
            signingConfig signingConfigs.release
            dexOptions {
                jumboMode = true
                javaMaxHeapSize "1g"
            }
            multiDexEnabled true
            matchingFallbacks = ['debug', 'release']
        }
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release
            dexOptions {
                jumboMode = true
                javaMaxHeapSize "1g"
            }
        }
    }

    flavorDimensions "default"

    productFlavors {
        //noinspection GroovyMissingReturnStatement
        develop {
            applicationIdSuffix ".develop"
            dimension "default"
            sourceSets {
                develop.java.srcDirs += 'src/develop/kotlin'
            }
        }

        //Normal build for release
        //noinspection GroovyMissingReturnStatement
        playstore {
            //In this flavour we use release* variable explicitly so they cannot be
            //overridden by mistake
            //Force min sdk version from the global variable
            minSdkVersion releaseMinSdk
            //Force version name from the global variables
            versionName releaseVersionName
            //Force version code from the global variable
            versionCode releaseVersionCode
            //Force compile and target sdk versions from the global variable
            compileSdkVersion releaseCompileSdkVersion
            targetSdkVersion releaseCompileSdkVersion
            dimension "default"
            sourceSets {
                playstore.java.srcDirs += 'src/playstore/kotlin'
            }
        }
    }

    dataBinding {
        enabled = true
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    // Runtime dep versions
    def condecoCoreVersion = "0.1.3"
    def appCenterVersion = "1.9.0"
    def thirtyinchVersion = '0.9.0'
    def stethoVersion = "1.5.0"
    def leakCanaryVersion = '1.5.4'
    def hahaVersion = "1.3"
    def multiDexVersion = "2.0.0"
    def constraintLayoutVersion = "1.1.3"

    // Test dep versions
    def jUnitVersion = "4.12"

    // Std lib dependency
    implementation group: 'org.jetbrains.kotlin', name: 'kotlin-stdlib-jdk8', version: "$kotlin_version"
    implementation group: 'org.jetbrains.kotlin', name: 'kotlin-reflect', version: "$kotlin_version"

    // Multidex dependency
    implementation "androidx.multidex:multidex:$multiDexVersion"

    // Junit dependency for testing
    testImplementation "junit:junit:$jUnitVersion"
}

这是我的 gradle.properties 文件:

# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx1536m

# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
# Kotlin code style for this project: "official" or "obsolete":
kotlin.code.style=official

# Use androidX to replace requirement for
# Support libraries to be imported via gradle
android.useAndroidX=true

# Jetifier automatically updates dependancy binaries
# To swap out support lib for androix
android.enableJetifier=true

编辑:这是我的项目级别 gradle:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext {
        kotlin_version = '1.2.71'
        gradle_plugin_version = '3.2.1'
    }

    repositories {
        google()
        jcenter()
    }

    dependencies {
        classpath "com.android.tools.build:gradle:$gradle_plugin_version"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        mavenCentral()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

好吧,终于破解了这个。

问题是我使用的库依赖于 android 数据绑定(而不是 androidx 数据绑定)。

即使我在我的电脑中启用了 Jetifiergradle.properties文件,由于某种原因,库二进制文件没有将 android 数据绑定交换为相应的 androidx 版本。幸运的是,该库位于内部,因此我更新了该库以迁移到 androidx,这整个噩梦就自行解决了。

感谢您的所有建议,希望这个答案可以帮助任何遇到类似问题的人,因为我花了 2 个工作日才弄清楚!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

AndroidX 与 Android 数据绑定不兼容 的相关文章

  • 如何获取之前的碎片?

    为了在我的应用程序中重用某些片段 我需要知道哪个片段是返回堆栈上的第二个片段 为了做到这一点 我正在使用getFragmentManager getFragments 显示以下错误 但有效 FragmentManager getFragme
  • Android 在打开应用程序时会广播吗?

    例如 如果我想知道Youtube何时打开 是否有与之相关的广播 我当然知道我可以轮询 logcat 消息来检查活动 但我可以通过广播来做到这一点吗 因为它会少得多的耗电 此链接似乎表明这是不可能的 如何跟踪 Android 中的应用程序使用
  • 如何使用Android opencv使图像的白色部分透明

    我无法链接超过 2 个网址 因此我将我的照片发布到此博客 请在这里查看我的问题 http blog naver com mail1001 220650041897 http blog naver com mail1001 220650041
  • Service 和 IntentService,运行从服务器轮询数据库值的服务哪个更好?

    我读过很多关于Service and IntentService 然而 当做出决定时 我没有足够的信心选择使用哪种类型来创建一个后台服务 该服务将在一定时间间隔内从数据库轮询数据 并在获得所需数据时停止它 因为数据代表请求的状态 例如 订购
  • 从 Android 代码设置的 SECRET_CODE

    我知道如何使用清单文件中的秘密代码 它与此源代码配合良好
  • Android 如何更改 OnTouchListener 上的按钮背景

    你好 我在 xml 中有一个按钮 我正在使用OnTouchListener在我的活动中获得button按下并释放 但问题是 当我按下按钮时背景颜色没有改变 当我延长可能的活动时OnClickListener背景正在改变 任何人都可以告诉我的
  • 调试:在 Android 1.0 中找不到文件

    今天我更新到 Android Studio v 1 0 在尝试编译任何项目时出现以下错误 app build intermediates classes debug 找不到文件 问题是在更新之前我没有任何问题 这是我实际尝试编译的代码 构建
  • 如何在android线性布局上获得阴影? [复制]

    这个问题在这里已经有答案了 可能的重复 如何在android中为View设置阴影 https stackoverflow com questions 4406524 how to set shadow to a view in androi
  • Android 服务是否有办法检测设备何时锁定?

    我有一个 Android 服务 我希望在设备锁定时执行操作 我想澄清一下 我对屏幕开 关状态不感兴趣 我知道如何使用带有 Intent ACTION USER PRESENT 和 KeyguardManager inKeyguardRest
  • 如何检查 Android 中连接的 wifi 网络是否处于活动状态

    如何自动检查android中连接的WiFi网络上的互联网是否处于活动状态 我可以检查 wifi 是否已启用或 wifi 网络是否已连接 但我不确定如何检查互联网是否已连接 这可能吗 private boolean connectionAva
  • 画透明圆,外面填充

    我有一个地图视图 我想在其上画一个圆圈以聚焦于给定区域 但我希望圆圈倒转 也就是说 圆的内部不是被填充 而是透明的 其他所有部分都被填充 请参阅这张图片了解我的意思 http i imgur com zxIMZ png 上半部分显示了我可以
  • 使用 PhoneGap 使 Android 应用程序易于访问(对于残障人士)

    有人有过使用 PhoneGap 使 Android 应用程序可访问的经验吗 至少我们需要使我们的应用程序符合第 508 条规定 我尝试实现一些标准的辅助功能 文本框标签 向 div 添加标题属性等 但是 当在 Android 中使用 Tal
  • 有关 ListView 自定义行布局项目上的 onClick() 事件的帮助

    我有一个 ListView 其行由我格式化 每行都有 ImageView 和 TextView 的混合 我还实现了自己的适配器 并且能够通过它绘制每一行 现在 我想要这样的东西 用户单击 ImageView 不是行上的其他任何位置 但只有此
  • 如何在android asynctask中使用inputstream作为参数?

    我正在制作一个 Android 应用程序来跟踪股票详细信息 我将通过 csv 雅虎财经 检索数据 据我所知 在android 4 0中 网络连接无法在主线程上完成 因此 我将使用 asynctask 来建立连接 但是 我在参数方面遇到了一些
  • Android:监听状态栏通知

    有没有办法在状态栏被下拉时监听通知 1 用于检测状态栏变化 您可以注册一个监听器来获取系统UI可见性变化的通知 因此 要在您的活动中注册侦听器 Detecting if the user swipe from the top down to
  • Android:RecyclerView 不显示片段中的列表项

    有人可以帮我尝试让我的 RecyclerView 出现吗 如果我不在片段中实现它 就会出现这种情况 然而 当我尝试将其实现到片段中时 CarFront 中的其他 XML 代码与 RecyclerView 分开显示 我的日志中收到此错误 E
  • Android studio - 如何查找哪个库正在使用危险权限?

    我正在尝试将 apk 上传到 google play 商店 但令我惊讶的是 我正在使用以下权限 Your APK is using permissions that require a privacy policy android perm
  • 单元测试时 Android Studio 2.0 中测试状态终止且没有任何失败消息

    Issue 我昨天在 Ubuntu 上从 1 5 升级到了 Android Studio 2 0 当我在 Android Studio 2 0 中进行单元测试时 即使所有测试都已通过 它也会显示 终止测试 状态 有时它只显示部分测试通过 我
  • 使用单选按钮更改背景颜色 Android

    我试图通过从单选组中选择单选按钮来更改应用程序选项卡的背景 但是我不确定如何执行此操作 到目前为止我已经 收藏夹 java import android app Activity import android os Bundle publi
  • Android 中带有组的列表视图

    我有一个列表视图 每行都有一些日期和文本 我可以像 iPhone 中那样将这个 listView 分组 组之间有标题吗 在 android 中是否可能 请帮忙 即 我需要在 Listview 行之间有标题栏 以便如果我使用日期对其进行分组

随机推荐