1

我正在添加安全参数依赖项:

项目级别:

dependencies {
    classpath 'com.android.tools.build:gradle:3.3.0'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$version_kotlin"
    classpath "android.arch.navigation:navigation-safe-args-gradle-plugin:$version_navigation"

应用级别:

apply plugin: 'androidx.navigation.safeargs'

当我同步项目时,我的数据绑定停止工作,例如:

val binding: FragmentGameOverBinding = DataBindingUtil.inflate(
        inflater, R.layout.fragment_game_over, container, false)
binding.tryAgainButton.setOnClickListener {view: View ->
    view.findNavController().navigate(R.id.action_gameOverFragment2_to_gameFragment2)
}
return binding.root

binding.root 显示未解析的引用,当我转到 DataBindingUtil.java 时,出现错误:

Library source does not match the bytecode for class DataBindingUtil

我正在遵循 Google udacity 课程中的确切步骤,但似乎添加安全参数依赖项与数据绑定库发生了某种冲突。我能做些什么来解决这个问题?

4

1 回答 1

1

转到你的gradle.properties文件检查你里面有这个或添加它:

android.enableJetifier=true
android.useAndroidX=true

然后转到您的build.gradle (app) 并将其添加到 android{

dataBinding {
    enabled = true
}
compileOptions {
    sourceCompatibility = JavaVersion.VERSION_1_8
    targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
    jvmTarget = JavaVersion.VERSION_1_8
}

这里没有更新udacity课程是我使用的依赖版本

注意这部分:

不要将其用作 Udacity 建议(在 build.gradle 应用程序级别) 不要使用 android.arch

// Navigation
implementation "android.arch.navigation:navigation-fragment-ktx:$version_navigation"
implementation "android.arch.navigation:navigation-ui-ktx:$version_navigation"

利用

// Navigation

implementation "androidx.navigation:navigation-fragment-ktx:2.3.0-alpha04"
implementation "androidx.navigation:navigation-ui-ktx:2.3.0-alpha04"

并在 build.gradle (项目级别)

classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.3.0-alpha04

这是使用版本的完整依赖项列表(以防万一)

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

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.21"

    // Support libraries
    implementation "androidx.constraintlayout:constraintlayout:1.1.3"

    // Android KTX
    implementation "androidx.core:core-ktx:"1.2.0"

    // Room and Lifecycle dependencies
    implementation "androidx.room:room-runtime:$version_room"
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    kapt "androidx.room:room-compiler:"2.0.0"
    implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"

    // Coroutines
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:"1.1.0"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:"1.1.0"

    // Navigation
    implementation "androidx.navigation:navigation-fragment-ktx:2.3.0-alpha04"
    implementation "androidx.navigation:navigation-ui-ktx:2.3.0-alpha04"

    // Testing
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}
于 2020-04-26T16:12:47.170 回答