以下是使用梆梆收費(fèi)后的命令包,同時(shí)目前梆梆為了安全,并不支持放線上簽名,所以我們要自己多寫一個(gè)簽名的命令,那么我們直接開干:
首先確定相關(guān)配置,新建global.gradle
// 用來存放應(yīng)用中的所有配置變量,統(tǒng)一管理,而不再是每個(gè)moudle里都自己寫一份,修改起來更加的方便
ext {
// Android API 信息
android = [compileSdkVersion: 31, // 配置編譯SDK版本
buildToolsVersion: '31.0.0', // 配置編輯工具版本
minSdkVersion : 21, // 兼容的最低SDK版本
targetSdkVersion : 31, // 用于提高指定版本的設(shè)備上程序運(yùn)行體驗(yàn)
]
// 依賴包管理
dependencies = ['androidx_appcompat': 'androidx.appcompat:appcompat:1.3.0',
'constraintlayout' : 'androidx.constraintlayout:constraintlayout:2.0.4',
'annotation' : 'androidx.annotation:annotation:1.0.0',
'material' : 'com.google.android.material:material:1.2.1']
// 梆梆加固配置
jiagu = [APIKey : 'XXX',
APISecretKey : 'XXX',
zipPath : "jiagu/secapi-4.1.5-SNAPSHOT.jar",
apksigner : "jiagu/33.02/apksigner.jar",
keystoreFile : "xxxx.keystore"
]
}
創(chuàng)建加固命令,新建jiagu.gradle
import org.apache.tools.ant.taskdefs.condition.Os
// 加固后所有apk的保存路徑
def PROD_OUTPUT_PATH = "${projectDir.absolutePath}/jiagu/apk/"
// 加固前的api文件夾路徑
def PROD_APK_PATH = "${projectDir.absolutePath}/build/outputs/apk/prod/release/"
/**
* 加固
* @param config 配置加固可選項(xiàng)
* @param apkPath 要加固的文件路徑
* @param outputPath 輸出路徑
* @param automulpkg 是否自動(dòng)生成多渠道包
*/
def jiaGu(String config, String apkPath, String outputPath, boolean automulpkg) {
println "File path before hardening:${apkPath}"
println "Hardened file path:${outputPath}"
// 直接梆梆命令加固,得出未簽名的加固apk
exec {
executable = 'java'
args = ['-jar', rootProject.ext.jiagu["zipPath"], '-i', '梆梆網(wǎng)站',
'-u', '梆梆賬號(hào)', '--password', '梆梆密碼',
'-a', 'APIKey', '-c', 'APISecretKey',
'-f', '0', '-t', '策略ID',
'-p', apkPath, '-d', outputPath,
'-m', '1', '--action', 'ud']
}
// 獲取加固后的apk文件路徑
File apkFile = null
File apkFolder = file(outputPath)
// 獲取該文件夾下的所有文件
File[] apkFiles = apkFolder.listFiles()
int i = 0
for (File file : apkFiles) {
if (file.isFile() && file.getName().endsWith(".apk")) {
// 獲取后綴名為.apk的文件
apkFile = file
i++
}
}
if (i > 1) {
println("There are multiple folders under this folder apk:" + apkFile.absolutePath)
return
}
if (apkFile == null || !apkFile.exists()) {
println("Can't find the apk:" + apkFile.absolutePath)
return
}
// 獲取文件名
String newFile = ""
int position = apkFile.absolutePath.lastIndexOf(".")
if (position > 0) {
newFile = apkFile.absolutePath.substring(0, position)
}
println ("apksigner:" + rootProject.ext.jiagu["apksigner"])
println ("keystoreFile:" + rootProject.ext.jiagu["keystoreFile"])
println ("_sign.apk:" + newFile + '_sign.apk')
println ("absolutePath:" + apkFile.absolutePath)
exec {
executable = 'java'
args = ['-jar', rootProject.ext.jiagu["apksigner"],
'sign', '--verbose',
'--v1-signing-enabled', 'true', '--v2-signing-enabled', 'true',
'--ks', rootProject.ext.jiagu["keystoreFile"],
'--ks-key-alias', '別名', '--ks-pass', 'pass:密碼',
'--out',
newFile + '_sign.apk',
apkFile.absolutePath]
}
}
/**
* 檢查file并且進(jìn)行刪除
*/
private static void checkOutputDir(File apkOutputFile) {
if (apkOutputFile.exists()) {
File[] files = apkOutputFile.listFiles()
if (files != null) {
for (File file : files) {
file.delete()
}
}
} else {
apkOutputFile.mkdirs()
}
}
/**
* @return 當(dāng)前時(shí)間,用于給加固后的包名
*/
static def getCurTime() {
return new Date().format("yyyy-MM-dd-HH-mm-ss")
}
/**
* prod加固
* 執(zhí)行命令:./gradlew releaseJiaGuProd
*/
task releaseJiaGuProd(dependsOn: 'assembleProdRelease') {
group = "publish"
doLast {
File apkOutputFile = new File(PROD_OUTPUT_PATH, getCurTime())
// 檢查file并且進(jìn)行刪除
checkOutputDir(apkOutputFile)
File apkFile = null
// 加固前的文件夾
File apkFolder = file(PROD_APK_PATH)
// 獲取該文件夾下的所有文件
File[] apkFiles = apkFolder.listFiles()
int i = 0
for (File file : apkFiles) {
if (file.isFile() && file.getName().endsWith(".apk")) {
// 獲取后綴名為.apk的文件
apkFile = file
i++
}
}
if (i > 1) {
println("There are multiple folders under this folder apk:" + apkFile.absolutePath)
return
}
if (apkFile == null || !apkFile.exists()) {
println("Can't find the apk:" + apkFile.absolutePath)
return
}
// 進(jìn)行加固
jiaGu("-", apkFile.absolutePath, apkOutputFile.absolutePath, true)
}
}
最后記得在app的bulid依賴進(jìn)gradle文件
apply from: '../jiagu.gradle'
完工