How to find gradle plugin id for given gradle library? - android

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

Related

Explain new format of top level gradle file

Few days ago I have update my android studio to new version (Bumblebee) after that I create new project and see top leve gradle file totally changed and I don't understand new format.
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
}
can anyone explain me what is mean of apply false here
and also how I can add
repositories {
maven { url 'https://jitpack.io' }
}
in top level gradle according to new format
build.gradle
plugins {
1. `«plugin id»`
2. id(«plugin id»)
3. id(«plugin id») version «plugin version» [apply «false»]
}
for core Gradle plugins
for core Gradle plugins or plugins already available to the build script
for binary Gradle plugins that need to be resolved
Where «plugin id», in case #1 is a static Kotlin extension property, named after the core plugin ID ; and in cases #2 and #3 is a string. «plugin version» is also a string. The apply statement with a boolean can be used to disable the default behavior of applying the plugin immediately (e.g. you want to apply it only in subprojects).
And You have to add repositories inside pluginManagement in setting.gradle
Example :
pluginManagement {
repositories {
maven(url = "./maven-repo")
gradlePluginPortal()
ivy(url = "./ivy-repo")
}
}
You can refer here for more info

com.huawei.agconnect plugin not found

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

Failed to apply 'io.fabric' plugin on Android

I'm trying to add Firebase Crashlytics.
Firebase Crashlytics tutorial is very simple:
https://firebase.google.com/docs/crashlytics/get-started?authuser=0
I've already added repositories (in buildscript and in all projects), as well as classpath and implementation of dependency. All just like in the tutorial. But when I applying 'io.fabric' plugin (apply plugin: 'io.fabric') and pressing 'Sync' in Android Studio - next error is shown:
A problem occurred evaluating project ':app'.
> Failed to apply plugin [id 'io.fabric']
> No such property: verboseGradlePlugin for class: java.lang.String
I'm applying plugin after "apply plugin: 'com.android.application'".
Tried to add Fabric plugin to Android Studio - didn't help.
Tried all plugin versions down to 1.24.0. (Current is 1.25.4)
Invalidated caches and restarted Android Studio.
Tried to add 'fabric.properties' file to app folder as well as 'crashlytics.properties' file.
Tried to pass -DverboseGradlePlugin=false or with 'true' to gradle's 'build' task.
Gradle knows about 'io.fabric' plugin, but trying to find 'verboseGradlePlugin' property that is missing. I haven't found any info about such issue in the google.
Maybe someone already faced the same problem or have any suggestions how to solve this?
UPD:
My project-level build.gradle
My app-level build.gradle
Gradle version - 4.4
Android gradle plugin version - 3.1.2
Step1:
In your project level build.gradle add:
maven {
url 'https://maven.fabric.io/public'
}
NB:This addition should strictly be pasted inside your buildscript and not your allprojects gradle script as follows:
buildscript {
repositories {
jcenter()
google()
maven {
url 'https://maven.fabric.io/public'
}
}
Next,add your io.fabric tools into your dependencies in the same gradle(build.gradle)
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
classpath 'com.google.gms:google-services:3.2.1'
classpath 'io.fabric.tools:gradle:1.25.4'
}
As soon as this is done,Sync your gradle before moving onto the next step.You should have something like this for step1
Step2:In your app level build.gradle add
apply plugin: 'io.fabric'
And in your dependencies:
dependencies {
// ...
implementation 'com.crashlytics.sdk.android:crashlytics:2.9.3'
}
Now Sync again and Rebuild and Run your project.
NB:Sync right after manipulating the project level build.gradle before manipulating your app level.
More details here
I had exactly the same problem. The error is generated due to the extra property "crashlytics" in the project-level build.gradle, which generates a conflict.
Simply change the extra property "crashlitycs" to "crashlyticsVersion" or something similar and the error disappears.
I also recommend that you use the suffix "Version" in the extra properties to avoid similar errors.
In your project gradle you should put
buildscript {
repositories {
...
maven {url 'https://maven.fabric.io/public'}
}
...
}
Meanwhile in the app gradle file in the top
apply plugin: 'io.fabric'
and as dependencies
implementation 'com.crashlytics.sdk.android:crashlytics:2.9.3'

com.android.library not find

I use gradle build my android code ,here is my build.gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle-experimental:0.2.1'
}
}
so I can apply
plugin:com.android.model.application ,but when I want to depend a module , I got a error:
error:plugin with com.android.library not find
which is my
android library module's build.gradle begin with
apply plugin: 'com.android.library
if I write build.gradle like this:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
}
}
I cannnot find
plugin: 'com.android.model.application' my question is what should I do to depend my library successfully
Hey you have to update the plugin to match the plugins available in the experimental build:
IE. change "com.android.library" to "com.android.model.library"
You can read more about it here on the experimental gradle build documentation.
Search for "com.android.model.library" and you'll go right to it.
Be sure to read the other stuff in the doc; there are other differences you will have to add to your old library-model build.gradle file also. (eg. "targetSdkVersion 23" changes to "targetSdkVersion.apiLevel 23"

Error:Plugin with id 'com.github.dcendents.android-maven' not found

I'm using this library in my Android app. (https://github.com/yazeed44/MultiImagePicker)
Before now, I was adding it to my project this way:
compile 'net.yazeed44.imagepicker:imagepicker:1.3.0'
The problem with importing it that way is, as far as I know, that I can't override any code because I'll lose all the changes after building the project again. (I need to change some code)
For that reason, I have downloaded the source code and I've imported the project as a module with this name: 'imagepicker'
After that, I added this line to my app build.gradle:
compile project(':imagepicker')
and this to my settings.gradle (Android Studio did it)
include ':app', ':imagepicker'
After doing that, I try to run the project and Android studio shows this error:
Gradle 'Project' project refresh failed
Error:Plugin with id 'com.github.dcendents.android-maven' not found.
Since you are using the module locally you have to add in your top-level build.gradle or in the imagepicker/build.gradle same config added in the imagepicker build.gradle project.
buildscript {
repositories {
jcenter()
}
dependencies {
//ADD THESE DEPENDENCIES
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.2'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
}
}
An alternative can be modify the imagepicker/build.gradle removing the last 2 lines. But you have to test this way.
apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/installv1.gradle'
apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/bintrayv1.gradle'
If you check these files you will find the
apply plugin: 'com.github.dcendents.android-maven'
In your case you don't need these files because they are useful only to uplaod in a maven repo the aar file.
I added below code in Project:gradle.build file and its resolved the problem :
allprojects {
repositories {
jcenter()
maven {
url "https://repo.commonsware.com.s3.amazonaws.com"
}
}
}
EDIT
If you still facing after adding above maven dependencies
Change url "https://repo.commonsware.com.s3.amazonaws.com" to url "https://s3.amazonaws.com/repo.commonsware.com".

Categories

Resources