Hans-Christoph Steiner
Hans-Christoph Steiner

Reputation: 2682

insert plugin into Android gradle build using init.gradle

F-Droid is a free software app store for Android. We enforce that apps are 100% free software, so its something like Debian for Android. Since most Android apps use gradle, it makes sense to use gradle tricks to automate our enforcement. The 'com.jaredsburrows.license' plugin is one kind of thing we'd like to automatically inject into every build. init.gradle should be able to do this, but I can't quite figure out how to insert the plugin into the Android plugin. Here's where I got:

apply plugin: FDroidLicenseCheck

class FDroidLicenseCheck implements Plugin<Gradle> {
  def supportedPlugins = [
    'org.gradle.api.plugins.JavaPlugin',
    'com.android.build.gradle.AppPlugin'
  ]

  void apply(Gradle gradle) {
    println('applying license check')
    def added = false

    gradle.allprojects { project ->
      project.with { 
        if (parent == null) {
          buildscript { 
            repositories {
              jcenter()
            }
            dependencies {
              classpath "com.jaredsburrows:gradle-license-plugin:0.5.0"
            }
          }
        }

        plugins.whenPluginAdded { plugin ->
          println('Checking ' + plugin.class.name)
          if (!added && supportedPlugins.contains(plugin.class.name)) {
            println('Adding to ' + plugin.class.name)
            rootProject.apply plugin: 'com.jaredsburrows.license'
            added = true
          }
        }
      }
    }
  }
}

Upvotes: 1

Views: 145

Answers (0)

Related Questions