-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuildPlugin.gradle
More file actions
190 lines (168 loc) · 5.93 KB
/
buildPlugin.gradle
File metadata and controls
190 lines (168 loc) · 5.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import java.util.regex.Matcher
import java.util.regex.Pattern
//-----------------------------------以下为生成插件Zip包部分---------------------------------
def SCRIPT_VERSION = "1.11.0 - 20230311 by zyp"
def SCRIPT_VERSION_DESC = "适配gradle 8.0.2 和 android gradle build tools 7.4.2"
def pluginName = getPluginName()
def projectDir=project.getProjectDir().absolutePath
def jarDir = pluginName + '/jar/'
def jarName = "plugin_" + pluginName + ".jar"
def jarPath = jarDir + jarName
// 主任务,以此为基准
task buildPlugin(){
println("=== buildPlugin processing ===")
println("=== SCRIPT_VERSION $SCRIPT_VERSION ===")
println("=== SCRIPT_VERSION_DESC $SCRIPT_VERSION_DESC ===")
}
// 另一个主任务,禁用了dex编译逻辑
task buildPluginWithoutDex(){
println("=== buildPluginWithoutDex processing ===")
println("=== SCRIPT_VERSION $SCRIPT_VERSION ===")
println("=== SCRIPT_VERSION_DESC $SCRIPT_VERSION_DESC ===")
File dexDir = new File(projectDir, "$pluginName/dex")
if (dexDir.exists()){
dexDir.deleteDir()
println("=== buildPluginWithoutDex dex files has been deleted ===")
}
}
// 清理jar包
task clearJar(type: Delete) {
delete jarPath
}
// 将插件代码编译为jar包输出到插件包目录中
task tempBuildPluginJar(type: Jar, dependsOn: ['compileReleaseJavaWithJavac']) {
setArchiveFileName(jarName)
setDestinationDirectory(file(jarDir))
exclude('**/R.class')
exclude('**/R\$*.class')
exclude('**/BuildConfig.class')
}
tempBuildPluginJar.dependsOn(clearJar)
tempBuildPluginJar.doFirst{
println '== compileJars start=='
// 这里是tempBuildPluginJar任务执行之前,而compileReleaseJavaWithJavac已经执行完成,因此可以判断此时的build输出目录结构来决定classes生成在哪里了
// 4.1.2路径
String classesDir = 'build/intermediates/javac/release/classes/'
if (!file(classesDir).exists()){
// 3.x路径
classesDir = 'build/intermediates/classes/release/'
println("===tempBuildPluginJar doFirst classes dir is old version ===")
}
if (!file(classesDir).exists()){
// 无法识别的路径
println("===tempBuildPluginJar doFirst classes dir NOT FOUND ERROR!!! ===")
}
tempBuildPluginJar.from(classesDir)
println("===tempBuildPluginJar doFirst classes dir ===" + classesDir)
}
tempBuildPluginJar.doLast{
println '== compileJars end=='
}
// 编译插件代码为dex,兼容IDE打包
task tempBuildPluginDex(type: Exec){
workingDir "./$pluginName"
def argsList =["--dex","--verbose","--no-strict","--output=$projectDir/$pluginName/dex/plugin_${pluginName}_dex.jar"]
new File(projectDir,"$pluginName/jar").listFiles().each { file ->
argsList.add(file.absolutePath)
println(file.absolutePath)
}
println(argsList)
def androidSDKDir = project.android.sdkDirectory.absolutePath
def androidToolDir=androidSDKDir+'/build-tools/'+"${android.buildToolsVersion}"+'/'
if (System.getProperty('os.name').toLowerCase().contains('windows')) {
commandLine androidToolDir+"dx.bat"
} else {
commandLine androidToolDir+"dx"
}
args argsList
standardOutput = new ByteArrayOutputStream()
}
tempBuildPluginDex.onlyIf{
File jarFile = new File(jarPath)
if (!jarFile.exists()){
println "tempBuildPluginDex: jarFile is not exist!!!"
return false
}
println "tempBuildPluginDex: onlyIf true"
return true
}
tempBuildPluginDex.doFirst{
println '== dexJars start=='
File dexDir=new File(projectDir,"$pluginName/dex")
if (!dexDir.exists()){
dexDir.mkdir()
}
}
tempBuildPluginDex.doLast{
println standardOutput
println '== dexJars end=='
}
//生成插件包
task zipPlugin(type: Zip) {
println '== zipPlugin processing =='
setArchiveBaseName(pluginName + "-android-" + getPluginVersion(pluginName))
from(pluginName)
into(pluginName)
setDestinationDirectory(file('.'))
}
buildPlugin.dependsOn tempBuildPluginJar
buildPlugin.dependsOn tempBuildPluginDex
buildPlugin.dependsOn zipPlugin
tempBuildPluginDex.mustRunAfter(tempBuildPluginJar)
zipPlugin.mustRunAfter tempBuildPluginDex
zipPlugin.mustRunAfter tempBuildPluginJar
buildPluginWithoutDex.dependsOn tempBuildPluginJar
buildPluginWithoutDex.dependsOn zipPlugin
zipPlugin.mustRunAfter tempBuildPluginJar
//-----------------------------------以下是辅助方法-----------------------------------
//获取应用Id
def getApplicationId() {
return getAndroidPlugin().extension.defaultConfig.applicationId;
}
def getAndroidPlugin(){
def plugin = project.plugins.findPlugin('com.android.application') ?:
project.plugins.findPlugin('com.android.library')
return plugin
}
//获取插件版本号
def getPluginVersion(String pluginName) {
def version = ''
Pattern p = Pattern.compile("version=\"(.*?)\"")
Matcher m = p.matcher(new File(project.getProjectDir(), pluginName + "/info.xml")
.getText('UTF-8'))
m.find()
if (m.find()) {
version = m.group(1)
println(pluginName + " version: " + version)
}
return version
}
def getPluginName(){
def pluginName = ''
Pattern p = Pattern.compile("uexName=\"(.*?)\"")
File infoFile=getInfoFile()
Matcher m = p.matcher(infoFile.getText('UTF-8'))
if (m.find()) {
pluginName = m.group(1)
println("pluginName: " + pluginName)
}
return pluginName
}
def getInfoFile(){
File infoFile
project.getProjectDir().listFiles().each { file->
if (file.isDirectory()){
File[] files=file.listFiles(new FileFilter() {
@Override
boolean accept(File pathname) {
return pathname.getName().equals("info.xml")
}
})
if (files!=null&&files.length>0){
infoFile=files[0]
}
}
}
println("info file:"+infoFile.absolutePath)
return infoFile
}