Android Studio New Project Template Uses Plugins not Dependencies

Viewed 726

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 Answers

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.

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'
    
Related