Android SafeArgs Unresolved Reference

Viewed 568

I can't seem to be able to get SafeArgs to work:

My XML navigation file contains:

<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>

This is because I need the argument to be false by default and true when coming from a specific fragment. Anyway, my Kotlin code is the following:

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

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

Which is fine because when writing code Android Studio recognizes the binding and even suggests me the (only) variable present. However, when building the app I get:

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

I've read the documentation many times so I think my gradle is ok, but anyway here is the project level 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
}

and the app level 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'

}

I've also tried reopening Android Studio, reopening the project, clean-building the project and rebuilding the project with no luck. Any help is appreciated.

2 Answers

change in the app gradle.build from apply plugin: 'androidx.navigation.safeargs.kotlin' to

apply plugin: 'androidx.navigation.safeargs'

I had the same problem and it helped me

this worked for me too. Super stoked about it working now. My problem was using nested custom enums which did not work when using the Kotlin plugin, even though I only use Kotlin.

Related