以Tethering.apk为例看APEX的搭建过程

由于系统模块无法像应用软件apk的方式进行更新,在更新系统模块时,往往需要重新烧录整个image才可以完成,所以Android引入的APEX文件格式。
详细概念可参考https://blog.csdn.net/qq_28351465/article/details/106458089
本文以Tethering.apk为例,介绍下apex如何创建、使用等。
Tethering.apk是系统预置apk,用来将一些interface的网络share给另外的interface使用、创建dhcp server等。

构建编译APEX

APEX 对应的Android.bp如下:

/frameworks/base/packages/Tethering/apex/Android.bp
17 apex {
18     name: "com.android.tethering",
19     updatable: true,
20     min_sdk_version: "current",
21     java_libs: ["framework-tethering"],
22     apps: ["Tethering"],
23     manifest: "manifest.json",
24     key: "com.android.tethering.key",
25 
26     androidManifest: "AndroidManifest.xml",
27 }
28 
29 apex_key {
30     name: "com.android.tethering.key",
31     public_key: "com.android.tethering.avbpubkey",
32     private_key: "com.android.tethering.pem",
33 }
34 
35 android_app_certificate {
36     name: "com.android.tethering.certificate",
37     certificate: "com.android.tethering",
38 }

L18 name 指apex name 决定平台上的挂载路径为 /apex/apex_name

console:/ # ls /apex/ | grep tether                                            
com.android.tethering

L21 java_libs指APEX包中包含java lib framework-tethering
L22 apps指APEX中包含Tethering apk
可以看到平台中/apex/com.android.tethering路径下的文件如下

console:/apex/com.android.tethering # ls -lh                                   
total 8.0K
-rw-r--r-- 1 root root   29 2020-07-13 05:13 apex_manifest.pb
-rw-r--r-- 1 root root 1.0K 2020-07-13 04:34 apex_pubkey
drwxr-xr-x 2 root root 4.0K 2020-07-13 05:37 javalib
drwxr-xr-x 3 root root 4.0K 2020-07-13 05:34 priv-app
console:/apex/com.android.tethering/priv-app/Tethering # ls -lh
total 250K
-rw-r--r-- 1 root root 500K 2020-07-13 05:34 Tethering.apk
1|console:/apex/com.android.tethering/javalib # ls -lh
total 28K
-rw-r--r-- 1 root root 53K 2020-07-13 05:37 framework-tethering.jar

其他的file类型可参考如下路径(相应文件放到子目录中)

File type Location in APEX
Shared libraries /lib and /lib64 (/lib/arm for translated arm in x86)
Executables /bin
Java libraries /javalib
Prebuilts /etc

L23 manifest.json包括package name和版本,用来标识该APEX文件

/frameworks/base/packages/Tethering/apex/manifest.json
1 {
2   "name": "com.android.tethering",
3   "version": 300000000
4 }

L24 该apex指定的apex_key的name 对应L29~L33
L25 为Androidmanifest

L31 L32 为指定公钥和私钥,具体生产方法参考https://blog.csdn.net/qq_28351465/article/details/106458089
用来编译阶段加密apex,以及平台运行是apexd通过平台中的公钥验证apex

L35 ~ L38与apk相同的签名

对应apex包中的apk和javalib中的Android.bp 如下:

/frameworks/base/packages/Tethering/Android.bp
122 // Updatable tethering packaged as an application
123 android_app {
124     name: "Tethering",
125     defaults: ["TetheringAppDefaults"],
126     static_libs: ["TetheringApiCurrentLib"],
127     certificate: "networkstack",
128     manifest: "AndroidManifest.xml",
129     use_embedded_native_libs: true,
130     // The permission configuration *must* be included to ensure security of the device
131     required: ["NetworkPermissionConfig"],
132     apex_available: ["com.android.tethering"],
133     min_sdk_version: "current",
134 }
/frameworks/base/packages/Tethering/common/TetheringLib/Android.bp
16 java_sdk_library {
17     name: "framework-tethering",
18     defaults: ["framework-module-defaults"],
19 
20     // Allow access to the stubs from anywhere.
21     visibility: ["//visibility:public"],
22 
23     // Restrict access to implementation library.
24     impl_library_visibility: [
25         "//visibility:override", // Ignore the visibility property.
26         "//frameworks/base/packages/Tethering:__subpackages__",
27     ],
28 
29     srcs: [
30         ":framework-tethering-srcs",
31     ],
32 
33     jarjar_rules: "jarjar-rules.txt",
34     installable: true,
35 
36     hostdex: true, // for hiddenapi check
37     apex_available: ["com.android.tethering"],
38     permitted_packages: ["android.net"],
39 }

其中 L132 和 L37都有特别注明apex_available: [“com.android.tethering”],标识该apk和javalib可以被name为com.android.tethering的apex包进去。
特别注意的是APK中 一旦有apex_available标签,那么将无法通过指定 PRODUCT_ENFORCE_RRO_TARGETS宏,使其自动生产rro_…apk
只能通过自己新建RRO apk的方式,进行overlay。

更新APEX

你可能感兴趣的:(Android,Q)