androidandroid-gradle-pluginbuild.gradle

How to find gradle plugin id for given gradle library?


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?


Solution

  • 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.