How do I support different android plugin?

I am creating a gradle plugin for android projects in which I intended to find the android plugin to get the namespace of the project. In the plugin’s gradle build file I am using implementation("com.android.tools.build:gradle:8.1.4") to get the CommonExtension of the android.
and I have create a typealias for the common extension.


import com.android.build.api.dsl.CommonExtension

typealias AndroidExtension = CommonExtension<*, *, *, *,*>

And In the task I am getting the android extension using the line

@Internal
    val androidExtension = project.extensions.getByName<AndroidExtension>("android")

When applying my plugin to an android project having same implementation of android tools.build.gradle:8.1.4, It works fine but when I update the plugin’s implementation of android.tools.build.gradle to 8.5.*, in the same project which the plugin was applied earlier throw exception

 > Mismatch of count of formal and actual type arguments in constructor of com.android.build.api.dsl.CommonExtension: 5 formal argument(s) 6 actual argument(s)

So, to use my plugin, an android project needs to update com.android.application plugin to 8.5.* which I believe is not ideal for the plugin. My question is, how do I support different Android projects?

I am considering creating a compatibility map for my plugin, such as:

For com.android.application:

  • 8.1.4 – my plugin version 1.0
  • 8.5.2 – my plugin version 2.0

How do I achieve this? I guess this is possible after I publish my plugin.

You could maybe have one class that compiles against 8.1.4 and handles the old case and one that compiles against 8.5.2. Then at runtime you use some means of determining the version or case and use the according class.

Or of course yes, a compatibility matrix if you do not need to support old AGP versions.

Or you make two different plugins instead of automatically deciding which variant to use.

Thanks for the reply. But I have a doubt. If i am using one dependency in the build file then how can I import same dependency with different version? I guess, this is not possible. Android project may use different agp version and A plugin’ implementation class for every version isn’t possible.

Thanks
Björn Kautler
for the effort but I found the simplest solution to the problem, Instead of explicitly specifying the type while finding the plugin of android( AndroidExtension created by the typealias), I can rely on the language to infer the type found and then cast it to the specific extension which is BaseExtension. I guess, it will solve the issue and plugin user doesn’t need worry about the compatibility matrix and also I doesn’t need to publish different version of the plugin for literally getting namespace of the android project.

1 Like