/
Plugin.kt
174 lines (160 loc) · 6.33 KB
/
Plugin.kt
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
package com.imf.plugin.so
import com.android.build.gradle.AppExtension
import com.google.gradle.osdetector.OsDetector
import org.gradle.api.GradleException
import org.gradle.api.Plugin
import org.gradle.api.Project
import java.io.*
import java.util.Collections
import java.util.stream.Collectors
fun log(msg: Any) {
// project.logger.info("[ApkSoLibStreamlineTask]: ${msg}")
println("[SoFilePlugin]: ${msg}")
}
abstract class SoFilePlugin : Plugin<Project> {
lateinit var intermediatesDir: File;
lateinit var android: AppExtension;
lateinit var pluginConfig: SoFileExtensions
override fun apply(project: Project) {
pluginConfig = project.extensions.create("SoFileConfig", SoFileExtensions::class.java)
android = project.extensions.getByType(AppExtension::class.java)
intermediatesDir = File(project.buildDir, "intermediates")
if (pluginConfig.exe7zName.isEmpty()) {
project.apply(mapOf("plugin" to "com.google.osdetector"))
}
project.afterEvaluate {
log("enable: ${pluginConfig.enable}")
if (pluginConfig.enable == true) {
if (pluginConfig.exe7zName.isEmpty()) {
pluginConfig.exe7zName = find7zPath(project)
}
log("p7z path: ${pluginConfig.exe7zName}")
afterProjectEvaluate(it)
}
}
}
private fun find7zPath(project: Project): String {
val p7zConfig = project.configurations.create("ApkSoFileAdjustLocatorP7z") {
it.isVisible = false
it.isTransitive = false
it.setExtendsFrom(Collections.emptyList())
}
val osdetector = project.extensions.getByType(OsDetector::class.java)
val dep = project.dependencies.add(
p7zConfig.name, mapOf<String, String>(
"group" to "com.mainli",
"name" to "p7z",
"classifier" to osdetector.classifier,
"version" to "1.0.1",
"ext" to "exe"
)
)
try {
val file = p7zConfig.fileCollection(dep).singleFile
if (!file.canExecute() && !file.setExecutable(true)) {
throw GradleException("Cannot set ${file} as executable")
}
return file.absolutePath
} catch (e: Exception) {
}
val os = System.getenv("OS")?.lowercase()
if (os != null && os.contains("windows")) {
return "7z.exe"
}
return "7z"
}
protected open fun afterProjectEvaluate(project: Project) {
val defaultConfig = android.defaultConfig
pluginConfig.abiFilters = defaultConfig.ndk.abiFilters
val minSdkVersion: Int = defaultConfig.minSdkVersion?.apiLevel ?: 0
pluginConfig.neededRetainAllDependencies =
pluginConfig.forceNeededRetainAllDependencies ?: (minSdkVersion <= 23)
}
}
class SoFileTransformPlugin : SoFilePlugin() {
override fun apply(project: Project) {
super.apply(project)
android.registerTransform(SoFileTransform(pluginConfig, intermediatesDir))
}
}
@Deprecated("")
class SoFileAttachMergeTaskPlugin : SoFilePlugin() {
override fun afterProjectEvaluate(project: Project) {
super.afterProjectEvaluate(project)
val buildTypes: MutableSet<String> = android.buildTypes.stream().map { it.name }.filter {
if (pluginConfig.excludeBuildTypes.isNullOrEmpty()) {
true
} else {
!pluginConfig.excludeBuildTypes!!.contains(it)
}
}.collect(Collectors.toSet())
if (!buildTypes.isEmpty()) {
val tasks = project.tasks
buildTypes.forEach { variantName: String ->
val upperCaseName =
variantName.replaceFirstChar { if (it.isLowerCase()) it.titlecase() else it.toString() }
val taskName = "merge${upperCaseName}NativeLibs"
val mergeNativeLibsTask = tasks.findByName(taskName)
if (mergeNativeLibsTask == null) {
tasks.forEach {
if (it.name.equals(taskName)) {
it.doLast(
SoFileVariantAction(
variantName, pluginConfig, intermediatesDir
)
)
}
}
} else {
mergeNativeLibsTask.doLast(
SoFileVariantAction(
variantName, pluginConfig, intermediatesDir
)
)
}
}
}
}
}
class ApkSoFileAdjustPlugin : SoFilePlugin() {
override fun afterProjectEvaluate(project: Project) {
super.afterProjectEvaluate(project)
android.applicationVariants.all { variant ->
val variantName = variant.name
createTask(project, variantName)
}
android.buildTypes.all { buildType ->
val buildTypeName = buildType.name
createTask(project, buildTypeName)
}
android.productFlavors.all { flavor ->
val flavorName = flavor.name
createTask(project, flavorName)
}
}
fun createTask(project: Project, variantName: String) {
val capitalizeVariantName =
variantName.replaceFirstChar { if (it.isLowerCase()) it.titlecase() else it.toString() }
val taskName = "ApkSoFileAdjust${capitalizeVariantName}"
val excludeBuildTypes = pluginConfig.excludeBuildTypes
if (!excludeBuildTypes.isNullOrEmpty()) {
if (excludeBuildTypes.contains(variantName)) {
return
}
}
if (project.tasks.findByPath(taskName) == null) {
val task = project.tasks.create(
taskName,
ApkSoLibStreamlineTask::class.java,
android,
pluginConfig,
intermediatesDir,
variantName
)
task.group = "Build"
task.description = "进行so共享库处理"
task.setMustRunAfter(setOf(project.tasks.findByPath("package${capitalizeVariantName}")))
project.tasks.findByPath("assemble${capitalizeVariantName}")?.dependsOn?.add(task)
}
}
}