'How to add classpath in new version of Android Studio

I updated my android studio version to bumblebee version.
Now I want add navigation component to my project.
I want add classpath to gradle, but this file gradle has been changed and I don'y know how can I add this.

I want add this classpath("androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version") to gradle files!

My project gradle file is :

    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
}

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

How can I add this classpath to application gradle ?!



Solution 1:[1]

They haven't talked about in the release docs but manually add a buildscript block above the plugins block then inside the buildscript block add a depedencies block.

like this:

buildscript {

        dependencies {

              classpath("androidx.navigation:navigation-safe-args-gradle-plugin:2.4.0")

                     }
       }


plugins {
      id 'com.android.application' version '7.1.0-rc01' apply false
      //......
    }

Solution 2:[2]

In the latest version in February 2022 doesn't need to add buildscript anymore just add the id in build.gradle for project. It will be like this in build.gradle for project:

plugins {
    id 'com.android.application' version '7.1.2' apply false
    id 'com.android.library' version '7.1.2' apply false
    id 'org.jetbrains.kotlin.android' version '1.6.10' apply false

    id 'androidx.navigation.safeargs' version '2.4.1' apply false
    // classpath are changed, so no need to add classpath anymore just the id and the version
}

Don't forget add the id again in the build.gradle module,

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

Solution 3:[3]

So they mentioned that in the build.Gradle file as top-level comment And I saw it as a bad practice by don't mention it floow that
1- open your build.Gradle (app module) scroll to the top until you see that comment // Top-level build file where you can add configuration options common to all sub-projects/modules.

then take this code As copy put it this line isenter image description here builtin comment // Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {

dependencies {

    classpath "androidx.navigation:navigation-safe-args-gradle-plugin:lasted-version"

}

}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Michael Ndiritu
Solution 2 Liong
Solution 3 Hassan Ammer