Search code examples
androidgradlebuild.gradleandroidxandroid-safe-args

Unable to enable "safe args" as instructed in Google's official "Build Your First Android App" Tutorial due to missing sections


I have been following this tutorial:

https://developer.android.com/codelabs/build-your-first-android-app-kotlin

but I am stuck at this section which seems to not refer to reality at all:

https://developer.android.com/guide/navigation/use-graph/pass-data#Safe-args

It says:

"Open Gradle Scripts > build.gradle (Module: My First App). Find the dependencies section In the buildscript section, and add the following lines after the other classpath entries"

but I don't have a dependencies or buildscript section!!! This is what I have:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    id 'com.android.application' version '7.4.0' apply false
    id 'com.android.library' version '7.4.0' apply false
    id 'org.jetbrains.kotlin.android' version '1.7.21' apply false
}

It then says:

"Open Gradle Scripts > build.gradle (Module: app). Just below the other lines that begin with apply plugin add a line to enable SafeArgs:"

but I don't have any lines with apply plugin!!! This is what I have:

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

android {
    namespace 'com.example.myfirstapp'
    compileSdk 33

    defaultConfig {
        applicationId "com.example.myfirstapp"
        minSdk 24
        targetSdk 33
        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.6.1'
    implementation 'com.google.android.material:material:1.9.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'androidx.navigation:navigation-fragment-ktx:2.4.1'
    implementation 'androidx.navigation:navigation-ui-ktx:2.4.1'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}

How do I enable safe args? I tried following this guide:

https://developer.android.com/guide/navigation/use-graph/pass-data#Safe-args

but this also did not work ("unable to resolve class val"). Please can someone help me enable safe args. I'm trying to learn how to make an Android app and I only wish the instructions would actually be followable. :-(

I was trying to enable safe args and found that numerous sections that the official Google app tutorial assumed would exist did not exist.


Solution

  • please follow this answer of @fambrosioo. It's kinda same question.

    add in Gradle Scripts > build.gradle (Module: My First App):

    id 'androidx.navigation.safeargs.kotlin' version '2.6.0' apply false
    

    And in Gradle Scripts > build.gradle (Module: app), top of file add:

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