Skip to content

Commit 8b6b377

Browse files
author
maqiang
committed
gradle、groovy相关基础知识
1 parent 8c3356c commit 8b6b377

File tree

15 files changed

+176
-83
lines changed

15 files changed

+176
-83
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66

77
# 更新日志:
88

9+
**2018/12/25**
10+
Gradle理论与实践一:Gradle入门 ( https://blog.csdn.net/u013700502/article/details/85231505
11+
Gradle理论与实践二:Groovy介绍 ( https://blog.csdn.net/u013700502/article/details/85231600
12+
Gradle理论与实践三:Gradle构建脚本基础 ( https://blog.csdn.net/u013700502/article/details/85231661
13+
Gradle理论与实践四:自定义Gradle插件:Gradle入门 ( https://blog.csdn.net/u013700502/article/details/85232032
14+
Gradle配置中subprojects 和 allprojects 的区别:Gradle入门 ( https://blog.csdn.net/u013700502/article/details/85231687
15+
916
**2018/09/20**
1017
Android基于DialogFragment封装一个通用的Dialog ( https://blog.csdn.net/u013700502/article/details/82777402
1118

app/build.gradle

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ android {
4444
buildConfigField "boolean", "IS_SHOW_LOG", SHOW_LOG
4545
}
4646
}
47+
4748
productFlavors {
4849
market_meizu {
4950
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "meizu"]
@@ -62,9 +63,6 @@ android {
6263
}
6364
market_360 {
6465
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "360"]
65-
// ndk {
66-
// abiFilters 'armeabi'
67-
// }
6866
}
6967
}
7068

build.gradle

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
1-
// Top-level build file where you can add configuration options common to all sub-projects/modules.
1+
apply from: 'config.gradle'
2+
23

34
buildscript {
45
repositories {
6+
maven {
7+
url = uri("$rootDir/repo")
8+
}
59
jcenter()
10+
google()
611
maven {
712
url 'https://maven.google.com/'
813
name 'Google'
914
}
1015
}
1116
dependencies {
1217
classpath 'com.android.tools.build:gradle:2.3.3'
13-
14-
// NOTE: Do not place your application dependencies here; they belong
15-
// in the individual module build.gradle files
18+
// classpath 'com.fastgo:plugin:1.0-test'
1619
}
1720
}
1821

1922

2023
allprojects {
2124
repositories {
2225
jcenter()
26+
google()
2327
maven {
2428
url 'https://maven.google.com/'
2529
name 'Google'
@@ -30,11 +34,34 @@ allprojects {
3034
task clean(type: Delete) {
3135
delete rootProject.buildDir
3236
}
33-
ext {
34-
compileSdkVersion = 26
35-
buildToolsVersion = '26.0.2'
36-
minSdkVersion = 16
37-
targetSdkVersion = 26
38-
versionCode = 3
39-
versionName = "1.3"
40-
}
37+
38+
//1、直接在build.gradle中定义插件
39+
//apply plugin: GreetingPlugin
40+
41+
//class GreetingPlugin implements Plugin<Project> {
42+
//
43+
// @Override
44+
// void apply(Project project) {
45+
// project.task('hello') {
46+
// doLast {
47+
// println 'Hello from the GreetingPlugin'
48+
// }
49+
// }
50+
// }
51+
//}
52+
53+
//2、在rootProject/buildSrc/main/groovy中定义插件
54+
//apply plugin: GreetingExtensionPlugin
55+
//
56+
//greeting {
57+
// message = 'hello'
58+
// greeter = 'GreetingExtensionPlugin'
59+
//}
60+
61+
//3、定义在单独的moudle中并发布
62+
// apply plugin: 'com.fastgo.plugin'
63+
//ext.greetingFile="$buildDir/hello.txt"
64+
65+
66+
67+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com
2+
3+
import org.gradle.api.Plugin
4+
import org.gradle.api.Project
5+
6+
class GreetingExtensionPlugin implements Plugin<Project> {
7+
8+
@Override
9+
void apply(Project project) {
10+
11+
// Add the 'greeting' extension object
12+
def extension = project.extensions.create('greeting', GreetingExtension)
13+
14+
// Add a task that uses configuration from the extension object
15+
project.tasks.create('buildSrc') {
16+
doLast {
17+
println "${extension.message} from ${extension.greeter}"
18+
println project.greeting
19+
}
20+
}
21+
}
22+
}
23+
24+
class GreetingExtension {
25+
String message
26+
String greeter
27+
}
28+
29+
30+

config.gradle

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
ext {
2+
compileSdkVersion = 26
3+
buildToolsVersion = '26.0.2'
4+
minSdkVersion = 16
5+
targetSdkVersion = 26
6+
versionCode = 3
7+
versionName = "1.3"
8+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Mon Aug 20 23:23:42 CST 2018
1+
#Tue Dec 25 15:18:50 CST 2018
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip

gradlew

100644100755
File mode changed.

multiprocess_sever/build.gradle

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
apply plugin: 'com.android.application'
22

33
android {
4-
compileSdkVersion 25
5-
buildToolsVersion "25.0.0"
4+
compileSdkVersion rootProject.ext.compileSdkVersion
5+
buildToolsVersion rootProject.ext.buildToolsVersion
66

7-
defaultConfig {
8-
applicationId "org.ninetripods.mq.multiprocess_sever"
9-
minSdkVersion 15
10-
targetSdkVersion 25
11-
versionCode 1
12-
versionName "1.0"
13-
14-
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
157

8+
defaultConfig {
9+
minSdkVersion rootProject.ext.minSdkVersion
10+
targetSdkVersion rootProject.ext.targetSdkVersion
11+
versionCode rootProject.ext.versionCode
12+
versionName rootProject.ext.versionName
1613
}
17-
sourceSets{
18-
main{
19-
java.srcDirs=['src/main/java','src/main/aidl']
14+
sourceSets {
15+
main {
16+
java.srcDirs = ['src/main/java', 'src/main/aidl']
2017
}
2118
}
2219
buildTypes {
@@ -34,5 +31,5 @@ dependencies {
3431
})
3532
compile 'com.android.support:appcompat-v7:25.2.0'
3633
compile 'com.android.support.constraint:constraint-layout:1.0.2'
37-
testCompile 'junit:junit:4.12'
34+
// testCompile 'junit:junit:4.12'
3835
}

multiprocess_sever/src/androidTest/java/org/ninetripods/mq/multiprocess_sever/ExampleInstrumentedTest.java

Lines changed: 0 additions & 26 deletions
This file was deleted.

multiprocess_sever/src/test/java/org/ninetripods/mq/multiprocess_sever/ExampleUnitTest.java

Lines changed: 0 additions & 17 deletions
This file was deleted.

plugin/build.gradle

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
plugins {
2+
id 'groovy'
3+
id 'maven-publish'
4+
id 'maven'
5+
}
6+
7+
dependencies {
8+
implementation gradleApi()
9+
implementation localGroovy()
10+
}
11+
12+
repositories {
13+
mavenCentral()
14+
}
15+
16+
group = 'com.fastgo'
17+
version = '1.0-test'
18+
publishing {
19+
repositories {
20+
maven {
21+
url = uri("$rootDir/repo")
22+
}
23+
}
24+
publications {
25+
maven(MavenPublication) {
26+
from components.java
27+
}
28+
}
29+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.fastgo.plugin
2+
3+
import org.gradle.api.DefaultTask
4+
import org.gradle.api.Project
5+
import org.gradle.api.Plugin
6+
import org.gradle.api.tasks.TaskAction
7+
8+
class CustomPlugin implements Plugin<Project> {
9+
10+
@Override
11+
void apply(Project project) {
12+
13+
project.tasks.create('writeToFile', CustomPluginTask) {
14+
15+
destination = { project.greetingFile }
16+
17+
doLast {
18+
println project.file(destination).text
19+
}
20+
}
21+
}
22+
}
23+
24+
class CustomPluginTask extends DefaultTask {
25+
26+
def destination
27+
28+
File getDestination() {
29+
//创建路径为destination的file
30+
project.file(destination)
31+
}
32+
33+
@TaskAction
34+
def greet() {
35+
def file = getDestination()
36+
file.parentFile.mkdirs()
37+
//向文件中写入文本
38+
file.write('hello world')
39+
}
40+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
implementation-class=com.fastgo.plugin.CustomPlugin

settings.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
include ':app', ':multiprocess_sever', ':sydialoglib'
2+
include ':plugin'
3+
include ':buildSrc'

sydialoglib/build.gradle

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
apply plugin: 'com.android.library'
22

33
android {
4-
compileSdkVersion 25
5-
buildToolsVersion "25.0.0"
4+
compileSdkVersion rootProject.ext.compileSdkVersion
5+
buildToolsVersion rootProject.ext.buildToolsVersion
66

77

88
defaultConfig {
9-
minSdkVersion 14
10-
targetSdkVersion 25
11-
versionCode 1
12-
versionName "1.0"
13-
14-
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
15-
9+
minSdkVersion rootProject.ext.minSdkVersion
10+
targetSdkVersion rootProject.ext.targetSdkVersion
11+
versionCode rootProject.ext.versionCode
12+
versionName rootProject.ext.versionName
1613
}
1714

1815
buildTypes {

0 commit comments

Comments
 (0)