gpt4 book ai didi

android - 使用 Android 数据绑定(bind)时禁用 Kotlin 合成绑定(bind)

转载 作者:行者123 更新时间:2023-12-04 11:33:58 26 4
gpt4 key购买 nike

我正在处理 this Udacity course on Android/Kotlin/KTX . “关于我”应用程序引入了数据绑定(bind),而 Android 团队似乎 advise against using KTX binding .所以我想做前者而不是后者。

然而,事情并没有按预期进行。这些是我面临的奇怪问题:

  • 在我第一次通过时,我无法让 Android Studio 识别我的任何 R.id.viewName身份证。打字 R.id.会给我各种选择,例如 R.id.action_bar ,但我的 Activity 中没有。在某些时候,这清楚了。
  • 直到我构建了这个东西(例如 lateinit var binding: ActivityMainBinding )之前,编辑器一直提示数据绑定(bind)类的引用未解决。实际上,我认为它需要不止一个构建,可能是由于我在其他地方乱搞的结果(见下文)。
  • 每个 View ID 仍然作为包含该 ID 的 View 对象的全局变量生成。也就是说,如果我有一个 ID 为 doneButton 的按钮,我可以在代码中将其引用为合成 doneButton只需导入它。我假设这意味着对象总是在运行时构建的,即使我不打算使用它们。如何让这些 KTX 风格的绑定(bind)消失?

  • 这是我的构建等级:
    // Top-level build file where you can add configuration options common to all sub-projects/modules.

    buildscript {
    ext.kotlin_version = '1.3.31'
    repositories {
    google()
    jcenter()

    }
    dependencies {
    classpath 'com.android.tools.build:gradle:3.4.0'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
    }
    }

    allprojects {
    repositories {
    google()
    jcenter()

    }
    }

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

    这是我的模块gradle:
    apply plugin: 'com.android.application'

    apply plugin: 'kotlin-android'

    apply plugin: 'kotlin-android-extensions'

    android {
    compileSdkVersion 28
    defaultConfig {
    applicationId "com.example.android.aboutme"
    minSdkVersion 19
    targetSdkVersion 28
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
    release {
    minifyEnabled false
    proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
    }
    dataBinding {
    enabled = true
    }
    }

    dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    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'
    }

    在网上搜索生成数据绑定(bind)类的方法时,我发现了 a possible need to include the following linegradle.properties :
    android.databinding.enableV2=true

    这样做后我进行了重建,瞧!这些类(class)终于建成了。所以我正准备向 Udacity 发送有关缺失位的反馈,当我决定最好确保我了解真正解决问题的方法时。所以程序运行成功后,我把上面的 gradle.properties去掉了线,清理和重建项目。令我惊讶的是,它仍然可以构建并运行。

    所以我不知道是什么解决了这个问题或者如何在下一个项目中解决这些问题。

    我还想阻止所有全局 KTX 绑定(bind)类被实例化,但我仍然对如何做到这一点一无所知。

    Another SO answer声称为生成绑定(bind)类的问题提供了两种解决方案,但给出的解决方案是针对 2016 年和 2017 年的,尽管没有实现这些解决方案,上述 gradle 文件目前正在工作(生成绑定(bind)类),所以显然有一种更新的方法。

    谢谢你尽你所能的帮助! (哦,我还要提一下,我使用的是完全更新的 Android Studio 3.4 版。)

    更新 1 : 我正在取得一些进展。看来,Android Studio 3.4 代码编辑器是唯一无法识别生成的数据绑定(bind)类的东西。重建时会再次识别这些类,但清洁时会丢失识别。我还不确定这种理解能解决我所有的问题......

    更新 2 :这是(至少部分) an Android Studio bug这是在三月份修复的,由于 Canary 7.

    最佳答案

    为了禁用 Kotlin Synthetic 访问,从应用级别的 build.gradle 文件中删除插件 kotlin-android 扩展 .这个插件生成合成导入。

    关于android - 使用 Android 数据绑定(bind)时禁用 Kotlin 合成绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56103689/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com