Skip to content

Commit

Permalink
provider 'use7z' extension for developer to decide whether repack bas…
Browse files Browse the repository at this point in the history
…e apk by 7z format when you use qigsawUploadSplit${VariantName} task to upload split apk files.
  • Loading branch information
kissonchan committed Feb 7, 2020
1 parent 0e54b09 commit d2d5298
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 42 deletions.
6 changes: 6 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ qigsawSplit {
*/
releaseSplitApk = false

/**
* Whether repack base apk with 7z format when you use qigsawUploadSplit${VariantName} task to upload split apk files.
* default value is {@code false}
*/
use7z = true

}

if (sample) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,9 @@ class QigsawAppBasePlugin extends QigsawPlugin {
qigsawInstallTask.installApkFile = apkFile
qigsawInstallTask.setGroup(QIGSAW)

boolean isSigningNeed = variant.buildType.signingConfig != null && variant.buildType.signingConfig.isSigningReady()
QigsawUploadSplitApkTask uploadSplitApkTask = project.tasks.create("qigsawUploadSplit${variantName}", QigsawUploadSplitApkTask)
uploadSplitApkTask.initArgs(oldApk == null ? null : new File(oldApk), splitUploadOutputDir, packageOutputDir, variantName)
uploadSplitApkTask.initArgs(oldApk == null ? null : new File(oldApk), splitUploadOutputDir, packageOutputDir, variantName, isSigningNeed)
uploadSplitApkTask.setGroup(QIGSAW)
//set task dependency
if (hasQigsawTask) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ class QigsawSplitExtension {
*/
boolean releaseSplitApk = false

/**
* Whether repack base apk with 7z format when you use qigsawUploadSplit${VariantName} task to upload split apk files.
* default value is {@code false}
*/
boolean use7z = false

/**
* Restrict splits working process, if you do not assign split name, this split will work on
* all processes, otherwise only work processes declared in its manifest.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class QigsawSplitExtensionHelper {
return null
}

static boolean getReleaseSplitApk(Project project) {
static boolean isReleaseSplitApk(Project project) {
try {
return project.extensions.qigsawSplit.releaseSplitApk
} catch (Throwable ignored) {
Expand All @@ -75,4 +75,12 @@ class QigsawSplitExtensionHelper {
}
return null
}

static boolean isUse7z(Project project) {
try {
return project.extensions.qigsawSplit.use7z
} catch (Throwable ignored) {
return false
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,17 @@ class FileUtils {
}
}

static void copyFile(File source, File dest)
static void copyFile(File source, File dest, boolean log)
throws IOException {
copyFile(new FileInputStream(source), new FileOutputStream(dest))
QigsawLogger.w("Succeed to copy ${source.absolutePath} to ${dest.absolutePath}")
if (log) {
QigsawLogger.w("Succeed to copy ${source.absolutePath} to ${dest.absolutePath}")
}
}

static void copyFile(File source, File dest)
throws IOException {
copyFile(source, dest, true)
}

static void closeQuietly(Closeable obj) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class QigsawAssembleTask extends DefaultTask {
File splitDependenciesOutputDir

QigsawAssembleTask() {
this.releaseSplitApk = QigsawSplitExtensionHelper.getReleaseSplitApk(project)
this.releaseSplitApk = QigsawSplitExtensionHelper.isReleaseSplitApk(project)
this.restrictWorkProcessesForSplits = QigsawSplitExtensionHelper.getRestrictWorkProcessesForSplits(project)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package com.iqiyi.qigsaw.buildtool.gradle.task

import com.android.SdkConstants
import com.iqiyi.qigsaw.buildtool.gradle.extension.QigsawSplitExtensionHelper
import com.iqiyi.qigsaw.buildtool.gradle.internal.entity.SplitDetails
import com.iqiyi.qigsaw.buildtool.gradle.internal.entity.SplitInfo
import com.iqiyi.qigsaw.buildtool.gradle.internal.tool.CommandUtils
Expand All @@ -47,13 +48,13 @@ import java.util.zip.ZipEntry

class QigsawUploadSplitApkTask extends DefaultTask {

static final String OLD_APK_EXTRACTION = "old_apk_extraction"

static final String NEW_SPLIT_INFO = "new_split_info"
static final String NEW_APKS = "new_apks"

static final String STORED_FILES = "store_files"

static final String NEW_APKS = "new_apks"
static final String NEW_SPLIT_INFO = "new_split_info"

static final String OLD_APK_EXTRACTION = "old_apk_extraction"

@InputFile
@Optional
Expand All @@ -68,11 +69,19 @@ class QigsawUploadSplitApkTask extends DefaultTask {
@OutputDirectory
File packageOutputDir

void initArgs(File oldApk, File outputDir, File packageOutputDir, String variantName) {
@Input
boolean use7z

@Input
boolean isSigningNeed

void initArgs(File oldApk, File outputDir, File packageOutputDir, String variantName, boolean isSigningNeed) {
this.oldApk = oldApk
this.variantName = variantName
this.outputDir = outputDir
this.packageOutputDir = packageOutputDir
this.use7z = QigsawSplitExtensionHelper.isUse7z(project)
this.isSigningNeed = isSigningNeed
}

@TaskAction
Expand Down Expand Up @@ -145,21 +154,13 @@ class QigsawUploadSplitApkTask extends DefaultTask {
FileUtils.copyFile(newSplitJsonFile, oldSplitJsonFile)
File newApksDir = new File(outputDir, NEW_APKS)
newApksDir.mkdirs()
Collection<File> resFiles = new ArrayList<>(0)
Collections.addAll(resFiles, new File(oldApkExtractionPath).listFiles())
File unsignedApk = new File(newApksDir, oldApk.name.replace(".apk", "_unsigned.apk"))
ZipUtils.zipFiles(resFiles, new File(oldApkExtractionPath), unsignedApk, compressData)
SplitApkSigner apkSigner = new SplitApkSigner(project, variantName)
File signedApk = new File(newApksDir, oldApk.name.replace(".apk", "_signed.apk"))
try {
apkSigner.signAPKIfNeed(unsignedApk, signedApk)
} catch (Throwable ignored) {
QigsawLogger.e("Can't find signingConfigs in app build.gradle")
}
//generate 7zip format apk files
File unsigned7zaApk = new File(newApksDir, oldApk.name.replace(".apk", "_7za_unsigned.apk"))
boolean createOk = run7zCmd("7za", "a", "-tzip", unsigned7zaApk.absolutePath, oldApkExtractionPath + File.separator + "*", "-mx9")
if (createOk) {
File signedApk
File unsignedApk
if (use7z) {
//generate 7zip format apk files
unsignedApk = new File(newApksDir, oldApk.name.replace(".apk", "_7za_unsigned_${variantName.uncapitalize()}.apk"))
signedApk = new File(newApksDir, oldApk.name.replace(".apk", "_7za_signed_${variantName.uncapitalize()}.apk"))
run7zCmd("7za", "a", "-tzip", unsignedApk.absolutePath, oldApkExtractionPath + File.separator + "*", "-mx9")
List<String> storedFiles = new ArrayList<>()
for (String name : compressData.keySet()) {
File file = new File(oldApkExtractionPath, name)
Expand All @@ -172,6 +173,7 @@ class QigsawUploadSplitApkTask extends DefaultTask {
}
}
if (storedFiles.size() > 0) {
QigsawLogger.w("Rewrite the stored files into the 7zip, file count ${storedFiles.size()}")
File storeFilesDir = new File(outputDir, STORED_FILES)
storeFilesDir.mkdirs()
for (String name : storedFiles) {
Expand All @@ -180,41 +182,44 @@ class QigsawUploadSplitApkTask extends DefaultTask {
if (parent != null && (!parent.exists())) {
parent.mkdirs()
}
FileUtils.copyFile(new File(oldApkExtractionPath, name), storeFile)
}
boolean updateOk = run7zCmd("7za", "a", "-tzip", unsigned7zaApk.absolutePath, storeFilesDir.absolutePath + File.separator + "*", "-mx0")
if (updateOk) {
File signed7zaApk = new File(newApksDir, oldApk.name.replace(".apk", "_7za_signed.apk"))
try {
apkSigner.signAPKIfNeed(unsigned7zaApk, signed7zaApk)
} catch (Throwable ignored) {
QigsawLogger.e("Can't find signingConfigs in app build.gradle")
}
FileUtils.copyFile(new File(oldApkExtractionPath, name), storeFile, false)
}
run7zCmd("7za", "a", "-tzip", unsignedApk.absolutePath, storeFilesDir.absolutePath + File.separator + "*", "-mx0")
}
} else {
signedApk = new File(newApksDir, oldApk.name.replace(".apk", "_signed_${variantName.uncapitalize()}.apk"))
unsignedApk = new File(newApksDir, oldApk.name.replace(".apk", "_unsigned_${variantName.uncapitalize()}.apk"))
Collection<File> resFiles = new ArrayList<>(0)
Collections.addAll(resFiles, new File(oldApkExtractionPath).listFiles())
ZipUtils.zipFiles(resFiles, new File(oldApkExtractionPath), unsignedApk, compressData)
}
if (isSigningNeed) {
QigsawLogger.w("'SignConfig' has been configured, start to sign apk ${unsignedApk.absolutePath}")
if (unsignedApk.exists()) {
SplitApkSigner apkSigner = new SplitApkSigner(project, variantName)
apkSigner.signAPKIfNeed(unsignedApk, signedApk)
unsignedApk.delete()
}
}
//copy target products to package output dir.
if (packageOutputDir.exists()) {
packageOutputDir.deleteDir()
}
File qigsawApksOutputDir = new File(packageOutputDir, "qigsaw_apks")
qigsawApksOutputDir.mkdirs()
packageOutputDir.mkdirs()
FileUtils.copyFile(newSplitJsonFile, new File(packageOutputDir, newSplitJsonFile.name))
if (newApksDir.listFiles() != null) {
newApksDir.listFiles().each {
FileUtils.copyFile(it, new File(qigsawApksOutputDir, it.name))
FileUtils.copyFile(it, new File(packageOutputDir, it.name))
}
}
}

static boolean run7zCmd(String... cmd) {
static void run7zCmd(String... cmd) {
try {
String cmdResult = CommandUtils.runCmd(cmd)
QigsawLogger.w("Run command successfully, result: " + cmdResult)
return true
} catch (Throwable e) {
QigsawLogger.e("'7za' command is not found, have you install 7zip?", e)
throw new GradleException("'7za' command is not found, have you install 7zip?", e)
}
return false
}
}

0 comments on commit d2d5298

Please sign in to comment.