From 16f7947b53d371822bdb608078e2c04366cece51 Mon Sep 17 00:00:00 2001 From: Omooo <869759698@qq.com> Date: Tue, 6 Aug 2019 14:51:10 +0800 Subject: [PATCH] add Gradle Extension --- README.md | 2 + blogs/Android/Gradle/Extension.md | 202 ++++++++++++++++++++++++++++++ 2 files changed, 204 insertions(+) create mode 100644 blogs/Android/Gradle/Extension.md diff --git a/README.md b/README.md index 3cbb197..1b9d69e 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,8 @@ Android Notes [Gralde Plugin 实践之 TinyPng Plugin](https://github.com/Omooo/Android-Notes/blob/master/blogs/Android/Gradle/TinyPngPlugin.md) +[Gradle 详解 Extension](https://github.com/Omooo/Android-Notes/blob/master/blogs/Android/Gradle/Extension.md) + 《Android Gradle 权威指南读书笔记》 - [基础知识相关](https://github.com/Omooo/Android-Notes/blob/master/blogs/Android/Gradle/Android%20Gradle%20%E6%9D%83%E5%A8%81%E6%8C%87%E5%8D%97/%E8%AF%BB%E4%B9%A6%E7%AC%94%E8%AE%B0%E4%B9%8B%E4%B8%80.md) diff --git a/blogs/Android/Gradle/Extension.md b/blogs/Android/Gradle/Extension.md new file mode 100644 index 0000000..cbb3d06 --- /dev/null +++ b/blogs/Android/Gradle/Extension.md @@ -0,0 +1,202 @@ +--- +Extension +--- + +#### 目录 + +1. 简单 Extension +2. 嵌套 Extension +3. 嵌套容器 Extension + +#### 简单 Extension + +``` +student { + name 'Omooo' + age 18 + isMale false +} +``` + +```groovy +class Student { + String name + int age + boolean isMale +} +``` + +```groovy +project.extensions.create('student', Student.class) +Student student = project.extensions.getByType(Student.class) +println(student.name) +``` + +#### 嵌套 Extension + +``` +student { + name 'Omooo' + age 18 + isMale false + + info { + qq 2333 + email '2333@qq.com' + isDog true + } +} +``` + +```groovy +class Student { + String name + int age + boolean isMale + Info info + + @SuppressWarnings("UnstableApiUsage") + Student(ObjectFactory factory) { + info = factory.newInstance(Info.class) + } + + def info(Action action) { + action.execute(info) + } + + + static class Info { + String email + int qq + boolean isDog + } +} +``` + +```groovy +project.extensions.create('student', Student.class, project.objects) +Student student = project.extensions.getByType(Student.class) +println(student.name) +println(student.info.qq) +``` + +#### 嵌套容器 Extension + +``` +animal { + count 2333 + + dog { + form 'Animal' + isMale false + } + + catConfig { + shanghaiCat { + from 'Shanghai' + weight 20000.0f + } + + beijingCat { + from 'Beijing' + weight 300f + } + } +} +``` + +```groovy +class Dog { + String form + boolean isMale + + @Override + String toString() { + return "Dog{" + + "form='" + form + '\'' + + ", isMale=" + isMale + + '}' + } +} +``` + +```groovy +class Cat { + String name + + String from + float weight + + Cat(String name) { + this.name = name + } + + @Override + String toString() { + return "Cat{" + + "name='" + name + '\'' + + ", from='" + from + '\'' + + ", weight=" + weight + + '}' + } +} +``` + +```groovy +class CatExtFactory implements NamedDomainObjectFactory { + + private Instantiator instantiator + + CatExtFactory(Instantiator instantiator1) { + this.instantiator = instantiator1 + } + + @Override + Cat create(String s) { + return instantiator.newInstance(Cat.class, s) + } +} +``` + +```groovy +class Animal { + int count + Dog dog + private NamedDomainObjectContainer catContainer + + Animal(Instantiator instantiator, + NamedDomainObjectContainer catContainer) { + this.dog = instantiator.newInstance(Dog.class) + this.catContainer = catContainer + } + + void dog(Action action) { + action.execute(dog) + } + + void catConfig(Action> action) { + action.execute(catContainer) + } + + @Override + String toString() { + return "dog info:" + dog.toString() + "\ncat info:" + catContainer + } +} +``` + +```groovy + Instantiator instantiator = ((DefaultGradle) project.getGradle()) + .getServices().get(Instantiator.class) + NamedDomainObjectContainer catContainer = + project.container(Cat.class, new CatExtFactory(instantiator)) + project.extensions.create('animal', Animal.class, instantiator, catContainer) + + project.task('showAnimalInfo'){ + doLast{ + Animal animal = project.extensions.getByName('animal') + println(animal.toString()) + } + } +``` +