How to add SafeArgs to new top level Kotlin gradle
Asked Answered
F

5

12

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.

Featly answered 6/3, 2022 at 17:22 Comment(0)
O
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.

Oops answered 6/3, 2022 at 21:5 Comment(2)
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.Matrimony
You're welcome! 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 docs.gradle.org/current/userguide/… for a more detailed view.Oops
V
29

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'
}
Venola answered 5/6, 2022 at 14:10 Comment(0)
D
8

At top build.gradle.kts

alias(libs.plugins.androidx.navigation.safeargs) apply false

libs.versions.toml

    androidx-navigation = "2.7.7"
    [plugins]
    androidx-navigation-safeargs = { id = "androidx.navigation.safeargs.kotlin", version.ref = "androidx-navigation" }

build.gradle.kts at module app

alias(libs.plugins.androidx.navigation.safeargs)
Decarlo answered 1/4 at 23:18 Comment(0)
G
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")
    
Graupel answered 24/4, 2022 at 6:1 Comment(2)
Hilt already has a plugin marker artifact published so you don't need a resolutionStrategy for it.Haft
This gradle build scripts for android each year going to be more and more crazy. I miss AntEleanoraeleanore
O
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.

Oops answered 6/3, 2022 at 21:5 Comment(2)
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.Matrimony
You're welcome! 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 docs.gradle.org/current/userguide/… for a more detailed view.Oops
S
0

(Hedgehog version)

Just two steps

  1. in build.gradle.kts (Project <appName>), add this line in plugin block:

     plugins {
         id("androidx.navigation.safeargs") version "2.5.3" apply false
     }
    

Note: last version till now is 2.7.7 but I use 2.5.3 as it's stable one

  1. in build.gradle.kts (Module :app), add this line in plugins block:

     plugins {
         id("androidx.navigation.safeargs.kotlin") // for pure kotlin
         id("androidx.navigation.safeargs") // for java or mixed java and kotlin
     }
    
Stubble answered 22/2 at 1:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.