40

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

Dr.KeyOk
  • 608
  • 1
  • 6
  • 13

5 Answers5

57

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'
}
Liong
  • 1,324
  • 12
  • 18
  • Thanks man. but how can I find this ids ? for example dagger-hilt – Dr.KeyOk Mar 09 '22 at 16:28
  • 1
    https://developer.android.com/training/dependency-injection/hilt-android#setup From there the id was you put in `build.gradle` module, the id is `dagger.hilt.android.plugin`, hope it works. – Liong Mar 10 '22 at 07:53
  • i tryed but dagger not working – GvHarish Mar 30 '22 at 07:30
  • 7
    For Dagger Hilt, add `id 'com.google.dagger.hilt.android' version '2.41' apply false` to project build.gradle file and `id 'kotlin-kapt' id 'dagger.hilt.android.plugin'` to app build.gradle file as usual – feigo808 Apr 01 '22 at 00:58
  • 4
    @feigo808 where did you find this? – Badr At May 12 '22 at 23:03
24

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
      //......
    }
Michael Ndiritu
  • 292
  • 2
  • 4
  • Thanks, but show me this error : `Unable to load class 'com.android.build.api.extension.AndroidComponentsExtension.` ' – Dr.KeyOk Feb 01 '22 at 10:26
  • take a look at this SO [Solution](https://stackoverflow.com/questions/70857476/unable-to-load-class-androidcomponentsextension-after-upgrading-the-android-grad) – Michael Ndiritu Feb 01 '22 at 10:59
2

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'
}
Numan Turkeri
  • 526
  • 4
  • 18
1

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"

}

}

1

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

vinay shetty
  • 895
  • 1
  • 9
  • 15