-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathandroid_module_common.gradle
More file actions
207 lines (174 loc) · 6.44 KB
/
Copy pathandroid_module_common.gradle
File metadata and controls
207 lines (174 loc) · 6.44 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
* File: 'android_module_common.gradle'
* Location: https://raw.githubusercontent.com/yongce/AndroidLib/master/android_module_common.gradle
* Version: 2021.10.1
* All android projects can copy and include this file.
*/
import org.gradle.api.DefaultTask
import org.gradle.api.GradleException
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.tasks.InputDirectory
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction
import java.nio.file.Files
import java.nio.file.StandardCopyOption
abstract class CopyApksToAppsOutTask extends DefaultTask {
@InputDirectory
abstract DirectoryProperty getApkFolder()
@OutputDirectory
abstract DirectoryProperty getOutputDirectory()
@TaskAction
void copyApks() {
def sourceDir = apkFolder.get().asFile
if (!sourceDir.exists()) {
throw new GradleException("Cannot load APKs from ${sourceDir}")
}
def targetDir = outputDirectory.get().asFile
if (!targetDir.exists()) {
targetDir.mkdirs()
}
sourceDir.eachFileRecurse { file ->
if (file.isFile() && file.name.endsWith(".apk")) {
Files.copy(
file.toPath(),
new File(targetDir, file.name).toPath(),
StandardCopyOption.REPLACE_EXISTING
)
}
}
}
}
// Define some common closures (methods) for code share
ext.getModuleProjectCommitCount = {
String gitCommitCountCmd = "git rev-list HEAD --count"
Process process = gitCommitCountCmd.execute((String[])null, project.projectDir)
String errText = process.err.text
try {
return process.text.trim().toInteger()
} catch (Exception e) {
println String.format("Failed to execute #getModuleProjectCommitCount() with error [%s], " +
"which is caused by [%s].", e.toString(), errText)
}
}
ext.getModuleProjectLastCommitSha1 = {
String gitCommitCountCmd = "git log --format=\"%H\" -1"
String cmdResult = gitCommitCountCmd.execute((String[])null, project.projectDir).text
return cmdResult.trim().replace('\"', '')
}
ext.getRootProjectLastCommitSha1 = {
String gitCommitCountCmd = "git log --format=\"%H\" -1"
String cmdResult = gitCommitCountCmd.execute((String[])null, rootProject.projectDir).text
return cmdResult.trim().replace('\"', '')
}
ext.getBuildIdSuffix = {
if (project.hasProperty('build_id')) {
return "." + project.property('build_id')
}
return ""
}
ext.isIgnoreLintWarnings = {
if (project.hasProperty('lint_ignore_warnings')) {
return true
}
return false
}
ext.isApkSplitsEnabled = {
return project.hasProperty('enable_apk_splits')
}
ext {
calculatedVersionCode = getModuleProjectCommitCount()
buildIdSuffix = getBuildIdSuffix()
}
android {
compileSdk = versions.compileSdk
defaultConfig {
manifestPlaceholders = [
MODULE_GIT_COMMIT_SHA1: getModuleProjectLastCommitSha1(),
ROOT_GIT_COMMIT_SHA1: getRootProjectLastCommitSha1()
]
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
if (project.plugins.hasPlugin("com.android.application")) {
signingConfigs {
androidTestKey {
storeFile file("${rootDir}/aosp.keystore")
storePassword "android"
keyAlias "android.testkey"
keyPassword "android"
}
androidPlatformKey {
storeFile file("${rootDir}/aosp.keystore")
storePassword "android"
keyAlias "android.platformkey"
keyPassword "android"
}
}
buildTypes {
debug {
signingConfig = signingConfigs.androidTestKey
}
release {
signingConfig = signingConfigs.androidTestKey
minifyEnabled = true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
if (project.plugins.hasPlugin("com.android.application")) {
androidComponents {
onVariants(selector().withBuildType("release")) { variant ->
variant.outputs.each { output ->
String fileName = "${project.name}"
if (variant.flavorName != null && !variant.flavorName.isEmpty()) {
fileName += "-${variant.flavorName}"
}
String abi = output.filters.find {
it.filterType.name() == "ABI"
}?.identifier
if (abi != null && !abi.isEmpty()) {
fileName += "-${abi}"
}
fileName += "-${output.versionName.get()}-${output.versionCode.get()}"
output.outputFileName.set("${fileName}.apk")
}
File appsOutDir = rootProject.file(rootProject.ext.appsOutDir)
def copyApksTask = tasks.register("copy${variant.name.capitalize()}ApksToAppsOut", CopyApksToAppsOutTask) {
def apkArtifact = variant.artifacts.class.classLoader
.loadClass("com.android.build.api.artifact.SingleArtifact\$APK")
.INSTANCE
apkFolder.set(variant.artifacts.get(apkArtifact))
outputDirectory.set(appsOutDir)
}
String assembleTaskName = "assemble${variant.name.capitalize()}"
tasks.configureEach {
if (name == assembleTaskName) {
finalizedBy(copyApksTask)
}
}
}
}
}
lint {
textReport = true
abortOnError = true
ignoreWarnings = isIgnoreLintWarnings()
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
testOptions {
unitTests.all {
systemProperty "robolectric.enabledSdks", "34"
// All the usual Gradle options.
testLogging {
events "passed", "skipped", "failed", "standardOut", "standardError"
outputs.upToDateWhen {false}
showStandardStreams = true
}
}
unitTests {
includeAndroidResources = true
}
}
}