android apk手动打包

最近在研究apk的编译,有些技术点放到这里作为备注吧。

下面图是手动打包的过程图

 手动编译的流程如下:

1.生成资源文件

2.编译本地库

3.编译java代码

4.生成dex文件

5.打包资源文件

6.生成apk文件

7.对apk文件进行签名

1.生成资源文件,使用aapt生成R.java类文件: 

    aapt package -f -m -J ./gen -S res -I D:\tools\java\android-sdk-windows\platforms\android-16\android.jar -M AndroidManifest.xml

    其中  

    -f -m -J ./gen  代表按覆盖的形式在gen目录下生成带包路径的R.java

    -S res指定资源文件 

    -I D:\tools\java\android-sdk-windows-1.6_r1\platforms\android-7\android.jar 指定使用的android类

    -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

    -S  directory in which to find resources.  Multiple directories will be scanned and the first match found (left to right) will take precedence.

    -I  add an existing package to base include set

2.编译本地库,使用android SDK提供的aidl.exe把.aidl转成.java文件: 

    usage: aidl OPTIONS INPUT [OUTPUT]

           aidl --preprocess OUTPUT INPUT...

    OPTIONS:

       -I<DIR>    search path for import statements.

       -d<FILE>   generate dependency file.

       -p<FILE>   file created by --preprocess to import.

       -o<FOLDER> base output folder for generated files.

       -b         fail when trying to compile a parcelable.

    INPUT:

       An aidl interface file.

    OUTPUT:

       The generated interface files. 

3.编译java代码,编译.java类文件生成class文件

    javac -encoding utf8 -target 1.5 -bootclasspath D:\tools\java\android-sdk-windows-1.6_r1\platforms\android-7\android.jar -d bin/classes src\com\jimmy\*.java src\com\baidu\np\client\*.java gen\com\jimmy\*.java

4.生成dex文件,使用android SDK提供的dx.bat命令行脚本生成classes.dex文件: 

    dx.bat --dex --output=d:/workspace/testaa/bin/classes.dex  d:/workspace/testaa/bin/classes 

5.打包资源文件,使用Android SDK提供的aapt.exe生成资源包文件(包括res、assets、androidmanifest.xml等):

    aapt.exe package -f -M AndroidManifest.xml -S res -A assets -I D:\tools\java\android-sdk-windows-1.6_r1\platforms\android-7\android.jar -F bin\byreadreader

    将AndroidManifest.xml,res和assets文件夹中的资源文件打包生成byreadreader,用法参见1 

6.生成apk文件,第六步 生成未签名的apk安装文件:

    

    D:\tools\java\android-sdk-windows-1.6_r1\tools\apkbuilder.bat d:/workspace/testaa/misc_dj_np.apk -u -z d:/workspace/testaa/bin/byreadreader -f d:/workspace/testaa/bin/classes.dex -rf d:/workspace/testaa/src -rj d:/workspace/testaa/lib

    apkbuilder  ${output.apk.file} -u -z  ${packagedresource.file} -f  ${dex.file}  -rf  ${source.dir}  -rj  ${libraries.dir} 

    ${output.apk.file} -u 创建一个未签名的包

    -z  ${packagedresource.file} 将文件压缩,添加到包里面

    -f  ${dex.file} 将文件添加到包里面

    -rf  ${source.dir} 

    -u      Creates an unsigned package.

    -z      Followed by the path to a zip archive. Adds the content of the application package.

    -f      Followed by the path to a file. Adds the file to the application package.

    -rf     Followed by the path to a source folder. Adds the java resources found in that folder to the application

        package, while keeping their path relative to the source folder.

7.对apk文件进行签名, 使用jdk的jarsigner对未签名的包进行apk签名: 

    java -jar D:\MyWorks\component\lib\signature\sign.jar keystore/platform.x509.pem keystore/platform.pk8 misc_dj_np.apk  misc_dj_np_sined.apk

    其中

    –keystore f:\explorer\eclipse3.5\bbyread.keystore 为密钥文件

    -storepass byread002为密钥文件密码

    byread 为密钥别名 

    -keypass byread002为密钥别名密码

    -signedjar f:\explorer\byread.apk为签名后生成的apk文件 

    f:\explorer\byreadreader.apk为未签名的文件

经过以上7个步骤,就可以获得一个签名的apk了。

在反编译的时候

apktool d -f apk/xxx.apk   decode/xxx

查看decode/xxx/res/values 有三个文件 ids.xml, public.xml, string.xml

ids.xml 这个项目有多少id 都在这个ids.xml文件中声明。

public.xml  ids对应的索引。

string.xml 项目的所有string声明 都在这个文件里。

猜你喜欢

转载自youzifei.iteye.com/blog/1815571