Records of problems encountered when compiling apk

1. Unzip the .tar.gz file

tar -zxvf 文件名.tar.gz

Wherein,文件名.tar.gz is the name of the .tar.gz file you want to decompress. The meanings of the decompression command options are as follows:

  • -z: Indicates using the gzip algorithm for decompression.
  • -x: Indicates decompression operation.
  • -v: Optional, indicating to display detailed information during the decompression process.
  • -f: followed by the name of the file to be decompressed.

Please ensure that the tar tool has been installed and the specified file name is correct before executing the decompression command. The unzipped files will be extracted to the current directory or the specified output directory.

2. The apk is decompressed and contains CERT.RSA

When you unzip an APK file, CERT.RSA is the signing certificate file of the APK file. After the APK file is signed, the integrity and authenticity of the source of the APK can be verified.

The CERT.RSA file contains the public key certificate and digital signature information used to verify the APK signature. It is usually located under the META-INF directory.

If you wish to view the APK’s signing certificate information, you can perform the following steps:

  1. Use the zip tool or command line to unzip the APK file.
  2. Find the META-INF directory in the unzipped file.
  3. In the META-INF directory you will find the CERT.RSA file.

Note that the CERT.RSA file is a binary file, so its contents cannot be read directly. If you want to view certificate information, you can use Java Keytool or other certificate tools to parse and view the CERT.RSA file.

3. Process of manually compiling APK

Note: Depending on the Android SDK version, the dx.bat and apkbuilder.bat files may be missing under tools after installation. You can also use them normally by upgrading the SDK or downloading them separately and placing them in the corresponding directory;
1. The process of manually compiling APK is as follows:

1. Generate resource files: Use aapt.exe provided by Android SDK to generate R.java class files

2. Compile local library: Use aidl.exe provided by Android SDK to convert .aidl into .java file (if there is no aidl, skip this step)

3. Compile java code: Use javac.exe provided by JDK to compile .java class file to generate class file

4. Generate dex file: Use the dx.bat command line script provided by Android SDK to generate classes.dex file

5. Pack resource files: Use aapt.exe provided by Android SDK to generate resource package files (including res, assets, androidmanifest.xml, etc.)

6. Generate apk file: Use apkbuilder.bat provided by Android SDK to generate an unsigned apk installation file

7. Sign the apk file: Use jdk's jarsigner.exe to sign the unsigned package apk

2. Detailed steps

1), preparation work

A、安装好JDK,配置环境变量;
B、将android-sdk-windows\platform-tools和 android-sdk-windows\tools 添加到CLASSPATH 环境变量中;
C、打开CMD命令行界面,将当前目录切换到工程所在目录;
D、准备一个目录,以下例子是以在eclipse中创建的Android test project项目,名称Test;

2) Generate resource files and use aapt to generate R.java class files. The path where aapt is located is android-sdk-windows\platform-tools\aapt.exe:

E:\Users\Administrator\workspace\Test>aapt package -f -m -J gen -S res -M AndroidManifest.xml -I E:\android-sdk_r23.0.2-windows\android-sdk-windows\platforms\android-18\android.jar

illustrate:

  • -f -m -J gen means to generate R.java with package path in the gen directory in the form of overwriting;
  • -f force overwrite of existing files;
  • -m make package directories under location specified by -J
  • -J specify where to output R.java resource constant definitions
  • -J is followed by the generated directory; -S is followed by the resource directory; -M is followed by AndroidManifest.xml; -I is followed by the AndroidAPI jar used;

3) javac.exe compiles the .java class file to generate a class file, the path where javac is locatedC:\Program Files\Java\jdk1.6.0_43\bin\javac.exe

E:\Users\Administrator\workspace\Test>javac -encoding utf-8 -target 1.6 -d bin -bootclasspath E:\android-sdk_r23.0.2-windows\android-sdk-windows\platforms\android-18\android.jar gen\android\support\v7\appcompat\test\*.java src\android\support\v7\appcompat\test\*.java

