Skip to content

Commit d96f742

Browse files
committed
解决生成Activity很多时候耗时过长问题
1 parent 93d0865 commit d96f742

File tree

5 files changed

+30
-127
lines changed

5 files changed

+30
-127
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ buildscript {
1111
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
1212
classpath "com.getkeepsafe.dexcount:dexcount-gradle-plugin:3.0.1"
1313
if (PLUGIN_ENABLE.toBoolean()) {
14-
classpath "com.github.qq549631030:android-junk-code:1.1.6"
14+
classpath "com.github.qq549631030:android-junk-code:1.1.7"
1515
}
1616
// NOTE: Do not place your application dependencies here; they belong
1717
// in the individual module build.gradle files

library/gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#project
22
GROUP=com.github.qq549631030
3-
VERSION_NAME=1.1.6
3+
VERSION_NAME=1.1.7
44

55
POM_ARTIFACT_ID=android-junk-code
66
POM_NAME=AndroidJunkCode

library/src/main/groovy/cn/hx/plugin/junkcode/task/AndroidJunkCodeTask.groovy

Lines changed: 28 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package cn.hx.plugin.junkcode.task
22

33
import cn.hx.plugin.junkcode.ext.JunkCodeConfig
4-
import cn.hx.plugin.junkcode.template.ManifestTemplate
54
import cn.hx.plugin.junkcode.template.ResTemplate
65
import com.squareup.javapoet.ClassName
76
import com.squareup.javapoet.JavaFile
87
import com.squareup.javapoet.MethodSpec
98
import com.squareup.javapoet.TypeSpec
10-
import groovy.text.GStringTemplateEngine
119
import org.gradle.api.DefaultTask
1210
import org.gradle.api.tasks.Input
1311
import org.gradle.api.tasks.Nested
@@ -32,11 +30,16 @@ class AndroidJunkCodeTask extends DefaultTask {
3230
@OutputDirectory
3331
File outDir
3432

33+
private List<String> activityList = new ArrayList<>()
34+
private List<String> stringList = new ArrayList<>()
35+
3536
@TaskAction
3637
void generateJunkCode() {
3738
if (outDir.exists()) {
3839
outDir.deleteDir()
3940
}
41+
activityList.clear()
42+
stringList.clear()
4043
//通过成类
4144
generateClasses()
4245
//生成资源
@@ -75,6 +78,8 @@ class AndroidJunkCodeTask extends DefaultTask {
7578
fileBuilder.build().writeTo(javaDir)
7679
}
7780
}
81+
//所有Activity生成完了
82+
generateManifest()
7883
}
7984

8085
/**
@@ -158,7 +163,7 @@ class AndroidJunkCodeTask extends DefaultTask {
158163
def fileBuilder = JavaFile.builder(packageName, typeBuilder.build())
159164
fileBuilder.build().writeTo(javaDir)
160165
}
161-
addToManifestByFileIo(className, packageName)
166+
activityList.add(packageName + "." + className)
162167
}
163168

164169
/**
@@ -172,10 +177,9 @@ class AndroidJunkCodeTask extends DefaultTask {
172177
}
173178
//生成string
174179
for (int i = 0; i < config.stringCount; i++) {
175-
def name = "${config.resPrefix.toLowerCase()}${generateName(i)}"
176-
def value = name
177-
addStringByFileIo(name, value)
180+
stringList.add("${config.resPrefix.toLowerCase()}${generateName(i)}")
178181
}
182+
generateStringsFile()
179183
}
180184

181185
/**
@@ -187,9 +191,6 @@ class AndroidJunkCodeTask extends DefaultTask {
187191
if (!drawableFile.getParentFile().exists()) {
188192
drawableFile.getParentFile().mkdirs()
189193
}
190-
if (!drawableFile.exists()) {
191-
drawableFile.createNewFile()
192-
}
193194
FileWriter writer
194195
try {
195196
writer = new FileWriter(drawableFile)
@@ -214,9 +215,6 @@ class AndroidJunkCodeTask extends DefaultTask {
214215
if (!layoutFile.getParentFile().exists()) {
215216
layoutFile.getParentFile().mkdirs()
216217
}
217-
if (!layoutFile.exists()) {
218-
layoutFile.createNewFile()
219-
}
220218
FileWriter writer
221219
try {
222220
writer = new FileWriter(layoutFile)
@@ -233,138 +231,61 @@ class AndroidJunkCodeTask extends DefaultTask {
233231

234232

235233
/**
236-
* 通过文件读写流的方式将新创建的Activity加入清单文件
237-
*
238-
* @param activityName
239-
* @param packageName
234+
* 生成Manifest
240235
*/
241-
void addToManifestByFileIo(String activityName, String packageName) {
236+
void generateManifest() {
242237
def manifestFile = new File(outDir, "AndroidManifest.xml")
243238
if (!manifestFile.getParentFile().exists()) {
244239
manifestFile.getParentFile().mkdirs()
245240
}
246-
if (!manifestFile.exists()) {
247-
def template = ManifestTemplate.TEMPLATE
248-
FileWriter writer
249-
try {
250-
writer = new FileWriter(manifestFile)
251-
writer.write(template.toString())
252-
} catch (Exception e) {
253-
e.printStackTrace()
254-
} finally {
255-
if (writer != null) {
256-
writer.close()
257-
}
258-
}
241+
StringBuilder sb = new StringBuilder()
242+
sb.append("<manifest xmlns:android=\"http://schemas.android.com/apk/res/android\">\n")
243+
sb.append(" <application>\n")
244+
for (i in 0..<activityList.size()) {
245+
sb.append(" <activity android:name=\"${activityList.get(i)}\"/>\n")
259246
}
260-
FileReader reader
247+
sb.append(" </application>\n")
248+
sb.append("</manifest>")
261249
FileWriter writer
262250
try {
263-
reader = new FileReader(manifestFile)
264-
StringBuilder sb = new StringBuilder()
265-
// 每一行的内容
266-
String line = ""
267-
while ((line = reader.readLine()) != null) {
268-
// 找到application节点的末尾
269-
if (line.contains("</application>")) {
270-
// 在application节点最后插入新创建的activity节点
271-
def binding = [
272-
packageName : packageName,
273-
activityName: activityName,
274-
]
275-
def template = makeTemplate(ManifestTemplate.ACTIVITY_NODE, binding)
276-
sb.append(template.toString() + "\n")
277-
}
278-
sb.append(line + "\n")
279-
}
280-
String content = sb.toString()
281251
writer = new FileWriter(manifestFile)
282-
writer.write(content)
252+
writer.write(sb.toString())
283253
} catch (Exception e) {
284254
e.printStackTrace()
285255
} finally {
286-
if (reader != null) {
287-
reader.close()
288-
}
289256
if (writer != null) {
290257
writer.close()
291258
}
292259
}
293260
}
294261

295262
/**
296-
* 将string写入strings.xml
297-
* @param name
298-
* @param value
263+
* 生成strings.xml
299264
*/
300-
void addStringByFileIo(String name, String value) {
301-
//生成string
265+
void generateStringsFile() {
302266
def stringFile = new File(outDir, "res/values/strings.xml")
303267
if (!stringFile.getParentFile().exists()) {
304268
stringFile.getParentFile().mkdirs()
305269
}
306-
if (!stringFile.exists()) {
307-
stringFile.createNewFile()
308-
FileWriter writer
309-
try {
310-
writer = new FileWriter(stringFile)
311-
def template = ResTemplate.TEMPLATE
312-
writer.write(template.toString())
313-
} catch (Exception e) {
314-
e.printStackTrace()
315-
} finally {
316-
if (writer != null) {
317-
writer.close()
318-
}
319-
}
270+
StringBuilder sb = new StringBuilder()
271+
sb.append("<resources>\n")
272+
for (i in 0..<stringList.size()) {
273+
sb.append("<string name=\"${stringList.get(i)}\">${stringList.get(i)}</string>\n")
320274
}
321-
FileReader reader
275+
sb.append("</resources>\n")
322276
FileWriter writer
323277
try {
324-
reader = new FileReader(stringFile)
325-
StringBuilder sb = new StringBuilder()
326-
// 每一行的内容
327-
String line = ""
328-
while ((line = reader.readLine()) != null) {
329-
// 找到resources节点的末尾
330-
if (line.contains("</resources>")) {
331-
// 在resources节点最后插入新创建的string节点
332-
def binding = [
333-
stringName : name,
334-
stringValue: value,
335-
]
336-
def template = makeTemplate(ResTemplate.STRING_NODE, binding)
337-
sb.append(template.toString() + "\n")
338-
}
339-
sb.append(line + "\n")
340-
}
341-
String content = sb.toString()
342278
writer = new FileWriter(stringFile)
343-
writer.write(content)
279+
writer.write(sb.toString())
344280
} catch (Exception e) {
345281
e.printStackTrace()
346282
} finally {
347-
if (reader != null) {
348-
reader.close()
349-
}
350283
if (writer != null) {
351284
writer.close()
352285
}
353286
}
354287
}
355288

356-
/**
357-
* 加载模板
358-
*
359-
* @param template
360-
* @param binding
361-
* @return
362-
*/
363-
static def makeTemplate(def template, def binding) {
364-
def engine = new GStringTemplateEngine()
365-
return engine.createTemplate(template).make(binding)
366-
}
367-
368289
/**
369290
* 生成名称
370291
* @param index

library/src/main/groovy/cn/hx/plugin/junkcode/template/ManifestTemplate.groovy

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

library/src/main/groovy/cn/hx/plugin/junkcode/template/ResTemplate.groovy

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package cn.hx.plugin.junkcode.template
22

33
class ResTemplate {
4-
static final def TEMPLATE = '''<resources>
5-
</resources>'''
64

75
static final def DRAWABLE = '''<vector xmlns:android="http://schemas.android.com/apk/res/android"
86
xmlns:aapt="http://schemas.android.com/aapt"
@@ -39,10 +37,6 @@ class ResTemplate {
3937
android:strokeColor="#00000000" />
4038
</vector>'''
4139

42-
43-
static final def STRING_NODE = ''' <string name="${stringName}">${stringValue}</string>'''
44-
45-
4640
static final def LAYOUT_TEMPLATE = '''<?xml version="1.0" encoding="utf-8"?>
4741
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
4842
android:layout_width="match_parent"

0 commit comments

Comments
 (0)