1

How do I add safeargs and secrets as a plugin, or must it remain as dependency?

The latest Kotlin template for new fragment + view model build.gradle(Project) uses plugins {} not dependencies {} Here is my work around, how do convert a classpath with path:plugin:version plugin id.

buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.1.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10"
        // not compatible with upgraded gradle classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.4.0"
        //https://stackoverflow.com/questions/70857476/unable-to-load-class-androidcomponentsextension-after-upgrading-the-android-grad/70857477
        // Update this line to use 2.5.0-alpha01
        // While Navigation 2.4.1 is not out yet
        classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.5.0-alpha01"
        classpath "com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin:2.0.0"
    }
}
// buildscript MUST come before this these are used in the project build.gradle not a dependency
/* This came with the 'new project template' replaced with old style dependencies,
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
} */

The build.gradle for the module, is different, no version, so easy to convert.

2 Answers2

3

Assuming these repositories in settings.gradle:

pluginManagement {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
    }
}

One can replace the buildscript block with:

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.5.0-alpha02' apply false
    id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin' version '2.0.0' apply false
}

Maybe with or without that -gradle-plugin suffix.

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • This works for a new project. However my upgrade attempt on an existing project did not work. I did not discover what additional changes are required, but this solution answered my question. Thank you. – Shimon Rothschild Feb 28 '22 at 07:49
  • @ShimonRothschild The `buildscript` block may be conflicting; if one can migrate to `pluginManagement` depends on the plugins and plugin repositories used; in this [answer](https://stackoverflow.com/a/71135974/549372) I've explained it in more detail. – Martin Zeitler Feb 28 '22 at 14:26
0

Do not add repositories block to the buildscript in the project level build.gradle file. It should be in the project settings.gradle file.

Here's my work around:

  • build.gradle(Project)

     // Top-level build file where you can add configuration options common to all sub-projects/modules.
    buildscript {
    
     dependencies {
    
         // NOTE: Do not place your application dependencies here; they belong
         // in the individual module build.gradle files
         classpath "com.google.dagger:hilt-android-gradle-plugin:2.38.1"
         classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.5.0-alpha02"
     }
    }
    
    plugins {
     id 'com.android.application' version '7.1.1' apply false
     id 'com.android.library' version '7.1.1' apply false
     id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
    }
    
    task clean(type: Delete) {
     delete rootProject.buildDir
    }
    
  • settings.gradle(Project Settings) The repositories are declared here.

    pluginManagement {
     repositories {
         gradlePluginPortal()
         google()
         mavenCentral()
     }
    }
    dependencyResolutionManagement {
     repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
     repositories {
         google()
         mavenCentral()
     }
    }
    
    rootProject.name = "Dictionary App"
    include ':app'
    
  • Thank you, this might work in some scenarios, but my objective was to replace all classpath dependencies with plugin id. One important point you make is that order matters, pluginManagement needs to be before dependencyResolutionManagement. – Shimon Rothschild Feb 28 '22 at 07:53