Skip to content

Commit 72aeb6c

Browse files
author
mrzhang
committed
release build-gradle 1.1.0
1 parent ed8f8ac commit 72aeb6c

File tree

10 files changed

+74
-89
lines changed

10 files changed

+74
-89
lines changed

basiclib/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ dependencies {
2828
})
2929
compile 'com.android.support:appcompat-v7:26.+'
3030
testCompile 'junit:junit:4.12'
31-
compile 'com.squareup.okhttp3:okhttp:3.4.1'
3231
compile 'com.squareup.picasso:picasso:2.5.2'
3332
compile 'com.luojilab.ddcomponent:componentlib:1.0.0'
3433
}

basiclib/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<application
77
android:allowBackup="true"
8-
android:label="@string/app_name"
8+
android:label="@string/kotlin_app_name"
99
android:supportsRtl="true">
1010

1111
</application>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<resources>
2-
<string name="app_name">basiclib</string>
2+
<string name="kotlin_app_name">basiclib</string>
33
</resources>

basicres/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
package="com.luojilab.component.basicres">
44

5-
<application android:allowBackup="true" android:label="@string/app_name"
5+
<application android:allowBackup="true" android:label="@string/kotlin_app_name"
66
android:supportsRtl="true">
77

88
</application>

build-gradle/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ ext {
55
artifact = bintrayName
66
libraryName = 'component build'
77
libraryDescription = 'gradle plugin for buid component'
8-
libraryVersion = "1.0.0"
8+
libraryVersion = "1.1.0"
99
licenseName = 'The Apache Software License, Version 2.0'
1010
licenseUrl = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
1111
allLicenses = ["Apache-2.0"]

build-gradle/src/main/groovy/com.dd.buildgradle/ComBuild.groovy

Lines changed: 39 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ package com.dd.buildgradle
33
import com.dd.buildgradle.exten.ComExtension
44
import org.gradle.api.Plugin
55
import org.gradle.api.Project
6-
import org.gradle.api.Task
76

8-
public class ComBuild implements Plugin<Project> {
7+
class ComBuild implements Plugin<Project> {
98

109
//默认是app,直接运行assembleRelease的时候,等同于运行app:assembleRelease
1110
String compilemodule = "app"
@@ -14,31 +13,31 @@ public class ComBuild implements Plugin<Project> {
1413
project.extensions.create('combuild', ComExtension)
1514

1615
String taskNames = project.gradle.startParameter.taskNames.toString()
17-
System.out.println("taskNames is " + taskNames);
16+
System.out.println("taskNames is " + taskNames)
1817
String module = project.path.replace(":", "")
19-
System.out.println("current module is " + module);
18+
System.out.println("current module is " + module)
2019
AssembleTask assembleTask = getTaskInfo(project.gradle.startParameter.taskNames)
2120

2221
if (assembleTask.isAssemble) {
23-
fetchMainmodulename(project, assembleTask);
24-
System.out.println("compilemodule is " + compilemodule);
22+
fetchMainmodulename(project, assembleTask)
23+
System.out.println("compilemodule is " + compilemodule)
2524
}
2625

2726
if (!project.hasProperty("isRunAlone")) {
2827
throw new RuntimeException("you should set isRunAlone in " + module + "'s gradle.properties")
2928
}
3029

3130
//对于isRunAlone==true的情况需要根据实际情况修改其值,
32-
// 但如果是false,则不用修改,该module作为一个lib,运行module:assembleRelease则发布aar到中央仓库
31+
// 但如果是false,则不用修改
3332
boolean isRunAlone = Boolean.parseBoolean((project.properties.get("isRunAlone")))
3433
String mainmodulename = project.rootProject.property("mainmodulename")
3534
if (isRunAlone && assembleTask.isAssemble) {
3635
//对于要编译的组件和主项目,isRunAlone修改为true,其他组件都强制修改为false
3736
//这就意味着组件不能引用主项目,这在层级结构里面也是这么规定的
3837
if (module.equals(compilemodule) || module.equals(mainmodulename)) {
39-
isRunAlone = true;
38+
isRunAlone = true
4039
} else {
41-
isRunAlone = false;
40+
isRunAlone = false
4241
}
4342
}
4443
project.setProperty("isRunAlone", isRunAlone)
@@ -55,32 +54,14 @@ public class ComBuild implements Plugin<Project> {
5554
}
5655
}
5756
}
58-
System.out.println("apply plugin is " + 'com.android.application');
57+
System.out.println("apply plugin is " + 'com.android.application')
5958
if (assembleTask.isAssemble && module.equals(compilemodule)) {
6059
compileComponents(assembleTask, project)
6160
project.android.registerTransform(new ComCodeTransform(project))
6261
}
6362
} else {
6463
project.apply plugin: 'com.android.library'
65-
System.out.println("apply plugin is " + 'com.android.library');
66-
project.afterEvaluate {
67-
Task assembleReleaseTask = project.tasks.findByPath("assembleRelease")
68-
if (assembleReleaseTask != null) {
69-
assembleReleaseTask.doLast {
70-
File infile = project.file("build/outputs/aar/$module-release.aar")
71-
File outfile = project.file("../componentrelease")
72-
File desFile = project.file("$module-release.aar");
73-
project.copy {
74-
from infile
75-
into outfile
76-
rename {
77-
String fileName -> desFile.name
78-
}
79-
}
80-
System.out.println("$module-release.aar copy success ");
81-
}
82-
}
83-
}
64+
System.out.println("apply plugin is " + 'com.android.library')
8465
}
8566

8667
}
@@ -99,7 +80,7 @@ public class ComBuild implements Plugin<Project> {
9980
if (assembleTask.modules.size() > 0 && assembleTask.modules.get(0) != null
10081
&& assembleTask.modules.get(0).trim().length() > 0
10182
&& !assembleTask.modules.get(0).equals("all")) {
102-
compilemodule = assembleTask.modules.get(0);
83+
compilemodule = assembleTask.modules.get(0)
10384
} else {
10485
compilemodule = project.rootProject.property("mainmodulename")
10586
}
@@ -109,67 +90,72 @@ public class ComBuild implements Plugin<Project> {
10990
}
11091

11192
private AssembleTask getTaskInfo(List<String> taskNames) {
112-
AssembleTask assembleTask = new AssembleTask();
93+
AssembleTask assembleTask = new AssembleTask()
11394
for (String task : taskNames) {
11495
if (task.toUpperCase().contains("ASSEMBLE")
11596
|| task.contains("aR")
97+
|| task.toUpperCase().contains("INSTALL")
11698
|| task.toUpperCase().contains("RESGUARD")) {
11799
if (task.toUpperCase().contains("DEBUG")) {
118-
assembleTask.isDebug = true;
100+
assembleTask.isDebug = true
119101
}
120-
assembleTask.isAssemble = true;
102+
assembleTask.isAssemble = true
121103
String[] strs = task.split(":")
122-
assembleTask.modules.add(strs.length > 1 ? strs[strs.length - 2] : "all");
123-
break;
104+
assembleTask.modules.add(strs.length > 1 ? strs[strs.length - 2] : "all")
105+
break
124106
}
125107
}
126108
return assembleTask
127109
}
128110

129111
/**
130112
* 自动添加依赖,只在运行assemble任务的才会添加依赖,因此在开发期间组件之间是完全感知不到的,这是做到完全隔离的关键
131-
* 支持两种语法:module或者modulePackage:module,前者之间引用module工程,后者使用componentrelease中已经发布的aar
113+
* 支持两种语法:module或者groupId:artifactId:version(@aar),前者之间引用module工程,后者使用maven中已经发布的aar
132114
* @param assembleTask
133115
* @param project
134116
*/
135117
private void compileComponents(AssembleTask assembleTask, Project project) {
136-
String components;
118+
String components
137119
if (assembleTask.isDebug) {
138120
components = (String) project.properties.get("debugComponent")
139121
} else {
140122
components = (String) project.properties.get("compileComponent")
141123
}
142124

143125
if (components == null || components.length() == 0) {
144-
System.out.println("there is no add dependencies ");
145-
return;
126+
System.out.println("there is no add dependencies ")
127+
return
146128
}
147129
String[] compileComponents = components.split(",")
148130
if (compileComponents == null || compileComponents.length == 0) {
149-
System.out.println("there is no add dependencies ");
150-
return;
131+
System.out.println("there is no add dependencies ")
132+
return
151133
}
152134
for (String str : compileComponents) {
153-
System.out.println("comp is " + str);
135+
System.out.println("comp is " + str)
154136
if (str.contains(":")) {
155-
File file = project.file("../componentrelease/" + str.split(":")[1] + "-release.aar")
156-
if (file.exists()) {
157-
project.dependencies.add("compile", str + "-release@aar")
158-
System.out.println("add dependencies : " + str + "-release@aar");
159-
} else {
160-
throw new RuntimeException(str + " not found ! maybe you should generate a new one ")
161-
}
137+
/**
138+
* 示例语法:groupId:artifactId:version(@aar)
139+
* compileComponent=com.luojilab.reader:readercomponent:1.0.0
140+
* 注意,前提是已经将组件aar文件发布到maven上,并配置了相应的repositories
141+
*/
142+
project.dependencies.add("compile", str)
143+
System.out.println("add dependencies lib : " + str)
162144
} else {
145+
/**
146+
* 示例语法:module
147+
* compileComponent=readercomponent,sharecomponent
148+
*/
163149
project.dependencies.add("compile", project.project(':' + str))
164-
System.out.println("add dependencies project : " + str);
150+
System.out.println("add dependencies project : " + str)
165151
}
166152
}
167153
}
168154

169155
private class AssembleTask {
170-
boolean isAssemble = false;
171-
boolean isDebug = false;
172-
List<String> modules = new ArrayList<>();
156+
boolean isAssemble = false
157+
boolean isDebug = false
158+
List<String> modules = new ArrayList<>()
173159
}
174160

175161
}