Description: -target: jdk version; -d: specify the location where the generated class file is stored; -bootclasspath: overwrite the location of the boot class file; -encoding: followed by character encoding to avoid garbled characters;

4) Use the dx.bat command line script to generate the classes.dex file, the path where dx.bat is located android-sdk-windows\tools\dx.bat

E:\Users\Administrator\workspace\Test>dx.bat --dex --output=E:\Users\Administrator\workspace\Test\bin\classes.dex E:\Users\Administrator\workspace\Test\bin\classes

Description: –output: the storage path of the generated dex file; finally bring the directory where the class file is located, no need to add a package directory

5) Use aapt.exe to generate the resource package file. The path where aapt.exe is located is android-sdk_r23.0.2-windows\android-sdk-windows\platform-tools\aapt.exe

E:\Users\Administrator\workspace\Test>aapt.exe package -f -M AndroidManifest.xml -S res -A assets -I E:\android-sdk_r23.0.2-windows\android-sdk-windows\platforms\android-18\android.jar -F bin\resourcesLater

Description: -S: resource directory; -M: AndroidManifest.xml file; -I: jar file for import; -F: followed by the storage location of the generated resource file. The name of the generated resource file is newly created by yourself.

6) Use apkbuilder.bat to generate an unsigned apk installation file. The path where apkbuilder.bat is located is android-sdk_r23.0.2-windows\android-sdk-windows\tools\apkbuilder.bat.
(When using this command: it will prompt that the tool is outdated and Google does not recommend its use. The recommended one is: com.android.ant.ApkBuilderTask, but the apk can still be generated successfully) a>

E:\Users\Administrator\workspace\Test>apkbuilder E:\Users\Administrator\workspace\Test\bin\HelloWorld.apk -u -z E:\Users\Administrator\workspace\Test\bin\resourcesLater -f E:\Users\Administrator\workspace\Test\bin\classes.dex -rf E:\Users\Administrator\workspace\Test\src

THIS TOOL IS DEPRECATED. See --help for more information.
Description: -z: resource packaging file; -f: class packaging file; -rf: source code directory;

(Note: This step did not successfully sign the apk)
7) Use jarsigner.exe to sign the apk of the unsigned package. The path where jarsigner.exe is located is C:\Program Files \Java\jdk1.6.0_43\bin\jarsigner.exe

E:\Users\Administrator\workspace\Test>jarsigner -verbose -keystore E:\Users\zhengshu_apk -signedjar E:\Users\Administrator\workspace\Test\bin\HelloWorld.apk E:\Users\Administrator\workspace\Test\bin\HelloWorld.apk.mydeystore

Description: -keystore: keystore file; -signedjar: signed file; followed by unsigned files and key aliases

usage:

 jarsigner [选项] jar 文件别名
 jarsigner -verify [选项] jar 文件
  • [-keystore] Keystore location
  • [-storepass <password>] Password for keystore integrity
  • [-storetype <type>] keystore type
  • [-keypass <password>] Password for the private key (if different)
  • [-sigfile <file>] name of .SF/.DSA file
  • [-signedjar <file>] The name of the signed JAR file
  • [-digestalg <algorithm>] Name of the digest algorithm
  • [-sigalg <algorithm>] name of the signature algorithm
  • [-verify] Verify signed JAR files
  • [-verbose] Output detailed information when signing/verifying
  • [-certs] Show certificates when outputting details and verification
  • [-tsa] timestamp authority location
  • [-tsacert <alias>] Timestamp authority's public key certificate
  • [-altsigner <class>] The class name of the alternative signature mechanism
  • [-altsignerpath <path list>] Location of alternative signing mechanisms
  • [-internalsf] Include .SF files within signature blocks
  • [-sectionsonly] Do not compute the hash of the entire list
  • [-protected] Keystore protected authentication path
  • [-providerName <name>] provider name
  • [-providerClass <class> name of the encryption service provider
  • [-providerArg <parameters>]] … main class file and constructor parameters

Guess you like

Origin blog.csdn.net/weixin_43233219/article/details/133811458