How to add a function to android.defaultConfig wit

2019-06-24 08:30发布

I want to create a Gradle plugin which adds functions to the Android Gradle plugin. I want to add a getGreeting function to android.defaultConfig such as outlined here - but via a plugin:

// build.gradle
android {
    defaultConfig {
        def getGreeting = { name ->
            return "Hello ${name}"
        }
    }
}

I started preparing the Groovy project in general. Now I am at this point:

package com.example.myexample

import com.android.build.gradle.AppPlugin
import com.android.build.gradle.LibraryPlugin
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.tasks.StopExecutionException


class MyExamplePlugin implements Plugin<Project> {

    @Override
    void apply(Project project) {
        if (hasAndroidPlugin(project)) {
            throw new StopExecutionException(
                "Must be applied before 'android' or 'android-library' plugin.")
        }
        // def extension = project.android.extensions.create("foobar", MyExamplePlugin, project)
        // def AppPlugin androidPlugin = project.plugins.getPlugin("android")
    }

    static def hasAndroidPlugin(Project project) {
        return project.plugins.hasPlugin(AppPlugin) || 
               project.plugins.hasPlugin(LibraryPlugin)
    }

}

Since I never used Groovy I do not even know how to debug into the class. The commented lines might be a way to access the android.defaultConfig block. How can I add a function there?

0条回答
登录 后发表回答