- 打包Jar
jar cvf a.jar b.java c.java 將b和c打包到a.jar中
或者使用 jar cvf a.jar * 當前目錄全部打包到a.jar
-
打包 Release
./gradlew assembleRelease --stacktrace 發(fā)布Release包
如果只想為該工程下的某個模塊打包
./gradlew :newhealthylife:assembleRelease
- 生成簽名
keytool -genkey -keystore ./keyfile.keystore -keyalg RSA -validity 10000 -alias yan
//注:validity為天數(shù),keyfile為生成key存放的文件,yan為私鑰,RSA為指定的加密算法(可用RSA或DSA)
-
為包簽名
jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 –keystore $[cis.jks](簽名文件)$[/Users/mima123/IdeaProjects/cis-android/newhealthylife/build/outputs/apk/cn.com.cis.newhealthy.apk ](文件apk) $[cis.com.cn](別名) exp: jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore cis.jks /Users/mima123/IdeaProjects/cis-android/newhealthylife/build/outputs/apk/cn.com.cis.newhealthy.apk cis.com.cn
-
驗證簽名
jarsigner -verbose -certs -verify /Users/mima123/IdeaProjects/cis-android/newhealthylife/build/outputs/apk/cn.com.cis.newhealthy.apk
apksigner verify -v xxx.apk //查看簽名信息
android有自帶簽名的DOS命令 : jarsigner -verbose -keystore [您的私鑰存放路徑] -signedjar [簽名后文件存放路徑] [未簽名的文件路徑] [您的證書名稱]
此命令各參數(shù)含義如下:
jarsigner -verbose -keystore zdd.keystore -signedjar aaa.apk
bbb.apk xhwl
jarsigner -verbose:簽名命令標識符。
-keystore:后面跟著的是你簽名使用的密鑰文件(keystore)的絕對路徑。
-signedjar:此后有三個參數(shù):
參數(shù)一:簽名后生成的apk文件所要存放的路徑。
參數(shù)二:未簽名的apk文件的存放路徑。
參數(shù)三:你的證書名稱,通俗點說就是你keystore文件的別名,那這個別名怎么查看?其實就是在你eclipse進行簽名打包時的Alias的值。
- Apktool
apktool
apktool d -r -f xx. apk //解包
apktool b xx.apk //構(gòu)包
- zipalign
zipalign -v 4 in.apk out.apk //4字節(jié)對齊優(yōu)化
zipalign -c -v 4 in.apk //檢查APK是否對齊
//zipalign可以在V1簽名后執(zhí)行 但zipalign不能在V2簽名后執(zhí)行,只能在V2簽名之前執(zhí)行!!!
- 驗證zipalign對齊
zipalign -c -v 4 xxx.apk //在Sdk\build-tools\28.0.0中
通過properties傳遞簽名信息
1、在module的根目錄下新建signing.properties文件
STORE_FILE = ./keystore.jks
STORE_PASSWORD = your password
KEY_ALIAS =
your password
KEY_PASSWORD = your password
注意:此處沒有”“
2、在module的build.gradle中創(chuàng)建
android {
signingConfigs {
release {
storeFile
storePassword
keyAlias
keyPassword
}
}
//調(diào)用配置文件
getSigningProperties()
}
buildTypes {
debug {
minifyEnabled true
zipAlignEnabled false
shrinkResources false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
release {
minifyEnabled true //混淆
zipAlignEnabled true //Zipalign優(yōu)化
shrinkResources true // 移除無用的resource文件
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
**//注意要添加簽名
signingConfig signingConfigs.release**
//自定義apk名字
applicationVariants.all { variant ->
variant.outputs.each { output ->
output.outputFile = new File(
output.outputFile.parent,
"應用前綴${variant.buildType.name}-${defaultConfig.versionName}-${releaseTime()}-${variant.productFlavors[0].name}.apk".toLowerCase())
}
}
}
}
如果需要時間的話,需要增加一個函數(shù)releaseTime()
//獲取時間
def releaseTime() {
return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))
}
注意:函數(shù)需要放在android{}外面
3、讀取配置文件
///讀取簽名配置文件 這個需要放在android{}外面
def getSigningProperties(){
def propFile = file('signing.properties')
if (propFile.canRead()){
def Properties props = new Properties()
props.load(new FileInputStream(propFile))
if (props!=null && props.containsKey('STORE_FILE') && props.containsKey('STORE_PASSWORD') &&props.containsKey('KEY_ALIAS') && props.containsKey('KEY_PASSWORD')) {
android.signingConfigs.release.storeFile = file(props['STORE_FILE'])
android.signingConfigs.release.storePassword = props['STORE_PASSWORD']
android.signingConfigs.release.keyAlias = props['KEY_ALIAS']
android.signingConfigs.release.keyPassword = props['KEY_PASSWORD']
} else {
println 'signing.properties found but some entries are missing'
android.buildTypes.release.signingConfig = null
}
}else {
println 'signing.properties not found'
android.buildTypes.release.signingConfig = null
}
}
4、命令打包
//這個是打包所有渠道release版本./gradlew assembleRelease
//如果你只要打某個渠道的./gradlew assembleBaiduRelease
三:總結(jié)
1、打包后的apk文件在app–>build–>outputs—>apk中2、使用gradlew時可能出現(xiàn)沒有找到該命令,需要chmod 755 gradlew