Skip to content

Commit 208ada8

Browse files
author
Mihail Slavchev
committed
update gradle to use compile${variantName}Sources tasks in order to avoid resigning the *.apk file
1 parent 62b7d0e commit 208ada8

File tree

1 file changed

+34
-96
lines changed

1 file changed

+34
-96
lines changed

build/project-template-gradle/build.gradle

Lines changed: 34 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -79,20 +79,10 @@ def renameResultApks (variant) {
7979
def apkDirectory = output.packageApplication.outputFile.parentFile
8080
def apkNamePrefix = rootProject.name + "-" + variant.buildType.name
8181

82-
if (output.zipAlign) {
83-
if (project.ext.selectedBuildType == variant.buildType.name) {
84-
output.zipAlign.dependsOn(buildMetadata)
85-
}
86-
87-
name = apkNamePrefix + ".apk"
88-
output.outputFile = new File(apkDirectory, name);
89-
}
90-
9182
name = apkNamePrefix + "-unaligned.apk"
9283
output.packageApplication.outputFile = new File(apkDirectory, name);
9384

9485
if (output.packageApplication && (project.ext.selectedBuildType == variant.buildType.name)) {
95-
buildMetadata.dependsOn(output.packageApplication)
9686
project.ext.currentApkFile = output.packageApplication.outputFile.getAbsolutePath().replace('\\', '/')
9787
}
9888
}
@@ -141,6 +131,14 @@ android {
141131
applicationVariants.all { variant ->
142132
renameResultApks(variant)
143133
}
134+
135+
136+
applicationVariants.all { variant ->
137+
def variantName = variant.name.capitalize()
138+
def compileSourcesTaskName = "compile${variantName}Sources"
139+
def compileSourcesTask = project.tasks.findByName(compileSourcesTaskName)
140+
compileSourcesTask.finalizedBy "buildMetadata"
141+
}
144142
}
145143