build-gradle/src/main/groovy/com.dd.buildgradle/ComCodeTransform.groovy

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,44 +7,44 @@ import org.apache.commons.codec.digest.DigestUtils
77
import org.apache.commons.io.FileUtils
88
import org.gradle.api.Project
99

10-
public class ComCodeTransform extends Transform {
10+
class ComCodeTransform extends Transform {
1111

1212
private Project project
1313
ClassPool classPool
14-
String applicationName;
14+
String applicationName
1515

1616
ComCodeTransform(Project project) {
1717
this.project = project
1818
}
1919

2020
@Override
2121
void transform(TransformInvocation transformInvocation) throws TransformException, InterruptedException, IOException {
22-
getRealApplicationName(transformInvocation.getInputs());
22+
getRealApplicationName(transformInvocation.getInputs())
2323
classPool = new ClassPool()
2424
project.android.bootClasspath.each {
2525
classPool.appendClassPath((String) it.absolutePath)
2626
}
2727
def box = ConvertUtils.toCtClasses(transformInvocation.getInputs(), classPool)
2828

2929
//要收集的application,一般情况下只有一个
30-
List<CtClass> applications = new ArrayList<>();
30+
List<CtClass> applications = new ArrayList<>()
3131
//要收集的applicationlikes,一般情况下有几个组件就有几个applicationlike
32-
List<CtClass> activators = new ArrayList<>();
32+
List<CtClass> activators = new ArrayList<>()
3333

3434
for (CtClass ctClass : box) {
3535
if (isApplication(ctClass)) {
3636
applications.add(ctClass)
37-
continue;
37+
continue
3838
}
3939
if (isActivator(ctClass)) {
4040
activators.add(ctClass)
4141
}
4242
}
4343
for (CtClass ctClass : applications) {
44-
System.out.println("application is " + ctClass.getName());
44+
System.out.println("application is " + ctClass.getName())
4545
}
4646
for (CtClass ctClass : activators) {
47-
System.out.println("applicationlike is " + ctClass.getName());
47+
System.out.println("applicationlike is " + ctClass.getName())
4848
}
4949

5050
transformInvocation.inputs.each { TransformInput input ->
@@ -78,7 +78,7 @@ public class ComCodeTransform extends Transform {
7878
if (classNameTemp.endsWith(".class")) {
7979
String className = classNameTemp.substring(1, classNameTemp.length() - 6)
8080
if (className.equals(applicationName)) {
81-
injectApplicationCode(applications.get(0), activators, fileName);
81+
injectApplicationCode(applications.get(0), activators, fileName)
8282
}
8383
}
8484
}
@@ -102,30 +102,30 @@ public class ComCodeTransform extends Transform {
102102

103103

104104
private void injectApplicationCode(CtClass ctClassApplication, List<CtClass> activators, String patch) {
105-
System.out.println("injectApplicationCode begin");
106-
ctClassApplication.defrost();
105+
System.out.println("injectApplicationCode begin")
106+
ctClassApplication.defrost()
107107
try {
108108
CtMethod attachBaseContextMethod = ctClassApplication.getDeclaredMethod("onCreate", null)
109109
attachBaseContextMethod.insertAfter(getAutoLoadComCode(activators))
110110
} catch (CannotCompileException | NotFoundException e) {
111-
StringBuilder methodBody = new StringBuilder();
112-
methodBody.append("protected void onCreate() {");
113-
methodBody.append("super.onCreate();");
111+
StringBuilder methodBody = new StringBuilder()
112+
methodBody.append("protected void onCreate() {")
113+
methodBody.append("super.onCreate();")
114114
methodBody.
115-
append(getAutoLoadComCode(activators));
116-
methodBody.append("}");
117-
ctClassApplication.addMethod(CtMethod.make(methodBody.toString(), ctClassApplication));
115+
append(getAutoLoadComCode(activators))
116+
methodBody.append("}")
117+
ctClassApplication.addMethod(CtMethod.make(methodBody.toString(), ctClassApplication))
118118
} catch (Exception e) {
119119

120120
}
121121
ctClassApplication.writeFile(patch)
122122
ctClassApplication.detach()
123123

124-
System.out.println("injectApplicationCode success ");
124+
System.out.println("injectApplicationCode success ")
125125
}
126126

127127
private String getAutoLoadComCode(List<CtClass> activators) {
128-
StringBuilder autoLoadComCode = new StringBuilder();
128+
StringBuilder autoLoadComCode = new StringBuilder()
129129
for (CtClass ctClass : activators) {
130130
autoLoadComCode.append("new " + ctClass.getName() + "()" + ".onCreate();")
131131
}
@@ -137,26 +137,26 @@ public class ComCodeTransform extends Transform {
137137
private boolean isApplication(CtClass ctClass) {
138138
try {
139139
if (applicationName != null && applicationName.equals(ctClass.getName())) {
140-
return true;
140+
return true
141141
}
142142
} catch (Exception e) {
143143
println "class not found exception class name: " + ctClass.getName()
144144
}
145-
return false;
145+
return false
146146
}
147147

148148
private boolean isActivator(CtClass ctClass) {
149149
try {
150150
for (CtClass ctClassInter : ctClass.getInterfaces()) {
151151
if ("com.luojilab.component.componentlib.applicationlike.IApplicationLike".equals(ctClassInter.name)) {
152-
return true;
152+
return true
153153
}
154154
}
155155
} catch (Exception e) {
156156
println "class not found exception class name: " + ctClass.getName()
157157
}
158158

159-
return false;
159+
return false
160160
}
161161

162162
@Override

0 commit comments

Comments
 (0)