I am starting a new project by using the architecture-templates by google (https://github.com/android/architecture-templates)
In this template, they use Gradle with Kotlin DSL. I am trying to add Crashlytics to this project but the structure of gradle is quite different from my old projects.
I am stuck on the step 2 of the base guide (Firebase Get Started Documentation)
Error resolving plugin [id: 'com.android.application', version:
'7.3.1']
The request for this plugin could not be satisfied because the plugin is already on the classpath with an unknown version, so
compatibility cannot be checked.
Any suggestion?
After some research I found that the architecture template is based on gradle 7.6 which use the version catalog feature.
So I based my Version Catalog file on this https://github.com/RedMadRobot/gradle-version-catalogs/blob/main/versions-stack/libs.versions.toml
Now my build.gradle.kts file is
plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.kotlin.android)
alias(libs.plugins.kotlin.kapt)
alias(libs.plugins.hilt.gradle)
alias(libs.plugins.firebase.crashlitycs)
alias(libs.plugins.gms.googleServices)
}
....
dependencies {
implementation(platform(libs.firebase.bom))
implementation(libs.firebase.crashlytics)
implementation(libs.firebase.analytics)
}
I used to be confused about this too, but after some tries, I found the correct answer.
I think this is the first point of step 2 where you are confused. Just add the following code at the top of the project level build.gradle:
buildscript {
dependencies {
classpath 'com.google.gms:google-services:4.3.14'
}
}
Just follow the Google guide for the rest.
Complete code:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
dependencies {
classpath 'com.google.gms:google-services:4.3.14'
}
}
plugins {
id 'com.android.application' version '7.2.2' apply false
id 'com.android.library' version '7.2.2' apply false
id 'org.jetbrains.kotlin.android' version '1.7.10' apply false
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Edited:
DSL version:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
dependencies {
classpath("com.google.gms:google-services:4.3.14")
classpath("com.google.firebase:firebase-crashlytics-gradle:2.9.2")
}
}
plugins {
id("com.android.application") version "7.2.2" apply false
id("com.android.library") version "7.2.2" apply false
id("org.jetbrains.kotlin.android") version "1.7.10" apply false
}
tasks.register("clean", Delete::class) {
delete(rootProject.buildDir)
}
This website(Gradle Kotlinize - Groovy to Kotlin converter online) may be able to help you.
Related
I am currently updating my project and as one of the steps I am changing gradle files to use the plugins { id 'xxx' } way instead of the legacy apply plugin 'xxx' approach. I was able to migrate most of the imports to the new format, however I cannot add some plugins, as I am unable to find their gradle plugin ids.
For example, here are my old gradle files:
settings.gradle file
include ':app'
project's build.gradle file
buildscript {
repositories {
google()
mavenCentral()
(...)
}
dependencies {
(...)
classpath 'com.google.firebase:firebase-crashlytics-gradle:2.9.0'
classpath 'com.google.android.gms:oss-licenses-plugin:0.10.5'
}
}
(...)
module's build.gradle file
apply plugin: 'com.android.application'
apply plugin: 'com.google.firebase.crashlytics'
apply plugin: 'com.google.android.gms.oss-licenses-plugin'
(...)
And here are partially modified new gradle files:
settings.gradle file
pluginManagement {
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
}
rootProject.name = "xxxx"
include ':app'
project's build.gradle file
plugins {
id 'com.android.application' version '7.3.1' apply false
id 'com.google.firebase.crashlytics' version '2.9.2' apply false
// DOESN'T WORK:
id 'com.google.android.gms.oss-licenses-plugin' version '0.10.5' apply false
}
(...)
module's build.gradle file
plugins {
id 'com.android.application'
id 'com.google.firebase.crashlytics'
// NEED TO SET SAME ID AS IN PROJECT'S GRADLE FILE PROBABLY:
id 'com.google.android.gms.oss-licenses-plugin'
(...)
}
Problem lays in how to get gradle plugin id for given plugin?
Many plugin installation instructions use the old apply plugin approach and I don't want to mix both of them.
For example in case of Crashlytics with classpath of com.google.firebase:firebase-crashlytics-gradle, the id is com.google.firebase.crashlytics - how was I supposed to know that? I found this in one of the answers on Stackoverflow, but without information about how someone knew that.
Currently I am trying to add the oss-licenses-plugin and I am completly clueless as about how to find its gradle plugin id...
Any suggestions?
Or maybe it is not guaranteed that every plugin added with use of classpath can be translated to the new plugins { } way? In this case, how can I tell it is this situation?
There's a certain pattern that plugin publishers need to follow in order for Gradle to find the plugin implementation. A properties file needs to be included in the JAR's META-INF/gradle-plugins directory. And the name of this file needs to be formatted in the following way:
<plugin-id>.properties
And inside this file, the plugin implementation is defined:
implementation-class=<com.example.SomePluginImpl>
So, to answer your question, you'll need to get a hold of the JAR & look at its contents to figure out the id: META-INF/gradle-plugins/<plugin-id>.properties.
For com.google.android.gms.oss-licenses-plugin, I checked here: Link. Alternatively, you can grab the JAR from maven, extract its contents & check the properties file: Link.
The other issue with your code is that for external plugin resolution, you need to define a resolutionStrategy under pluginManagement:
pluginManagement {
resolutionStrategy {
eachPlugin {
if (requested.id.id == 'com.google.android.gms.oss-licenses-plugin') {
useModule "com.google.android.gms:oss-licenses-plugin:${requested.version}"
}
}
}
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
}
Now you should be able to use id 'com.google.android.gms.oss-licenses-plugin' version '0.10.5' apply false in your project-level build.gradle.
according to gradle docs, you can find the plugin registered in here
not all gradle plugin available, because the dev need to publish their plugin to gradle first, here how to publish the plugin https://plugins.gradle.org/docs/publish-plugin
edit :
I tried to find oss-licenses-plugin, no luck with that. So you should use the old method to apply the plugin.
other info : What the difference in applying gradle plugin
I want to migrate build.gradle file from Groovy to Kotlin, since I updated my Android Studio to Bumblebee, I'm not sure how to do it.
This is how build.gradle looks when I create a new project.
I'm not sure on how to migrate plugins{} part.
buildscript {
ext {
compose_version = '1.0.1'
}
}
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.21' apply false
id 'org.jetbrains.kotlin.jvm' version '1.6.21' apply false
}
task clean(type: Delete) {
delete rootProject.buildDir
}
I got the answer in the documentation here https://developer.android.com/studio/build#kts.
Here's how it's written in Kotlin.
plugins {
id("com.android.application") version "7.1.0-beta02" apply false
id("com.android.library") version "7.1.0-beta02" apply false
id("org.jetbrains.kotlin.android") version "1.5.30" apply false
}
tasks.register("clean", Delete::class) {
delete(rootProject.buildDir)
}
The following steps worked for me.
Step-1: Create the plugins tag and give it the id values. delete the buildscript tag.
Step-2: Open the pluginManagement tag in the settings.gradle file and make some tags and values.
Step-3: In the last step, update the values defined at the top in the build.gradle file as id.
Optional: Since it is in the build.gradle file, it must be deleted.
repositories {
mavenCentral()
}
I setup a new (Kotlin) project using Android Studio. When I take a look at my build.gradle (Project), I see this content:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
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
}
However, other build.gradles seem to be completely different.
I searched all files, but couldn't find any occurrences of the buildscript, dependencies or any other block.
Where are they or how do I add them?
buildscript was used to add the version of dependencies and classpaths. In the newer versions of android studio you have to mention the version directly after the dependency implementations.
So for example, I wanted to add the classpath "androidx.navigation:navigation-safe-args-gradle-plugin" to my build.gradle(Project) for the plugin id "androidx.navigation.safeargs.kotlin" , but in the newer version it has been moved to settings.gradle and is done by adding a resolutionStrategy block just after the repositories block: -
pluginManagement {
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
resolutionStrategy {
eachPlugin {
if (requested.id.id == 'androidx.navigation.safeargs.kotlin') {
useModule("androidx.navigation:navigation-safe-args-gradle-plugin:2.4.2")
}
}
}
}
after that, you can add the plugin id "androidx.navigation.safeargs.kotlin" in plugin block in the build.gradle(Module) and sync it: -
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
id 'androidx.navigation.safeargs.kotlin'
}
Hope this is helpful!!
There are two build.gradle files in android studio i.e. build.gradle(Project) & build.gradle(Module).
Required blocks like buildscript, dependencies are available in build.gradle(Module)
build.gradle(Module) looks like:
Hope this will help :)
Repositories have been moved to settings.gradle and module Gradle has been moved to the app package gradle.
So I'm trying to add AppGallery connect gradle plugin to my android project using new Kotlin DSL syntax. But I'm getting error like this:
org.gradle.internal.exceptions.LocationAwareException: Build file 'D:\#KERJAAN\devbase\sample\build.gradle.kts' line: 3
Plugin [id: 'com.huawei.agconnect', version: '1.6.3.300'] was not found in any of the following sources:
- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (could not resolve plugin artifact 'com.huawei.agconnect:com.huawei.agconnect.gradle.plugin:1.6.3.300')
Searched in the following repositories:
Gradle Central Plugin Repository
Google
MavenRepo
maven(https://developer.huawei.com/repo/)
What i did was adding repository for plugin like this in settings.gradle.kts:
pluginManagement {
repositories {
gradlePluginPortal()
google()
mavenCentral()
maven { setUrl("https://developer.huawei.com/repo/") }
}
}
And adding the plugin like this in app's build.gradle.kts:
plugins {
id("com.huawei.agconnect") version "1.6.3.300"
}
Interestingly, It works if using classpath from the root build.gradle.kts. Does anyone know why?
Any Gradle plugin (this is not AGC specific at all) can only be loaded at the root project level, and then usually be applied on the module level. I've just tried to remove the buildscript block (alike in the question), which indeed leads to:
Plugin [id: 'com.huawei.agconnect', version: '1.7.3.302', apply: false] was not found in any of the following sources:
maven(https://developer.huawei.com/repo/)
Plugin Repositories (could not resolve plugin artifact 'com.huawei.agconnect:com.huawei.agconnect.gradle.plugin:1.7.3.302')
The plugin dependency won't resolve, while the pluginManagement keeps adding .gradle.plugin. If the repository would know about the full and not only the shorthanded name agcp, this should work out of the box (that's actually the default expected package name, unless changing it):
com.huawei.agconnect:com.huawei.agconnect.gradle.plugin:1.7.3.302
And this doesn't match:
com.huawei.agconnect:agcp:1.7.3.302
One can use pluginManagement.resolutionStrategy as a temporary workaround ...
The settings.gradle is being used to rewrite the wrongfully assumed package name:
pluginManagement {
repositories {
gradlePluginPortal()
mavenCentral()
google()
maven { url 'https://developer.huawei.com/repo/' }
}
plugins {}
resolutionStrategy {
eachPlugin {
if (it.requested.id.getNamespace() == 'com.huawei.agconnect') {
println ">> ${it.requested.id.id}"
if (it.requested.id.id == 'com.huawei.agconnect.agcp') {
it.useModule('com.huawei.agconnect:agcp:1.7.3.302')
}
if (it.requested.id.id == 'com.huawei.agconnect.apms') {
it.useModule('com.huawei.agconnect:agconnect-apms-plugin:1.6.1.300')
}
println ">> ${it.target}"
} else {
println "> ${it.target}"
}
}
}
}
plugins have to be defined in build.gradle:
plugins {
id "com.android.application" version "7.3.1" apply false
id "com.android.library" version "7.3.1" apply false
id "com.huawei.agconnect.agcp" version "1.7.3.302" apply false
id "com.huawei.agconnect.apms" version "1.6.1.300" apply false
}
println will output the updated (fake) id to artifact mapping it.target:
[
id: 'com.huawei.agconnect.agcp',
version: '1.7.3.302',
artifact: 'com.huawei.agconnect:agcp:1.7.3.302',
apply: false
]
When applying it, one still needs to use the real id:
apply plugin: 'com.huawei.agconnect'
It is just that (as of version 1.7.3.302) APMSTransform has some check in place, which requires to explicitly put AGP on classpath. The buildscript block is "almost" obsolete, if not APMSTransform would wrongfully assume, that it is the only place where the Android Gradle plugin can be loaded.
/** Still required due to AGCP plugin. */
buildscript {
repositories {
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.3.1'
}
}
It would also need to check for either of these plugins:
plugins {
id "com.android.application" version "7.3.1" apply false
id "com.android.library" version "7.3.1" apply false
}
For example:
project.getPluginManager().hasPlugin('com.android.application') || project.getPluginManager().hasPlugin('com.android.library')
In order to make this work flawlessly (without resolutionStrategy), this would require an updated check, in order not to get com.android.tools.build:gradle is no set in the build.gradle file and
also an URL rewrite, which would handle the package name's .gradle.plugin suffix properly, so that com.huawei.agconnect.gradle.plugin and agcp would result in the same package download. resolutionStrategy indeed is the workaround and not the answer.
Don't use
plugins {
id("com.huawei.agconnect") version "1.6.3.300"
}
PLS old way like use
dependencies {
// 增加agcp插件配置,推荐您使用最新版本的agcp插件。
classpath 'com.huawei.agconnect:agcp:1.6.3.300'
// important add next line , huawei dependencies agp.
classpath 'com.android.tools.build:gradle:7.1.2'
}
I'm creating a new application in Android Studio Bumblebee and this defaults to using the new Groovy DSL plugin management in settings.gradle.
I need to be able to use Google Play Services to enable Firebase functionality, however I am running into a build error when applying the com.google.gms.google-play-services plugin using the documentation here: Google Play Services Readme
I have added the following to my settings.gradle file:
pluginManagement {
repositories {
gradlePluginPortal()
google()
}
plugins {
id 'com.android.application' version '7.1.0-alpha13'
id 'com.android.library' version '7.1.0-alpha13'
id 'org.jetbrains.kotlin.android' version '1.5.31'
id 'com.google.gms.google-services' version '4.3.10'
}
}
and the following to my app's build.gradle file:
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'com.google.gms.google-services'
}
however when I build the application, I get the following UnknownPluginException:
Plugin [id: 'com.google.gms.google-services', version: '4.3.10'] was not found in any of the following sources:
* Try:
Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Exception is:
org.gradle.api.plugins.UnknownPluginException: Plugin [id: 'com.google.gms.google-services', version: '4.3.10'] was not found in any of the following sources:
- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (could not resolve plugin artifact 'com.google.gms.google-services:com.google.gms.google-services.gradle.plugin:4.3.10')
Searched in the following repositories:
Gradle Central Plugin Repository
Google
I have also tried the legacy method of classpath etc. but this results in a much longer error message regarding dependency resolution.
I'm not sure what I am doing wrong.
Adding the google-services plugin to the plugins {} block is causing errors. The alternate way that I've found is:
First, in your root build file (not the one in the app folder), inside the buildscript {} block, add this
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.google.gms:google-services:4.3.10'
}
}
In the build file in the app folder, apply the plugin like this,
apply plugin: 'com.google.gms.google-services'
In step 1, using mavenCentral() is necessary as the google-services plugin downloads the linked dependencies like gson from maven central :)
By using Android Studio Bumblebee new Groovy DSL plugin management Add below line in build.gradle(Project:) plugins{} section and sync project will solve your problem.
id 'com.google.gms.google-services' version '4.3.10' apply false
your build.gradle(Project:) looks like
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
id 'com.google.dagger.hilt.android' version '2.41' apply false
id 'com.google.gms.google-services' version '4.3.10' apply false
id 'com.google.firebase.crashlytics' version '2.8.1' apply false
}
task clean(type: Delete) {
delete rootProject.buildDir
}
your build.gradle(Module:) looks like
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'org.jetbrains.kotlin.kapt'
id 'com.google.dagger.hilt.android'
id 'com.google.gms.google-services'
id 'com.google.firebase.crashlytics'
}
android {
compileSdk 32
signingConfigs {
config {
enableV3Signing true
enableV4Signing true
}
}
}
your settings.gradle(Project Settings) looks like
pluginManagement {
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
}
rootProject.name = "MyApp"
include ':app'