Dr.KeyOk
Dr.KeyOk

Reputation: 768

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

Upvotes: 50

Views: 41111

Answers (5)

Hassan El Sayed Ammer
Hassan El Sayed Ammer

Reputation: 137

So they mentioned that in the build.Gradle file as top-level comment And I saw it as a bad practice don't mention it below 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"

}

}

Upvotes: 5

vinay shetty
vinay shetty

Reputation: 981

We can add class path directly in the top Level Build.gradle file as follows.

Code enter image description here

buildscript {
    repositories {
        google()
    }
    dependencies {
        def nav_version = "2.5.3"
        classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
    }
}
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    id 'com.android.application' version '7.3.1' apply false
    id 'com.android.library' version '7.3.1' apply false
    id 'org.jetbrains.kotlin.android' version '1.7.20' apply false
}

This is sample for adding safe args for Navigation component as suggested in the Documentation

Upvotes: 1

Numan Turkeri
Numan Turkeri

Reputation: 536

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

Upvotes: 3

Liong
Liong

Reputation: 1564

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

Upvotes: 75

Michael Ndiritu
Michael Ndiritu

Reputation: 302

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

Upvotes: 25

Related Questions