Sharing logic between convention plugins

I have two convention plugins (teams-android-library-java-only.gradle.kts and teams-android-library.gradle.kts) that I apply to Java android library modules with java and Kotlin android library modules. They are applied to ~600 modules in my project.

I need to configure unit tests in those modules. Both of them have code similar to below.

android {
  // common android configuration here.
  unitTestVariants.all {
    // 300 lines of duplication here in both convention plugins.
  }
}

How can I avoid the 300 lines of duplication in each of the convention plugins.

Make a third plugin that does the common configuration and apply that third plugin from the other two.

I see, I will try that. Do you have a sample code that does that?

Surely not with Android, as I’m not into Android development.
But I’m also unsure what you need, just apply one from the other.
Precompiled script plugins can apply other precomplied script plugins that are next to it just like any other plugin.

Thank you so much. That worked.

1 Like