APK的组成结构

Quibbler 2021-5-11 753

APK的组成结构


        通过上一篇APK的构建过程了解了Apk的构建,通过AAPTapksignerzipalign等工具执行一系列流程,最终生成Android应用程序安装包,扩展名为.apk(全称:Android Package,Android应用程序包),对应的MIME类型为application/vnd.android.package-archive,MIME见解媒体类型MIME一文。

        


1、APK的组成

        了解一下Apk内部是由哪些组成。APK文件本质上其实是一个压缩包,在进行Apk的反编译时就需要将Apk先解压,参考另一篇APK反编译


1.1、查看Apk内容

        可以用压缩工具打开查看其内部组成目录。

        AndroidStudio用到了SDK中的apkanalyzer,打开菜单Build > Analyze Apk...选择要分析的Apk文件,或者用最简单的方式直接将APK拖到AS中即可打开。

        

1.2、目录结构说明

        从上面可以看到,一个Apk通常包含以下Android程序所必需的文件和文件夹:

        META-INF/:包含.RSA、.SF、MANIFEST.MF。其中.RSA是开发者利用私钥对APK进行签名的签名文件,.SF和MANIFEST.MF记录了文件中文件的SHA-1哈希值。

        lib/:在特定设备体系结构(armeabi-v7a,x86等)上运行的Native库,通常做法是只用armeabi-v7a或者arm64-v8a减少Apk体积。

        res/:存放未编译为resources.arsc的资源文件(例如图像)

        assets/:与应用捆绑在一起的原始资源文件,例如游戏扩展包obb

        AndroidManifest.xml:描述应用的名称,版本和权限等全局信息

        classes.dex:字节码可执行文件,class.dex是由Java字节码转换的Dalvik字节码。

        resources.arsc:在Apk构建过程中由AAPT编译的资源文件(例如字符串等)

        


2、apkanalyzer分析APk

        还是用专业的APK分析工具apkanalyzer打开,详见Android Studio > apkanalyzerapkanalyzer../Android/Sdk/tools/bin目录下。


2.1、apkanalyzer分析APK

        直接输入apkanalyzer查看可用命令,详细命令介绍详见apkanalyzer > commands

$ ./apkanalyzer
Subject must be one of: apk, files, manifest, dex, resources

apk summary              Prints the application Id, version code and version name.
apk file-size            Prints the file size of the APK.
apk download-size        Prints an estimate of the download size of the APK.
apk features             Prints features used by the APK.
apk compare              Compares the sizes of two APKs.

files list               Lists all files in the zip.
files cat                Prints the given file contents to stdout

manifest print           Prints the manifest in XML format
manifest application-id  Prints the application id.
manifest version-name    Prints the version name.
manifest version-code    Prints the version code.
manifest min-sdk         Prints the minimum sdk.
manifest target-sdk      Prints the target sdk
manifest permissions     Prints a list of used permissions
manifest debuggable      Prints if the app is debuggable

dex list                 Prints a list of dex files in the APK
dex references           Prints number of references in dex files
dex packages             Prints the class tree from DEX.
P,C,M,F: indicates       packages, classes methods, fields
x,k,r,d: indicates       removed, kept, referenced and defined nodes
dex code                 Prints the bytecode of a class or method in smali format

resources packages       Prints a list of packages in resources table
resources configs        Prints a list of configurations for a type
resources value          Prints the given resource's value
resources names          Prints a list of resource names for a type
resources xml            Prints the human readable form of a binary XML

        用法如下,输入参数和可选参数,最后是apk文件名。

Usage:
apkanalyzer [global options] <subject> <verb> [options] <apk> [<apk2>]

        可选参数,目前只有一个--human-readable,将信息以阅读友好的形式输出。

Option            Description
------            -----------
--human-readable  Print sizes in human readable format


2.2、apkanalyzer命令实践

        纸上得来终觉浅,绝知此事要躬行,进入Android SDK目录,apkanalyzer就在../Android/Sdk/tools/bin目录下。

        尝试apkanalyzer的第一个命令选项,似乎有问题( ・´ω`・ ),SDK出bug也是常见了。

$ ./apkanalyzer apk summary file.apk
unknown 0       ?

        使用apk download-size命令显示apk下载大小:

$ ./apkanalyzer apk download-size 今日头条.apk
41290813

        上面显示的是大小单位是B,加上--human-readable可选参数,将显示为Mb单位:

$ ./apkanalyzer --human-readable apk download-size 今日头条.apk
39.4MB

        使用dex查看.dex相关内容,加上list选项查看dex分包列表:

$ ./apkanalyzer dex list file.apk
classes3.dex
classes2.dex
classes.dex

        还可以查看Apk支持的最小SDK,使用manifest min-sdk选项。19也就是Android 4.4,关于API和Android版本对应关系,详见Android各版本对应的SDK版本

$ ./apkanalyzer manifest min-sdk 今日头条.apk
19

        

不忘初心的阿甘
最新回复 (0)
    • 安卓笔记本
      2
        登录 注册 QQ
返回
仅供学习交流,切勿用于商业用途。如有错误欢迎指出:fluent0418@gmail.com