146144
repositories {
@@ -323,7 +321,7 @@ task cleanLocalAarFiles(type: Delete) {
323321
}
324322

325323
task ensureMetadataOutDir {
326-
def outputDir = file("$rootDir/metadata/output")
324+
def outputDir = file("$rootDir/metadata/output/assets/metadata")
327325
outputDir.mkdirs()
328326
}
329327

@@ -348,7 +346,7 @@ task collectAllJars {
348346
}
349347

350348
metadataParams.add("metadata-generator.jar")
351-
metadataParams.add("../metadata/output")
349+
metadataParams.add("../metadata/output/assets/metadata")
352350
for(def i = 0; i < allJarPaths.size(); i++) {
353351
metadataParams.add(allJarPaths.get(i));
354352
}
@@ -382,7 +380,7 @@ task buildMetadata (type: JavaExec) {
382380
inputs.files(allJarPaths)
383381
inputs.dir("build/intermediates/classes")
384382

385-
outputs.files("metadata/output/treeNodeStream.dat", "metadata/output/treeStringsStream.dat", "metadata/output/treeValueStream.dat")
383+
outputs.files("metadata/output/assets/metadata/treeNodeStream.dat", "metadata/output/assets/metadata/treeStringsStream.dat", "metadata/output/assets/metadata/treeValueStream.dat")
386384

387385
doFirst {
388386
workingDir "build-tools"
@@ -391,93 +389,32 @@ task buildMetadata (type: JavaExec) {
391389

392390
args metadataParams.toArray()
393391
}
394-
392+
395393
doLast {
396-
copy {
397-
from "metadata/output"
398-
into "src/main/assets/metadata"
399-
}
400-
401-
def zip_properties = new HashMap<String, String>()
402-
zip_properties.put("create", "true")
403-
zip_properties.put("encoding", "UTF-8")
404-
405-
def currentApk = project.ext.currentApkFile
406-
def currentApkAsEncoded = currentApk.replace(" ", "%20")
407-
def urlPrefix = "jar:file:" + (isWinOs ? "/" : "")
408-
409-
def zip_disk = URI.create(urlPrefix + currentApkAsEncoded);
410-
411-
def zipfs
412-
def pathInZipfile
413-
try {
414-
zipfs = java.nio.file.FileSystems.newFileSystem(zip_disk, zip_properties)
415-
416-
def filesToDelete = ["assets/metadata/treeNodeStream.dat", "assets/metadata/treeStringsStream.dat", "assets/metadata/treeValueStream.dat",
417-
"META-INF/CERT.RSA", "META-INF/CERT.SF", "META-INF/MANIFEST.MF"]
418-
419-
for (def file: filesToDelete) {
420-
pathInZipfile = zipfs.getPath(file)
421-
if (java.nio.file.Files.exists(pathInZipfile)) {
422-
java.nio.file.Files.delete(pathInZipfile)
423-
}
424-
}
425-
426-
pathInZipfile = zipfs.getPath("assets/metadata")
427-
if (!java.nio.file.Files.exists(pathInZipfile)) {
428-
java.nio.file.Files.createDirectory(pathInZipfile)
429-
}
430-
431-
def metadataFiles = ["treeNodeStream.dat", "treeStringsStream.dat", "treeValueStream.dat"]
432-
for (def mdFile: metadataFiles) {
433-
pathInZipfile = java.nio.file.Files.createFile(zipfs.getPath("assets/metadata/" + mdFile))
434-
435-
def mdFilePath = java.nio.file.Paths.get("$rootDir/metadata/output/" + mdFile)
436-
def data = java.nio.file.Files.readAllBytes(mdFilePath)
437-
438-
java.nio.file.Files.write(pathInZipfile, data, java.nio.file.StandardOpenOption.WRITE)
439-
}
440-
} finally {
441-
if (zipfs != null) {
442-
zipfs.close()
443-
}
444-
}
445-
446-
def androidHome = new File(System.properties['user.home'], ".android")
447-
def keyStoreFile = new File(androidHome, "debug.keystore")
448-
449-
def storePass
450-
def keyPass
451-
def keyStorePath
452-
def alias
453-
454-
if (project.ext.selectedBuildType.equals("release")) {
455-
storePass = project.password
456-
keyPass = project.ksPassword
457-
keyStorePath = project.ksPath
458-
alias = project.alias
459-
} else {
460-
storePass = "android"
461-
keyPass = "android"
462-
keyStorePath = keyStoreFile.getAbsolutePath()
463-
alias = "androiddebugkey"
464-
}
465-
def jarSignerPath = new File(System.getenv("JAVA_HOME"), "bin/jarsigner").getAbsolutePath()
466-
467-
def cmdParams = new ArrayList<String>([jarSignerPath, "-sigalg", "SHA1withRSA", "-digestalg", "SHA1", "-storepass", storePass, "-keypass", keyPass, "-keystore", keyStorePath, currentApk, alias])
468-
394+
def tmpResourceAPK = new File("${buildDir}/intermediates/res/resources-${project.ext.selectedBuildType}.ap_")
395+
396+
def aaptCommand = "${android.getSdkDirectory().getAbsolutePath()}/build-tools/${android.buildToolsVersion}/aapt"
469397
if (isWinOs) {
470-
cmdParams.add(0, "/c")
471-
cmdParams.add(0, "cmd")
398+
aaptCommand += ".exe"
472399
}
473-
400+
401+
println ">>aapt found=" + new File(aaptCommand).exists()
402+
403+
def tmpAPKPath = tmpResourceAPK.getPath()
404+
405+
def removeCmdParams = new ArrayList<String>([aaptCommand, "remove", tmpAPKPath, "assets/metadata/treeNodeStream.dat", "assets/metadata/treeStringsStream.dat", "assets/metadata/treeValueStream.dat"])
406+
474407
exec {
475-
if (isWinOs) {
476-
commandLine cmdParams.toArray()
477-
}
478-
else {
479-
commandLine cmdParams.toArray()
480-
}
408+
ignoreExitValue true
409+
workingDir "metadata/output"
410+
commandLine removeCmdParams.toArray()
411+
}
412+
413+
def addCmdParams = new ArrayList<String>([aaptCommand, "add", tmpAPKPath, "assets/metadata/treeNodeStream.dat", "assets/metadata/treeStringsStream.dat", "assets/metadata/treeValueStream.dat"])
414+
415+
exec {
416+
workingDir "metadata/output"
417+
commandLine addCmdParams.toArray()
481418
}
482419
}
483420
}
@@ -494,6 +431,7 @@ task deleteExplodedAarFolder (type: Delete) {
494431
}
495432

496433

434+
497435
////////////////////////////////////////////////////////////////////////////////////
498436
////////////////////////////// OPTIONAL TASKS //////////////////////////////////////
499437
////////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)