启用视图绑定后,Android Studio 会给出错误和警告。怎么修?

Wiz*_*006 6 android gradle kotlin android-viewbinding

在我的项目中启用视图绑定后出现错误。

我尝试重新同步项目,使缓存无效。我的应用程序gradle文件:

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

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId "com.learning.aboutme"
        minSdkVersion 19
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    viewBinding.enabled = true

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.13'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
Run Code Online (Sandbox Code Playgroud)

我的项目gradle文件

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

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

     }
     dependencies {
         classpath 'com.android.tools.build:gradle:3.6.3'
         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
}
Run Code Online (Sandbox Code Playgroud)

我的活动 main.xml:

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

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId "com.learning.aboutme"
        minSdkVersion 19
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    viewBinding.enabled = true

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.13'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
Run Code Online (Sandbox Code Playgroud)

在这里,机器人工作室给出一个警告android:contentDescriptiontextAlignmentlineSpacingMultipliertext

这是我的主要 activity.kt 文件:

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

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

     }
     dependencies {
         classpath 'com.android.tools.build:gradle:3.6.3'
         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
}
Run Code Online (Sandbox Code Playgroud)

这里 android studio 说:无法访问android.viewbinding.ViewBinding哪个是com.learning.aboutme.databinding.ActivityMainBinding. 在ActivityMainBinding.inflate()

我尝试创建一个新项目并将我的文件移动到那里,但出现相同的错误(尝试移动activity_main.xml和相同的错误。

如何解决这个问题?

uwe*_*uwe 12

对我来说,当我添加时错误消失了

implementation 'com.android.databinding:viewbinding:4.0.1'
Run Code Online (Sandbox Code Playgroud)

build.gradle文件。

此依赖项包含android.viewbinding.ViewBindingAndroidStudio 无法访问的类。

  • 而是现代 Gradle 中的“compileOnly”。正如文档[source](https://blog.gradle.org/introducing-compile-only-dependencies)所述:“在编译时需要API但其实现由使用库、应用程序或运行时提供的依赖项环境。” (6认同)
  • 我们使用“compile”而不是“implementation”,因此依赖项不会包含在 APK 中,因为此问题仅出现在 Android Studio 中,而不是在运行应用程序时出现。 (4认同)

Rom*_*ych 12

就我而言,以下步骤(清除缓存)确实有帮助:

第 1 步:打开终端并转到终端中的项目路径。
步骤 2:可选点击此命令git clean -xfd或安全变体git clean请小心-xfd这一点,因为这会删除未跟踪的文件和.gitignore files
步骤 3:转到:文件 -> 使缓存无效/重新启动。

  • 请小心此命令 - 它将删除 gitignore 中引用的任何文件。如果您有一些未跟踪的安全文件或密钥或您未推送但项目中存在的任何内容,它们将被删除。人们应该停止发布命令而不解释他们在做什么 (4认同)

sus*_*vi 11

昨天面临同样的问题,确切的错误。对我来说解决方案是

  • 第 1 步 - 删除 .Idea 文件夹
  • 第 2 步 - 关闭 android studio
  • 步骤3-使用相同的项目路径重新打开android studio(这将生成新的.Idea文件夹)

并且错误消失了

确保

  1. android.useAndroidX=true。在 gradle.properties 文件内
  2. 在应用程序 build.gradle 内部
 buildFeatures {
       viewBinding true
 }
Run Code Online (Sandbox Code Playgroud)
  1. 始终使用小写的文件夹结构,后者仅允许类名大写
  2. 如果您在 xml 或布局标签顶部使用任何版权注释而没有变量标签,或者我们可以说没有任何数据,则删除它并尝试清理/重建项目