Android App 的构成成分以及 apk 文件解读方法

Tags: Android 

目录

Android App 的构成

Android App 由三部分组成:AndroidManifest.xml、组件代码、资源文件。均位于 Andorid 项目的 app/src/main 目录中。

AndroidManifest.xml

AndroidManifest.xml 是 xml 格式的应用描述文件,指定了应用的代码,定义了应用基本信息(图标、权限、最低适用Android版本等),索引了使用的所有组件。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android_01_first_proj">             <-- 使用的代码 package

    <application
        android:allowBackup="true"                           <-- 应用基本信息
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Android01firstproj">
        <activity                                            <-- 使用的 activity 组件
            android:name=".MainActivity"                        <-- 对应的代码是 「package」.MainActivity
            android:exported="true"
            android:label="@string/app_name"
            android:theme="@style/Theme.Android01firstproj.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />    <-- Activity 是 app 启动的首页,即  app 运行入口 
            </intent-filter>
        </activity>
    </application>

</manifest>

AndroidManifest.xml 文件的名称不可更改,编译工具通过 AndroidManifest.xml 收录需要编译打包的代码和文件。

详细说明见 App Manifest Overview

组件代码

Andorid 应用的全部功能由构成 app 的一堆组件提供,组件类型一共有四种,分别是:Activity、Service、Receiver、Provider。

下面是四种类型的组件在 AndroidManifest.xml 的声明标签,App Manifest Overview 的列出了所有可配置项:

<activity> 
<service>
<receiver>
<provider>

组件代码位于 app/src/main/java 目录中。

资源文件

Android 应用的代码和资源文件是分开存放,app/src/main/res 中存放的是资源文件,资源文件保存布局/layout、菜单/menu、数值/value、图片等等。

AndroidManifest.xml 和组件代码通过资源的唯一 ID 引用资源,布局/layout 资源通过资源文件中的 tools:context 和组件关联。

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">       <-- 应用于.MainActivity 的布局/layout
    ....
</androidx.coordinatorlayout.widget.CoordinatorLayout>

Apk 文件解读方法

Apk 是 Android 应用使用的文件格式,它实际上是一个 zip 压缩包,可以直接用 tar 命令解压:

tar -xvf app-debug.apk
✗ tree -L 1
.
├── AndroidManifest.xml
├── DebugProbesKt.bin
├── META-INF
├── classes.dex
├── classes2.dex
├── classes3.dex
├── classes4.dex
├── kotlin
├── res
└── resources.arsc

3 directories, 7 files

Apk 的 AndroidManifest.xml

tar 解压缩得到的 AndroidManifest.xml 是一个二进制文件,不是直接可读的文本文件。这个二进制文件是 Android 自行实现的,不是 xml 通用的二进制的格式,Binary XML,没用标准的解读工具。

Android application package uses an undocumented binary XML format.[5]

用 axmldex 解读

axmldec: Android Binary XML Decoder 解读 AndroidManifest.xml:

brew tap ytsutano/toolbox
brew install axmldec

将二进制格式的 AndroidManifest.xml 转换成文本格式:

axmldec -o output.xml AndroidManifest.xml

用 apktool 解读

apktool 是 apk 修改器,能够将 apk 文件反向解析,然后将其重新打包成 apk 文件,为修改闭源的第三方 apk 提供了方便。

brew install apktool

apktool 会将整个 apk 文件反向解析:

apktool d app-debug.apk

参考

  1. 李佶澳的博客
  2. Android 开发者指南
  3. Android Studio 开发Android应用
  4. App Manifest Overview
  5. Binary XML
  6. axmldec: Android Binary XML Decoder
  7. 在AVD_7.1_x86 emulator上安装微信
  8. apktool

推荐阅读

Copyright @2011-2019 All rights reserved. 转载请添加原文连接,合作请加微信lijiaocn或者发送邮件: [email protected],备注网站合作

友情链接:  李佶澳的博客  小鸟笔记  软件手册  编程手册  运营手册  网络课程  收藏文章  发现知识星球  百度搜索 谷歌搜索