Skip to content

Commit e88fed4

Browse files
author
Ryze
committed
自定义Gradle Plugin
1 parent da84ca6 commit e88fed4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+893
-1
lines changed

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/workspace.xml
5+
/.idea/libraries
6+
.DS_Store
7+
/build
8+
/captures
9+
.externalNativeBuild
10+
/.idea

README.md

Lines changed: 184 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,184 @@
1-
# Android-GradlePlugin-Demo
1+
## Android Gradle Plugin
2+
3+
### Android Gradle Plugin 有两种形式
4+
5+
> 1. 直接在build.gradle/创建x.gradle中开发
6+
7+
> 2. 实现`Plugin`重写build 过程
8+
9+
这里讲解怎么开发自定义插件
10+
11+
### 首先创建Gradle Plugin 工程
12+
13+
* 为了方便测试先创建一个Android 工程,然后创建一个Android library Module工程
14+
15+
![创建module](https://raw.githubusercontent.com/goodbranch/AndroidNote/master/note/gradle/module-1.png)
16+
17+
* 删除如图中箭头所指目录和文件
18+
19+
![删除无效目录](https://raw.githubusercontent.com/goodbranch/AndroidNote/master/note/gradle/module-2.png)
20+
21+
* `build.gradle`中改成groovy方式
22+
23+
24+
apply plugin: 'groovy'
25+
26+
dependencies {
27+
compile gradleApi()
28+
compile localGroovy()
29+
}
30+
31+
32+
### 自定义Gradle Plugin,在main目录下创建groovy目录,这个目录下创建创建自己的代码
33+
34+
#### 以删除log日志为例
35+
36+
![Groovy 项目结构](https://raw.githubusercontent.com/goodbranch/AndroidNote/master/note/gradle/groovy-project.png)
37+
38+
#### 1.继承 gradle`Plugin`,类的后缀不再是.java而是.groovy
39+
40+
class DelLogPlugin implements Plugin<Project> {
41+
@Override
42+
void apply(Project project) {
43+
44+
45+
project.extensions.create('dellogExtension', DelLogExtension);
46+
47+
project.afterEvaluate {
48+
//在gradle 构建完之后执行
49+
project.logger.error("dellogExtension : " + project.dellogExtension.sourceDir);
50+
51+
def rootDir = project.projectDir.toString().plus(project.dellogExtension.sourceDir);
52+
53+
project.logger.error(rootDir);
54+
55+
DelLogUtil.delLog(new File(rootDir));
56+
}
57+
58+
project.task('dellog', {
59+
project.logger.error("dellogExtension : " + project.dellogExtension.sourceDir);
60+
61+
def rootDir = project.projectDir.toString().plus(project.dellogExtension.sourceDir);
62+
63+
project.logger.error(rootDir);
64+
65+
DelLogUtil.delLog(new File(rootDir));
66+
67+
})
68+
69+
}
70+
}
71+
72+
73+
`afterEvaluate`是在gradle构建完后自动执行的,但task需要手动执行
74+
一个插件中可以创建多个Task,如代码中的`dellog`,可以在控制台执行`gradle -q dellog`,也可以在gradle图形界面执行
75+
76+
![图形界面执行task](https://raw.githubusercontent.com/goodbranch/AndroidNote/master/note/gradle/gradle-task-ui.png)
77+
78+
#### 2. 创建可以输入的Gradle 插件
79+
80+
> 很多时候我们需要输入参数,然后根据参数来做处理,处理如下:
81+
project.extensions.create(...,...); 前面是Name,后面是Model,model中在gralde script 中键对上就可以。
82+
83+
class DelLogExtension {
84+
85+
String sourceDir;
86+
87+
}
88+
89+
90+
class DelLogPlugin implements Plugin<Project> {
91+
@Override
92+
void apply(Project project) {
93+
94+
95+
project.extensions.create('dellogExtension', DelLogExtension);
96+
97+
......
98+
99+
然后在app 下的build.gradle中
100+
101+
dellogExtension.sourceDir = '/src'
102+
103+
104+
105+
dellogExtension {
106+
sourceDir = '/src'
107+
}
108+
109+
![使用输入](https://raw.githubusercontent.com/goodbranch/AndroidNote/master/note/gradle/use-extension.png)
110+
111+
112+
### 3. 在main下创建`resources`目录
113+
114+
`src/main/resources/META-INF/gradle-plugins/com.branch.plugin.dellog.properties`
115+
116+
`xxx.properties` 将来作为apply plugin: xxx 插件名称,这里的目录结构不能错,先有`META-INF`再有`gradle-plugins`
117+
118+
内容:
119+
120+
`implementation-class=com.branch.dellog.DelLogPlugin(继承Plugin的类,插件的入口类)`
121+
122+
### 4. 发布到本地仓库
123+
124+
在当前lib项目build.gradle中增加maven支持
125+
126+
apply plugin: 'maven'
127+
128+
repositories {
129+
mavenCentral()
130+
}
131+
132+
然后增加对应的maven deployer
133+
134+
//设置maven deployer
135+
uploadArchives {
136+
repositories {
137+
mavenDeployer {
138+
//设置插件的GAV参数
139+
pom.groupId = 'com.branch.plugin'
140+
pom.artifactId = 'dellog'
141+
pom.version = '1.0.0'
142+
//文件发布到下面目录
143+
repository(url: uri('../repo'))
144+
}
145+
}
146+
}
147+
148+
这里设置发布到上一个目录的`repo`中,同时可以查看gradle task中有一个名为uploadArchives的task
149+
150+
![发布到本地task](https://raw.githubusercontent.com/goodbranch/AndroidNote/master/note/gradle/maven-local-task.png)
151+
152+
执行就能在`repo`中查看到相应jar包
153+
154+
![查看本地仓库](https://raw.githubusercontent.com/goodbranch/AndroidNote/master/note/gradle/maven-local-resuslt.png)
155+
156+
### 5. 使用本地仓库
157+
158+
在project 的build.gradle 中`buildscript`中增加本地仓库地址
159+
160+
buildscript {
161+
repositories {
162+
jcenter()
163+
maven {
164+
url uri("/repo")
165+
}
166+
}
167+
dependencies {
168+
classpath 'com.android.tools.build:gradle:2.2.2'
169+
classpath 'com.branch.plugin:dellog:1.0.0'
170+
171+
// NOTE: Do not place your application dependencies here; they belong
172+
// in the individual module build.gradle files
173+
}
174+
}
175+
176+
然后在app 的build.gradle中增加plugin
177+
178+
apply plugin: 'com.branch.plugin.dellog'
179+
180+
181+
#### 所有这些配置正确后同步项目可在Gradle Console查看到日志
182+
183+
184+
#### [github demo](https://github.com/goodbranch/Android-GradlePlugin-Demo/tree/master)

app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

app/build.gradle

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
apply plugin: 'com.android.application'
2+
apply plugin: 'com.branch.plugin.dellog'
3+
4+
android {
5+
compileSdkVersion 25
6+
buildToolsVersion "25.0.1"
7+
defaultConfig {
8+
applicationId "com.branch.www.dellogdemo"
9+
minSdkVersion 11
10+
targetSdkVersion 25
11+
versionCode 1
12+
versionName "1.0"
13+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
14+
}
15+
buildTypes {
16+
release {
17+
minifyEnabled false
18+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
19+
}
20+
}
21+
22+
}
23+
24+
dependencies {
25+
compile fileTree(dir: 'libs', include: ['*.jar'])
26+
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
27+
exclude group: 'com.android.support', module: 'support-annotations'
28+
})
29+
compile 'com.android.support:appcompat-v7:25.1.0'
30+
testCompile 'junit:junit:4.12'
31+
}
32+
33+
//dellogExtension.sourceDir = '/src'
34+
35+
dellogExtension {
36+
sourceDir = '/src'
37+
}

app/proguard-rules.pro

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in E:\BaiduYunDownload\adt-bundle-windows-x86_64-20140702\sdk/tools/proguard/proguard-android.txt
4+
# You can edit the include path and order by changing the proguardFiles
5+
# directive in build.gradle.
6+
#
7+
# For more details, see
8+
# http://developer.android.com/guide/developing/tools/proguard.html
9+
10+
# Add any project specific keep options here:
11+
12+
# If your project uses WebView with JS, uncomment the following
13+
# and specify the fully qualified class name to the JavaScript interface
14+
# class:
15+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16+
# public *;
17+
#}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.branch.www.dellogdemo;
2+
3+
import android.content.Context;
4+
import android.support.test.InstrumentationRegistry;
5+
import android.support.test.runner.AndroidJUnit4;
6+
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
10+
import static org.junit.Assert.*;
11+
12+
/**
13+
* Instrumentation test, which will execute on an Android device.
14+
*
15+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
16+
*/
17+
@RunWith(AndroidJUnit4.class)
18+
public class ExampleInstrumentedTest {
19+
@Test
20+
public void useAppContext() throws Exception {
21+
// Context of the app under test.
22+
Context appContext = InstrumentationRegistry.getTargetContext();
23+
24+
assertEquals("com.branch.www.dellogdemo", appContext.getPackageName());
25+
}
26+
}

app/src/main/AndroidManifest.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.branch.www.dellogdemo">
4+
5+
<application
6+
android:allowBackup="true"
7+
android:icon="@mipmap/ic_launcher"
8+
android:label="@string/app_name"
9+
android:supportsRtl="true"
10+
android:theme="@style/AppTheme">
11+
<activity android:name=".MainActivity">
12+
<intent-filter>
13+
<action android:name="android.intent.action.MAIN"/>
14+
15+
<category android:name="android.intent.category.LAUNCHER"/>
16+
</intent-filter>
17+
</activity>
18+
</application>
19+
20+
</manifest>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.branch.www.dellogdemo;
2+
3+
import android.os.Bundle;
4+
import android.support.v7.app.AppCompatActivity;
5+
import android.util.Log;
6+
7+
public class MainActivity extends AppCompatActivity {
8+
9+
@Override
10+
protected void onCreate(Bundle savedInstanceState) {
11+
super.onCreate(savedInstanceState);
12+
setContentView(R.layout.activity_main);
13+
14+
;
15+
16+
;
17+
18+
19+
}
20+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.branch.www.dellogdemo;
2+
3+
import android.util.Log;
4+
5+
/**
6+
* Created by Ryze on 2017-2-5.
7+
*/
8+
9+
public class TestDelLog {
10+
11+
public static void test() {
12+
13+
;
14+
15+
;
16+
17+
18+
19+
}
20+
21+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RelativeLayout
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:id="@+id/activity_main"
6+
android:layout_width="match_parent"
7+
android:layout_height="match_parent"
8+
android:paddingLeft="@dimen/activity_horizontal_margin"
9+
android:paddingRight="@dimen/activity_horizontal_margin"
10+
android:paddingTop="@dimen/activity_vertical_margin"
11+
android:paddingBottom="@dimen/activity_vertical_margin"
12+
tools:context="com.branch.www.dellogdemo.MainActivity">
13+
14+
<TextView
15+
android:layout_width="wrap_content"
16+
android:layout_height="wrap_content"
17+
android:text="Hello World!"/>
18+
</RelativeLayout>
3.34 KB
Loading
2.15 KB
Loading
4.73 KB
Loading
7.54 KB
Loading
10.2 KB
Loading
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<resources>
2+
<!-- Example customization of dimensions originally defined in res/values/dimens.xml
3+
(such as screen margins) for screens with more than 820dp of available width. This
4+
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). -->
5+
<dimen name="activity_horizontal_margin">64dp</dimen>
6+
</resources>

app/src/main/res/values/colors.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<color name="colorPrimary">#3F51B5</color>
4+
<color name="colorPrimaryDark">#303F9F</color>
5+
<color name="colorAccent">#FF4081</color>
6+
</resources>

app/src/main/res/values/dimens.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<resources>
2+
<!-- Default screen margins, per the Android Design guidelines. -->
3+
<dimen name="activity_horizontal_margin">16dp</dimen>
4+
<dimen name="activity_vertical_margin">16dp</dimen>
5+
</resources>

app/src/main/res/values/strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<resources>
2+
<string name="app_name">DelLogDemo</string>
3+
</resources>

0 commit comments

Comments
 (0)