javaandroidbuild.gradleandroid-navigationandroid-safe-args

SafeArgs does not work anymoe after Android Studio update


I have an app that has been working properly for many years and I also installed it on several phones and tablets without any problems. Unfortunately I mad an update of Android Studio (from Flamingo 2022 to ALadybug Feature Drop | 2024.2.2) and now all of a sudden, the app does not compile anymore.

One source of erros messages comes from using SafeArgs and directions. So for example in the code

NavController navController = Navigation.findNavController(requireActivity(), R.id.navHostfragment);
        viewModel.setPastTimeMillis(pastDaysForDisplayingScores);
        liveData.forceUpdate();

        //Repeat the level if the repeat button is pressed
        if (view.getId() == R.id.imageView_RepeatSymbol) {
            navController.navigate(DialogFR_LevelEndDirections.actionDialogFRLevelEndToFRGame());
        }

I get the error Cannot resolve symbol 'DialogFR_LevelEndDirections' and I get this in every class where I use safeargs. The generated classes look okay as far as I see it for example

package com.example.game

import androidx.navigation.ActionOnlyNavDirections
import androidx.navigation.NavDirections

public class DialogFR_LevelEndDirections private constructor() {
  public companion object {
    public fun actionDialogFRLevelEndToFRGame(): NavDirections =
        ActionOnlyNavDirections(R.id.action_dialogFR_LevelEnd_to_FR_Game)

    public fun actionDialogFRLevelEndToFRMenu(): NavDirections =
        ActionOnlyNavDirections(R.id.action_dialogFR_LevelEnd_to_FR_Menu)
  }
}

So something in the connection between the classes and the generated safeargs does not work anymore. Here are my build.gradle file

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'androidx.navigation.safeargs.kotlin'
}

android {
    namespace 'com.example.game'

    compileSdk = 34

    defaultConfig {
        applicationId "com.example.game"
        minSdk = 24
        targetSdk = 34
        versionCode = 1
        versionName = "1.0"

        viewBinding {
            enabled = true
        }

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

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

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }

    kotlinOptions {
        jvmTarget = '17'
    }
}

dependencies {
    implementation 'androidx.core:core-ktx:1.10.0'
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.8.0'
    implementation "androidx.constraintlayout:constraintlayout:2.1.4"
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.gridlayout:gridlayout:1.0.0'

    testImplementation 'junit:junit:4.13.2' // Fixed empty JUnit version
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'

    implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
    implementation "androidx.navigation:navigation-ui-ktx:$nav_version"

    implementation platform('com.google.firebase:firebase-bom:32.0.0')
    implementation 'com.google.firebase:firebase-database-ktx'

    implementation platform("org.jetbrains.kotlin:kotlin-bom:1.8.0")
}

and

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    ext.nav_version = '2.7.0' // Use the latest Navigation version

    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:8.1.1' // Use the latest stable AGP
        classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"

        classpath 'com.google.gms:google-services:4.4.0' // Check the latest version
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

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

Can you imagine what the problem might be? One thing that I want to mentione is that I am only coding in Java and dont use Kotlin at all. Still in the gradle files kotlin is mentioned in the plugins and the generated classes appear to be generated in Kotlin. But I don't know if this is a problem or not because the app had been working for 2 years abut the update just crashed everything.

Update: Based on the answer of Thogaruchesti I changed to id 'androidx.navigation.safeargs' in the build.gradle module file from the kotlin version (as I don't actively use any Kotlin code in my app but only java). Actually now the app compiles and is started again without any errors. HOWEVER: There is a new issue now. The app does not react to any touch event with OnClickListener. Further, I tried different emulators and different starting fragments, and the app does not react to any UI event like scrolling, switch toggle buttons, clicking on links etc. Here is also my gradle-wrapper properties file. I don't know at all if this is related or not but I think something is wrong with all the gradle setups; I randomly tried different gradle versions but so far none worked :

#Mon Jan 27 15:03:40 CET 2025
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

Reminder: My bounty is expiring quite soon and unforunately I still could not solve this issue altough having tried many things. I think there is something wrong with the combination of the 2 build.gradle files, the wrapper.properties files and maybe the java version (here you see the screenshot from the Gradle JDK versions I can choose from):

enter image description here

Does anyone have an idea how to finally solve this problem?


Solution

  • The code is in kotlin and you using the compile options as java correct it and if you using java only make sure the Use Safe Args for Java Instead of Kotlin 'androidx.navigation.safeargs'. update the navigation to the latest version 2.7.7 and try invalid cache and restart once and build it again .