gradle Android SafeArgs未解析的引用

smtd7mpg  于 2022-11-14  发布在  Android
关注(0)|答案(1)|浏览(186)

我似乎无法让SafeArgs工作:
我的XML导航文件包含:

<fragment
        android:id="@+id/nav_adv_list"
        android:name="com.example.drawerexample.ui.AdvListFragment"
        android:label="@string/menu_show_adv_list"
        tools:layout="@layout/fragment_adv_list" >
        <argument
                android:name="onlyUserAdvs"
                app:argType="boolean"
                android:defaultValue="true" />
        ...
     <action
            android:id="@+id/nav_adv_list_to_my_adv_list"
            android:name="com.example.drawerexample.ui.AdvListFragment"
            app:destination="@id/nav_adv_list">
            <argument
                android:name="onlyUserAdvs"
                app:argType="boolean"
                android:defaultValue="true" />
        </action>
</fragment>

这是因为我需要参数在默认情况下是false,而在来自一个特定片段时是true

class AdvListFragment : Fragment(), DatePickerDialog.OnDateSetListener {
      ...
      private val fragmentArgs : AdvListFragmentArgs by navArgs()

     fun useArgs() {
         val x = arguments.onlyUserAdvs
     }
}

这很好,因为在编写代码时,Android Studio会识别绑定,甚至会向我建议(唯一的)变量。然而,在构建应用时,我会得到:

.../ui/AdvListFragment.kt: (54, 63): Unresolved reference: onlyUserAdvs

我已经阅读了很多次文档,所以我认为我的gradle是可以的,但无论如何,这里是项目级build.gradle:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        google()
    }

    dependencies {
        classpath 'com.google.gms:google-services:4.3.10'
        classpath("androidx.navigation:navigation-safe-args-gradle-plugin:2.4.2")
    }
}

plugins {
    id 'com.android.application' version '7.2.0' apply false
    id 'com.android.library' version '7.2.0' apply false
    id 'org.jetbrains.kotlin.android' version '1.6.21' apply false
}

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

和应用程序级别gradle.build:

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'kotlin-android'
    id 'kotlin-kapt'
}

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'androidx.navigation.safeargs.kotlin'

android {
    compileSdk 32

    defaultConfig {
        applicationId "com.example.drawerexample"
        minSdk 21
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    buildFeatures {
        viewBinding true
    }
}

dependencies {
    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'com.google.android.material:material:1.6.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
    implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.4.1'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1'
    implementation "androidx.navigation:navigation-fragment-ktx:2.4.2"
    implementation "androidx.navigation:navigation-ui-ktx:2.4.2"
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation platform('com.google.firebase:firebase-bom:30.0.0')
    implementation 'com.google.firebase:firebase-analytics-ktx:21.0.0'
    implementation 'com.google.android.gms:play-services-auth:20.2.0'
    implementation 'com.google.firebase:firebase-firestore-ktx:24.1.2'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    implementation 'com.google.firebase:firebase-auth-ktx'
    implementation 'com.google.firebase:firebase-storage-ktx'

}

我也尝试过重新打开Android Studio,重新打开项目,清理构建项目,重建项目,但没有成功。

yiytaume

yiytaume1#

应用程序www.example.com中gradle.build的应用插件更改:“androidx.导航.安全参数.Kotlin”到

apply plugin: 'androidx.navigation.safeargs'

我也有同样的问题,它帮助了我

相关问题