Apply plugin question

I am writing a library for an Android application to support Huawei push notifications on Android. According to Huawei documentation, I have to apply a plugin to the application module. But I want to make my library to do it for the user of the library.
I have read the topic in which they say, that I must use fully classified classname to add the plugin to the project. So I used the command ./gradlew tasks to see the fully classified classname for plugin com.huawei.agconnect and it is com.huawei.agconnect.agcp.AGCPlugin.
The code I use in my library module build.gradle :
evaluationDependsOn(':app')
rootProject.subprojects {
if (name == "app") {
if (!plugins.hasPlugin('com.huawei.agconnect')) {
apply {
plugin com.huawei.agconnect.agcp.AGCPlugin
}}}}

The problem is the way I apply Huawei plugin from my library module build.gradle file doesn’t work correctly. When I use apply plugin: 'com.huawei.agconnect' in app module everything works perfectly.
Have I missed anything? What is the difference between ‘apply plugin’ in main module and the way I am applying the plugin from my library module? Help me please to figure it out.

If you are trying to create a reusable, distributable library, it’s not possible to do what you’re trying to do. The build of your library cannot impact the build of the consuming project. In that case, you would need to write a plugin that would be applied by the consumer’s build. Applying that plugin could be the only thing necessary though as it could add other plugins, configure them with your opinionated defaults, and add your library as a dependency.

2 Likes

Creating a precompiled script plugin in buildSrc may fit your needs.