How to add a function to android.defaultConfig with a Gradle plugin?

。_饼干妹妹 提交于 2019-12-10 17:43:38

问题


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?

来源:https://stackoverflow.com/questions/28439997/how-to-add-a-function-to-android-defaultconfig-with-a-gradle-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!