PeterBe
PeterBe

Reputation: 830

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.

Reminder: Does anyone have an idea? I have been trying a lot since the update that broke everything but have not solve the problem. I tried to use different gradle versions but this does not solve the problem.

Upvotes: 1

Views: 108

Answers (1)

Martin Zeitler
Martin Zeitler

Reputation: 76569

These classes are not being generated at the right time and therefore it's easier to navigate by ID & arguments Bundle. I've once wrote a workaround in Groovy, which de-registers and registers the task which generates these NavDirections classes (at the right time, with product-flavor support even), but it's basically "useless complexity" because one does not really need them.

https://developer.android.com/guide/navigation/use-graph/navigate#id

Upvotes: 0

Related Questions