gpt4 book ai didi

android-kotlin 错误 : unresolved reference databinding & ActivityMainBinding

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:56:22 46 4
gpt4 key购买 nike

我使用的是 Android Studio 3.0 Canary 6 版本。我在我的类中启用了数据绑定(bind),代码没有显示任何错误。但是,当我构建 APK 时,构建失败并显示以下错误:

Error:(8, 37) Unresolved reference: databinding  
Error:(22, 26) Unresolved reference: ActivityMainBinding
Error:(38, 50) Unresolved reference: ActivityMainBinding
Error:(43, 52) Unresolved reference: ActivityMainBinding
Error:(46, 52) Unresolved reference: ActivityMainBinding
Error:(49, 52) Unresolved reference: ActivityMainBinding
Error:(52, 52) Unresolved reference: ActivityMainBinding
Error:(55, 52) Unresolved reference: ActivityMainBinding
Error:(58, 52) Unresolved reference: ActivityMainBinding
Error:(61, 52) Unresolved reference: ActivityMainBinding
Error:(64, 52) Unresolved reference: ActivityMainBinding
Error:(67, 52) Unresolved reference: ActivityMainBinding
Error:(70, 52) Unresolved reference: ActivityMainBinding
Error:(73, 52) Unresolved reference: ActivityMainBinding
Error:Execution failed for task ':app:compileDebugKotlin'.
Compilation error. See log for more details

顶层build.gradle文件如下:

buildscript {
ext.kotlin_version = '1.1.3-2'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0-alpha6'
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()
mavenCentral()
}
}

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

模块build.gradle文件如下:

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'kotlin-kapt'

android {
compileSdkVersion 26
buildToolsVersion "26.0.1"
defaultConfig {
applicationId "com.example.jimil.calculator"
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner
"android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
}
dataBinding{
enabled = true
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
androidTestImplementation ('com.android.support.test.espresso:espresso-
core:3.0.1', {
exclude group: 'com.android.support', module: 'support-
annotations'
})
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:26.1.0'
testImplementation 'junit:junit:4.12'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
kapt 'com.android.databinding:compiler:3.0.0-alpha6'
}

kapt {
generateStubs = true
}

MainActivity.kt 类是:

import com.example.jimil.calculator.databinding.ActivityMainBinding  
...
class MainActivity : AppCompatActivity() {
private var binding: ActivityMainBinding? = null
...
}

请帮我解决构建错误。

最佳答案

你的 gradle 中有几个错误。

当前的 Gradle-Version 是 3.0.0-beta 3。这意味着您需要将类路径更改为

classpath 'com.android.tools.build:gradle:3.0.0-beta6'

同样适用于您的依赖项

kapt "com.android.databinding:compiler:3.0.0-beta6"

您还应该在适当的文件夹中提供您的插件。

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

您还应该启用编译选项,让您的注解处理器使用 Java8 依赖项(这可能是 Kotlin 目标 1.8 所必需的)。

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

完成后使缓存失效并重新启动

如果错误仍然存​​在,请检查 Gradle 控制台。您的 XML 文件中可能存在错误,这会阻止您的注释处理器生成数据绑定(bind)类。

关于android-kotlin 错误 : unresolved reference databinding & ActivityMainBinding,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46521478/

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