5

I updated Android Studio and now in top level build.gradle there is no dependencies scope, instead there is plugins scope. And I want to add the dependency for navigation safe args. In old versions, I was able to add like:

dependencies {
    def nav_version = "2.3.5"
    classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
}

But now we have plugins scope.

plugins {
id 'com.android.application' version '7.2.0-alpha02' apply false
id 'com.android.library' version '7.2.0-alpha02' apply false
id 'org.jetbrains.kotlin.android' version '1.5.31' apply false }

And I added safe-args plugin to this scope

id "androidx.navigation:navigation-safe-args-gradle-plugin" version "2.3.5" apply false

, but I get this error:
plugin id 'androidx.navigation:navigation-safe-args-gradle-plugin' is invalid: Plugin id contains invalid char ':'

4 Answers4

9

You can do this thing.

ext {
    compose_version = '1.0.5'
    kotlin_version = '1.6.10'
}
// Top-level build file where you can add configuration options common to all sub-projects/modules.
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 "$kotlin_version" apply false
    id "com.google.dagger.hilt.android" version "2.41" apply false
    id 'androidx.navigation.safeargs.kotlin' version '2.5.0-alpha01' apply false // use this one
//    classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.4.0" (will not work now)

}

task clean(type: Delete) {
    delete rootProject.buildDir
}
Nafis Kabbo
  • 568
  • 5
  • 8
  • 2
    `2.4.0` gives me error, `2.5.0-alpha01` works for me: https://stackoverflow.com/a/70857477/3466808 – Sam Chen Feb 06 '22 at 15:58
  • Updated Answer for Hilt and Version Variable. For adding hilt, still you have to add in dependencies block and you can use a variable value like kotlin version in plugins also like this answer. You have to use "" instead of ''. – Nafis Kabbo Feb 06 '22 at 16:03
  • 1
    you can use hilt directly Updated answer – Nafis Kabbo Feb 19 '22 at 16:21
  • How can someone find "plugin id" for other classpath which may needed to add? – Mitul Varmora Apr 05 '22 at 09:16
2

I had the same problem.

What I did was add the dependencies wrapper inside the buildscript wrapper and it worked for me.

Example:

buildscript {
    ext {
        compose_version = '1.0.5'
    }
    dependencies {
        classpath "com.google.dagger:hilt-android-gradle-plugin:2.38.1"
    }
}// Top-level build file where you can add configuration options common 
    to all sub-projects/modules.
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.5.31' apply false
    id "com.google.dagger.hilt.android" version '2.41' apply false
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
Nafis Kabbo
  • 568
  • 5
  • 8
0

Add a buildscript group to the top of the build.gradle(Project) file.

buildscript {
    dependencies {
        classpath("androidx.navigation.safeargs:androidx.navigation.safeargs.gradle.plugin:2.4.1")
    }
}

Then add the plugin ID to the plugins group in the build.gradle(Module) file.

id 'androidx.navigation.safeargs'

Make sure the line android.useAndroidX=true is in gradle.properties.

That will do it.

Phil
  • 1,030
  • 1
  • 16
  • 22
-1
  1. Open build.gradle(Module:app)
  2. Use Strg+Alt+Shift+S to open Project Structure dialog
  3. Search for androidx.navigation
  4. Select androidx.navigation:navigation-safe-args-gradle-plugin:2.4.0-rc01
  5. Apply and wait for sync
Zoe
  • 27,060
  • 21
  • 118
  • 148