How to add classpath in new version of Android Studio

Viewed 8584

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 ?!

4 Answers

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'
}

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
      //......
    }

You dont need buildscript.You can add as 'id' inside plugin component.I was face with it in hilt entegration.Here is the sample Top level gradle

plugins {
    id 'com.android.application' version '7.2.2' apply false
    id 'com.android.library' version '7.2.2' apply false
    id 'com.google.dagger.hilt.android' version '2.43.2' apply false
}


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

And second level(project level gradle)

plugins {
    id 'com.android.application'
    id 'com.google.dagger.hilt.android'
}....


dependencies {

.....
    implementation 'com.google.dagger:hilt-android:2.43.2'
    annotationProcessor 'com.google.dagger:hilt-compiler:2.43.2'
}

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"

}

}

Related