The use of safeArgs in Android Studio is causing an error and I cannot build the project

Viewed 2034

I am trying to build my project in Kotlin and I am getting this error:

Execution failed for task ':app:generateSafeArgsDebug'.

Could not read 'F:\Program Files\Android Projects\CityOnApp\crowdapps-mobile-android\app\build\intermediates\metadata_application_id\debug\application-id.txt' as it does not exist.

and when I added in Settings > Build, Execution, Deployment > Compiler > Command Line Options : --stacktrace --debug -Pandroid.debug.obsoleteApi=true, then I got : [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] Execution failed for task ':app:generateSafeArgsDebug'.

Could not read 'F:\Program Files\Android Projects\CityOnApp\crowdapps-mobile-android\app\build\intermediates\metadata_application_id\debug\application-id.txt' as it does not exist.

I checked the path and the file does not exist there! I have the latest version in Kotlin and Android Studio. What I tried and did not work is :

  • clean build and rebuild/ build
  • downgrading the version of Android Studio
  • deleting the .gradle file from the root directory
4 Answers
plugins {
id 'com.android.application' version '7.1.0' apply false
id 'com.android.library' version '7.1.0' apply false
id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
id 'androidx.navigation.safeargs.kotlin' version '2.4.1' apply false
}

To add Safe Args to your project, include the following classpath in your top level build.gradle file:

buildscript {

repositories {
    google()
  }
dependencies {
    def nav_version = "2.3.4"
    classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
  }
}

You must also apply one of two available plugins.

To generate Java language code suitable for Java or mixed Java and Kotlin modules, add this line to your app or module's build.gradle file:

apply plugin: "androidx.navigation.safeargs"

Alternatively, to generate Kotlin code suitable for Kotlin-only modules add:

apply plugin: "androidx.navigation.safeargs.kotlin"

You must have android.useAndroidX=true in your gradle.properties file as per Migrating to AndroidX.

Source : Official Documentation

Try upgrading/downgrading the gradle plugin.

classpath 'com.android.tools.build:gradle:x.x.x'

Try upgrading/dowgrading gradle wrapper properties

distributionUrl=https\://services.gradle.org/distributions/gradle-x.x.x-bin.zip

Try invalidating caches in Android Studio through the file menu.

Try running Android Studio in admin/sudo.

Check your (git/local)history to see if anything changed that could cause this.

If all else fails, reinstall Android Studio and freshly import your project entirely.

To add safe-args add this dependencies to build.gradle(project) before plugins{}block on the top

buildscript {
repositories {
    google()
}
dependencies {
    classpath("androidx.navigation:navigation-safe-args-gradle-plugin:2.4.2")
}}

this to the build.gradle(Module) file inside plugin{}block

id("androidx.navigation.safeargs.kotlin")

After adding Rebuild the project this will implement the safe-args

Related