修改Android的apk包
解包
使用apktool工具,对apk进行反编译并解包 注:需使用java1.8来执行jar包
set PATH=E:\NVPACK\jdk1.8.0_77\bin;%PATH% java -jar %~dp0\apktool\apktool.jar d D:\MyTest1-Android-Test-arm64.apk // 对MyTest1-Android-Test-arm64.pak进行解包
如果不需要反编译java代码,需带上-s参数;不需要反编译资源,需带上-r参数
set PATH=E:\NVPACK\jdk1.8.0_77\bin;%PATH% java -jar %~dp0\apktool\apktool.jar d -s -r D:\MyTest1-Android-Test-arm64.apk // 不反编译java代码和资源,来对MyTest1-Android-Test-arm64.pak进行解包
apktool使用说明详见:
F:\GameRepack\apktool\apktool>set PATH=E:\NVPACK\jdk1.8.0_77\bin;%PATH% F:\GameRepack\apktool\apktool>java -jar apktool.jar Apktool v2.5.0 - a tool for reengineering Android apk files with smali v2.4.0 and baksmali v2.4.0 Copyright 2010 Ryszard Wi?niewski <brut.alll@gmail.com> Copyright 2010 Connor Tumbleson <connor.tumbleson@gmail.com> usage: apktool -advance,--advanced prints advance information. -version,--version prints the version then exits usage: apktool if|install-framework [options] <framework.apk> -p,--frame-path <dir> Stores framework files into <dir>. -t,--tag <tag> Tag frameworks using <tag>. usage: apktool d[ecode] [options] <file_apk> -f,--force Force delete destination directory. -o,--output <dir> The name of folder that gets written. Default is apk.out -p,--frame-path <dir> Uses framework files located in <dir>. -r,--no-res Do not decode resources. -s,--no-src Do not decode sources. -t,--frame-tag <tag> Uses framework files tagged by <tag>. usage: apktool b[uild] [options] <app_path> -f,--force-all Skip changes detection and build all files. -o,--output <dir> The name of apk that gets written. Default is dist/name.apk -p,--frame-path <dir> Uses framework files located in <dir>. For additional info, see: https://ibotpeaches.github.io/Apktool/ For smali/baksmali info, see: https://github.com/JesusFreke/smali
apktool更详细的使用说明:
F:\GameRepack\apktool\apktool>java -jar apktool.jar -advance Apktool v2.5.0 - a tool for reengineering Android apk files with smali v2.4.0 and baksmali v2.4.0 Copyright 2010 Ryszard Wi?niewski <brut.alll@gmail.com> Copyright 2010 Connor Tumbleson <connor.tumbleson@gmail.com> Apache License 2.0 (https://www.apache.org/licenses/LICENSE-2.0) usage: apktool [-q|--quiet OR -v|--verbose] -advance,--advanced prints advance information. -version,--version prints the version then exits usage: apktool [-q|--quiet OR -v|--verbose] if|install-framework [options] <framework.apk> -p,--frame-path <dir> Stores framework files into <dir>. -t,--tag <tag> Tag frameworks using <tag>. usage: apktool [-q|--quiet OR -v|--verbose] d[ecode] [options] <file_apk> -api,--api-level <API> The numeric api-level of the file to generate, e.g. 14 for ICS. -b,--no-debug-info don't write out debug info (.local, .param, .line, etc.) -f,--force Force delete destination directory. --force-manifest Decode the APK's compiled manifest, even if decoding of resources is set to "false". -k,--keep-broken-res Use if there was an error and some resources were dropped, e.g. "Invalid config flags detected. Dropping resources", but you want to decode them anyway, even with errors. You will have to fix them manually before building. -m,--match-original Keeps files to closest to original as possible. Prevents rebuild. --no-assets Do not decode assets. -o,--output <dir> The name of folder that gets written. Default is apk.out --only-main-classes Only disassemble the main dex classes (classes[0-9]*.dex) in the root. -p,--frame-path <dir> Uses framework files located in <dir>. -r,--no-res Do not decode resources. -s,--no-src Do not decode sources. -t,--frame-tag <tag> Uses framework files tagged by <tag>. usage: apktool [-q|--quiet OR -v|--verbose] b[uild] [options] <app_path> -a,--aapt <loc> Loads aapt from specified location. -api,--api-level <API> The numeric api-level of the file to generate, e.g. 14 for ICS. -c,--copy-original Copies original AndroidManifest.xml and META-INF. See project page for more info. -d,--debug Sets android:debuggable to "true" in the APK's compiled manifest -f,--force-all Skip changes detection and build all files. -nc,--no-crunch Disable crunching of resource files during the build step. -o,--output <dir> The name of apk that gets written. Default is dist/name.apk -p,--frame-path <dir> Uses framework files located in <dir>. --use-aapt2 Upgrades apktool to use experimental aapt2 binary. usage: apktool [-q|--quiet OR -v|--verbose] publicize-resources <file_path> usage: apktool [-q|--quiet OR -v|--verbose] empty-framework-dir [options] -f,--force Force delete destination directory. -p,--frame-path <dir> Stores framework files into <dir>. usage: apktool [-q|--quiet OR -v|--verbose] list-frameworks [options] -p,--frame-path <dir> Stores framework files into <dir>. For additional info, see: https://ibotpeaches.github.io/Apktool/ For smali/baksmali info, see: https://github.com/JesusFreke/smali
打包
使用apktool将某个目录打包成apk
set PATH=E:\NVPACK\jdk1.8.0_77\bin;%PATH% java -jar %~dp0\apktool\apktool.jar b D:\MyTest1-Android-Test-arm64 // 将D:\MyTest1-Android-Test-arm64目录中的文件打包成apk
重签名
使用build-tools中的apksigner.bat工具对apk进行重签名
set PATH=E:\NVPACK\jdk1.8.0_77\bin;%PATH% set ApkSigner=E:\NVPACK\android-sdk-windows\build-tools\27.0.3\apksigner.bat set GameKeyStore=D:\Test1-key.keystore set GamePassword=Test111 set AnlignedName=Test1 java -Xmx4096M -Xss4m -Djava.ext.dirs="%ApkSignerDir%" -jar "%ApkSignerDir%\apksigner.jar" sign --ks %GameKeyStore% --ks-pass pass:%GamePassword% --ks-key-alias %AnlignedName% --in %~dp0\MyTest1-Android-Test-arm64\dist\MyTest1-Android-Test-arm64.apk --out %~dp0\MyTest1-Android-Test-arm64_signed.apk
注1:build-tools 27.0.3可以从这里下载
注2:由于apk有2GB,size比较大,会导致java进程崩溃,因此通过-Xmx来设置最大java堆的大小,通过-Xss设置java线程栈的大小
resources.arsc of installed APKs to be stored uncompressed and aligned on a 4-byte boundary
如果报如下错误,需要在打包与重签名中间,增加一个压缩对齐步骤
adb: failed to install F:\GameRepack\apktool\MyTest1-Android-Test-arm64.apk: Failure [-124: Failed parse during installPackageLI: Targeting R+ (version 30 and above) requires the resources.arsc of installed APKs to be stored uncompressed and aligned on a 4-byte boundary]
注:TargetSDKVersion从28改成30之后,出现此问题
压缩对齐
"%ApkSignerDir%\zipalign.exe" -f 4 %~dp0\MyTest1-Android-Test-arm64\dist\MyTest1-Android-Test-arm64.apk %~dp0\MyTest1-Android-Test-arm64\dist\MyTest1-Android-Test-arm64_align.apk
注:压缩对齐之后,再对%~dp0\MyTest1-Android-Test-arm64\dist\MyTest1-Android-Test-arm64_align.apk进行重签名
对于keystore秘钥可使用jdk1.8中的keytool工具来创建
set PATH=E:\NVPACK\jdk1.8.0_77\bin;%PATH% keytool -genkey -v -keystore Test1-key.keystore -alias Test1 -keyalg RSA -keysize 2048 -validity 100000