7

I m gonna use Navigation Framework with fragments but at the dependency step, when i tried to add SafeArgs from documentation i see that the new top level gradle file is different from documentation so i can't add it. Can you explain how to add SafeArgs to new kotlin top level gradle file ?

My top level gradle file :

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} 


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

Documentation Top Level Gradle :

buildscript {
repositories {
    google()
}
dependencies {
    val nav_version = "2.4.1"
    classpath("androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version")
}}

Thanks in advance.

Sevban Bayır
  • 196
  • 3
  • 13

3 Answers3

19

just add below in your top level Gradle plugin block

id 'androidx.navigation.safeargs' version '2.4.2' apply false

then if your project is pure Kotlin, add below in your app Gradle

plugins {
  id 'androidx.navigation.safeargs.kotlin'
}

else (for pure Java or Java mixed Kotlin) add

plugins {
  id 'androidx.navigation.safeargs'
}
Neo Luk
  • 446
  • 4
  • 10
3
  1. Goto setting.gradle and copy past this code in PluginManagement block:

            plugins{
                id 'androidx.navigation'
            }
    
            resolutionStrategy {
    
                eachPlugin {
    
                   if (requested.id.id == 'androidx.navigation') {
                      useModule("androidx.navigation:navigation-safe-args-gradle-plugin:2.3.5")
                 }
                }
            }
    
  2. In build.gradle(Project) Level, goto plugins:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    id 'com.android.application' version '7.1.3' apply false
    id 'com.android.library' version '7.1.3' apply false
    id 'org.jetbrains.kotlin.android' version '1.6.20' apply false
    id 'androidx.navigation' version '2.3.5' apply false
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
  1. In build.gradle(:app) Level, goto plugins:

       plugins {
        id 'kotlin-kapt'
        id 'androidx.navigation.safeargs.kotlin'
    }
    
  2. add dependencies:

    // Kotlin
     implementation("androidx.navigation:navigation-fragment-ktx:2.4.2")
     implementation("androidx.navigation:navigation-ui-ktx:2.4.2")
    
1

Your top level (project level) Gradle file is in Groovy, while the documentation is in KotlinScript. You need to copy and paste the document code in your top level Gradle file with a few changes like below:

buildscript {
    repositories {
        google()
    }
    dependencies {
        nav_version = "2.4.1"
        classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
    }
}

Also, consider moving plugins definition to your app level Gradle file.

Sepehr1812
  • 66
  • 1
  • 5
  • Thank you. Why they did not specified that part ? I m wondering that how can i know that must be that way ? Is there any resources that ı can take knowledge about this topic ? Thank you again. – Sevban Bayır Mar 07 '22 at 05:29
  • You're welcome! https://developer.android.com/studio/build/migrate-to-kts may be a good and short article to help you know the main differences between KTS and Groovy. See https://docs.gradle.org/current/userguide/migrating_from_groovy_to_kotlin_dsl.html for a more detailed view. – Sepehr1812 Mar 08 '22 at 17:21