gradle plugin开发(二)

Extension

  1. 创建扩展
abstract class MyPluginExtension {

    /**属性1*/
    abstract val data1: Property 

    /**属性2*/
    abstract val data2: Property

    var dataString: String? = null

   /**方法1*/
    fun testData(data: String) {
        this.dataString = data
    }

}
override fun apply(project: Project) {
//创建扩展
 val myExt = project.extensions.create("myExtensions",MyPluginExtension::class.java).apply {
                //设置默认值
               data1.convention(“”)
               data2.convention(“”)
               testData("")
  }

    //project评估完成后可以读取值
    project.afterEvaluate { p ->
        println("${myExt.data1},${myExt.data2},${myExt.dataString}")
    }
}
  1. 在gradle中使用extensions
plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'xxxx.xxx'
}

//使用插件的扩展
myExtensions {
    data1 ="data1"
    data2 ="data2"
    testData "测试1234"
}

你可能感兴趣的:(gradle plugin开发(二))