Skip to content

Commit 9f8b663

Browse files
committed
Relse: ApkPacker v1.0.0 release.
1 parent 1d69d00 commit 9f8b663

File tree

10 files changed

+377
-0
lines changed

10 files changed

+377
-0
lines changed

LICENSE

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
Apache License
2+
Version 2.0, January 2004
3+
http://www.apache.org/licenses/
4+
5+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6+
7+
1. Definitions.
8+
9+
"License" shall mean the terms and conditions for use, reproduction,
10+
and distribution as defined by Sections 1 through 9 of this document.
11+
12+
"Licensor" shall mean the copyright owner or entity authorized by
13+
the copyright owner that is granting the License.
14+
15+
"Legal Entity" shall mean the union of the acting entity and all
16+
other entities that control, are controlled by, or are under common
17+
control with that entity. For the purposes of this definition,
18+
"control" means (i) the power, direct or indirect, to cause the
19+
direction or management of such entity, whether by contract or
20+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
21+
outstanding shares, or (iii) beneficial ownership of such entity.
22+
23+
"You" (or "Your") shall mean an individual or Legal Entity
24+
exercising permissions granted by this License.
25+
26+
"Source" form shall mean the preferred form for making modifications,
27+
including but not limited to software source code, documentation
28+
source, and configuration files.
29+
30+
"Object" form shall mean any form resulting from mechanical
31+
transformation or translation of a Source form, including but
32+
not limited to compiled object code, generated documentation,
33+
and conversions to other media types.
34+
35+
"Work" shall mean the work of authorship, whether in Source or
36+
Object form, made available under the License, as indicated by a
37+
copyright notice that is included in or attached to the work
38+
(an example is provided in the Appendix below).
39+
40+
"Derivative Works" shall mean any work, whether in Source or Object
41+
form, that is based on (or derived from) the Work and for which the
42+
editorial revisions, annotations, elaborations, or other modifications
43+
represent, as a whole, an original work of authorship. For the purposes
44+
of this License, Derivative Works shall not include works that remain
45+
separable from, or merely link (or bind by name) to the interfaces of,
46+
the Work and Derivative Works thereof.
47+
48+
"Contribution" shall mean any work of authorship, including
49+
the original version of the Work and any modifications or additions
50+
to that Work or Derivative Works thereof, that is intentionally
51+
submitted to Licensor for inclusion in the Work by the copyright owner
52+
or by an individual or Legal Entity authorized to submit on behalf of
53+
the copyright owner. For the purposes of this definition, "submitted"
54+
means any form of electronic, verbal, or written communication sent
55+
to the Licensor or its representatives, including but not limited to
56+
communication on electronic mailing lists, source code control systems,
57+
and issue tracking systems that are managed by, or on behalf of, the
58+
Licensor for the purpose of discussing and improving the Work, but
59+
excluding communication that is conspicuously marked or otherwise
60+
designated in writing by the copyright owner as "Not a Contribution."
61+
62+
"Contributor" shall mean Licensor and any individual or Legal Entity
63+
on behalf of whom a Contribution has been received by Licensor and
64+
subsequently incorporated within the Work.
65+
66+
2. Grant of Copyright License. Subject to the terms and conditions of
67+
this License, each Contributor hereby grants to You a perpetual,
68+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69+
copyright license to reproduce, prepare Derivative Works of,
70+
publicly display, publicly perform, sublicense, and distribute the
71+
Work and such Derivative Works in Source or Object form.
72+
73+
3. Grant of Patent License. Subject to the terms and conditions of
74+
this License, each Contributor hereby grants to You a perpetual,
75+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76+
(except as stated in this section) patent license to make, have made,
77+
use, offer to sell, sell, import, and otherwise transfer the Work,
78+
where such license applies only to those patent claims licensable
79+
by such Contributor that are necessarily infringed by their
80+
Contribution(s) alone or by combination of their Contribution(s)
81+
with the Work to which such Contribution(s) was submitted. If You
82+
institute patent litigation against any entity (including a
83+
cross-claim or counterclaim in a lawsuit) alleging that the Work
84+
or a Contribution incorporated within the Work constitutes direct
85+
or contributory patent infringement, then any patent licenses
86+
granted to You under this License for that Work shall terminate
87+
as of the date such litigation is filed.
88+
89+
4. Redistribution. You may reproduce and distribute copies of the
90+
Work or Derivative Works thereof in any medium, with or without
91+
modifications, and in Source or Object form, provided that You
92+
meet the following conditions:
93+
94+
(a) You must give any other recipients of the Work or
95+
Derivative Works a copy of this License; and
96+
97+
(b) You must cause any modified files to carry prominent notices
98+
stating that You changed the files; and
99+
100+
(c) You must retain, in the Source form of any Derivative Works
101+
that You distribute, all copyright, patent, trademark, and
102+
attribution notices from the Source form of the Work,
103+
excluding those notices that do not pertain to any part of
104+
the Derivative Works; and
105+
106+
(d) If the Work includes a "NOTICE" text file as part of its
107+
distribution, then any Derivative Works that You distribute must
108+
include a readable copy of the attribution notices contained
109+
within such NOTICE file, excluding those notices that do not
110+
pertain to any part of the Derivative Works, in at least one
111+
of the following places: within a NOTICE text file distributed
112+
as part of the Derivative Works; within the Source form or
113+
documentation, if provided along with the Derivative Works; or,
114+
within a display generated by the Derivative Works, if and
115+
wherever such third-party notices normally appear. The contents
116+
of the NOTICE file are for informational purposes only and
117+
do not modify the License. You may add Your own attribution
118+
notices within Derivative Works that You distribute, alongside
119+
or as an addendum to the NOTICE text from the Work, provided
120+
that such additional attribution notices cannot be construed
121+
as modifying the License.
122+
123+
You may add Your own copyright statement to Your modifications and
124+
may provide additional or different license terms and conditions
125+
for use, reproduction, or distribution of Your modifications, or
126+
for any such Derivative Works as a whole, provided Your use,
127+
reproduction, and distribution of the Work otherwise complies with
128+
the conditions stated in this License.
129+
130+
5. Submission of Contributions. Unless You explicitly state otherwise,
131+
any Contribution intentionally submitted for inclusion in the Work
132+
by You to the Licensor shall be under the terms and conditions of
133+
this License, without any additional terms or conditions.
134+
Notwithstanding the above, nothing herein shall supersede or modify
135+
the terms of any separate license agreement you may have executed
136+
with Licensor regarding such Contributions.
137+
138+
6. Trademarks. This License does not grant permission to use the trade
139+
names, trademarks, service marks, or product names of the Licensor,
140+
except as required for reasonable and customary use in describing the
141+
origin of the Work and reproducing the content of the NOTICE file.
142+
143+
7. Disclaimer of Warranty. Unless required by applicable law or
144+
agreed to in writing, Licensor provides the Work (and each
145+
Contributor provides its Contributions) on an "AS IS" BASIS,
146+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147+
implied, including, without limitation, any warranties or conditions
148+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149+
PARTICULAR PURPOSE. You are solely responsible for determining the
150+
appropriateness of using or redistributing the Work and assume any
151+
risks associated with Your exercise of permissions under this License.
152+
153+
8. Limitation of Liability. In no event and under no legal theory,
154+
whether in tort (including negligence), contract, or otherwise,
155+
unless required by applicable law (such as deliberate and grossly
156+
negligent acts) or agreed to in writing, shall any Contributor be
157+
liable to You for damages, including any direct, indirect, special,
158+
incidental, or consequential damages of any character arising as a
159+
result of this License or out of the use or inability to use the
160+
Work (including but not limited to damages for loss of goodwill,
161+
work stoppage, computer failure or malfunction, or any and all
162+
other commercial damages or losses), even if such Contributor
163+
has been advised of the possibility of such damages.
164+
165+
9. Accepting Warranty or Additional Liability. While redistributing
166+
the Work or Derivative Works thereof, You may choose to offer,
167+
and charge a fee for, acceptance of support, warranty, indemnity,
168+
or other liability obligations and/or rights consistent with this
169+
License. However, in accepting such obligations, You may act only
170+
on Your own behalf and on Your sole responsibility, not on behalf
171+
of any other Contributor, and only if You agree to indemnify,
172+
defend, and hold each Contributor harmless for any liability
173+
incurred by, or claims asserted against, such Contributor by reason
174+
of your accepting any such warranty or additional liability.
175+
176+
END OF TERMS AND CONDITIONS
177+
178+
APPENDIX: How to apply the Apache License to your work.
179+
180+
To apply the Apache License to your work, attach the following
181+
boilerplate notice, with the fields enclosed by brackets "{}"
182+
replaced with your own identifying information. (Don't include
183+
the brackets!) The text should be enclosed in the appropriate
184+
comment syntax for the file format. We also recommend that a
185+
file or class name and description of purpose be included on the
186+
same "printed page" as the copyright notice for easier
187+
identification within third-party archives.
188+
189+
Copyright 2019 l0neman
190+
191+
Licensed under the Apache License, Version 2.0 (the "License");
192+
you may not use this file except in compliance with the License.
193+
You may obtain a copy of the License at
194+
195+
http://www.apache.org/licenses/LICENSE-2.0
196+
197+
Unless required by applicable law or agreed to in writing, software
198+
distributed under the License is distributed on an "AS IS" BASIS,
199+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200+
See the License for the specific language governing permissions and
201+
limitations under the License.
149 Bytes
Binary file not shown.

PackerTool/src/apk_packer.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ def callback(name, channel):
4444

4545
# todo 支持命令行选项执行?
4646

47+
_version = '1.0.0'
48+
4749
if __name__ == '__main__':
4850
print_time()
4951
debug_log.print_step_t____('开始多渠道打包')
164 Bytes
Binary file not shown.
Binary file not shown.
155 Bytes
Binary file not shown.
1.98 KB
Binary file not shown.
1.83 KB
Binary file not shown.

README.md

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
# ApkPacker
2+
3+
- [介绍](#介绍)
4+
- [使用](#使用)
5+
- [仓库说明](#仓库说明)
6+
- [1. 渠道读取](#1.-渠道读取)
7+
- [2. 渠道打包](#2.-渠道打包)
8+
- [3. 测试](#3.-测试)
9+
- [原理](#原理)
10+
- [原理简述](#原理简述)
11+
- [实现步骤简述](#实现步骤简述)
12+
- [参考](#参考)
13+
- [LICENSE](#license)
14+
15+
## 介绍
16+
17+
ApkPacker 是一个 Android 多渠道打包工具,打包脚本使用 Python 3.x 开发。
18+
19+
使用 Python 脚本,不依赖于 Android Studio 集成环境,易于使用。
20+
21+
ApkPacker 采用流行的的将签名写入 APK Signing Block(Apk 签名块)中的方法,避免了重新签名,所以生成渠道包效率极高,打包速度主要取决于 Apk 文件的复制速度。
22+
23+
## 使用
24+
25+
ApkPacker 的使用分为两部分,首先是使用 Python 脚本将需要用于统计的渠道写入对应的多个 Apk 副本中,得到多渠道 Apk 包,其次是 Apk 文件安装至 Android 设备后的运行时读取工作,运行时读取到 Apk 文件的对应渠道即达到多渠道打包目标。
26+
27+
注意:Apk 必须具有 v2 或 v3 签名,因为打包依赖于 Apk 签名分块,如果 Apk 只有 v1 签名,那么它将不具有 Apk 签名分块,无法进行打包。
28+
29+
### 仓库说明
30+
31+
首先将项目 clone 到本地,项目分为两块。
32+
33+
1. ChannelReader - Java 代码,用于 Android 客户端集成后读取渠道。
34+
2. PackerTool - Python 脚本,用于进行多渠道打包。
35+
36+
### 1. 渠道读取
37+
38+
首先需要集成渠道读取的代码,它将从 Apk 的签名分块中读取渠道。
39+
40+
`io.l0neman.channelreader` 包或包下的三个类复制到项目中合适位置。
41+
42+
使用如下代码即可获取渠道:
43+
44+
```
45+
String channel = ChannelReader.getInstance().read(context);
46+
```
47+
48+
如果应用还未进行打包(渠道还未写入 Apk),则返回 null。
49+
50+
### 2. 渠道打包
51+
52+
编译一个 Release 版本应用包,放入 `PackerTool/apk` 目录(没有则新建)下。
53+
54+
然后编辑渠道文件 `PackerTool/channel/channel.txt`,每行表示一个渠道,每个渠道包含一个渠道名字和渠道字符串,使用 `|` 分隔。例如:`应用宝|c1`,渠道名字用于打包时展示和命名输出文件,不会出现在 Apk 中,可为任意字符串;渠道字符串为需要读取的渠道,将会写入 Apk 文件中。
55+
56+
使用时将每行渠道按照上述规则修改为自己需要的渠道即可。
57+
58+
做完上述工作,使用 Python 执行 `PackerTool/src/apk_packer.py` 等待打印结束后即可完成多渠道打包。
59+
60+
例如:
61+
62+
```
63+
cd src
64+
python apk_apcker.py
65+
```
66+
67+
此时在 `PackerTool/out/` 目录下将出现与渠道数量相同的渠道包,命名为 `[apk 名称无后缀]_[渠道名]_[渠道号].apk`,使用它们即可进行测试和上线工作。
68+
69+
例如将 `Tool.apk` 输出渠道 `应用宝|c1` 的渠道包为 `Tool_应用宝_c1.apk`
70+
71+
### 3. 测试
72+
73+
`ChannelReader` 项目的 `app` Moduel 为渠道包 Demo,它会将渠道显示在 TextView 上,可以使用它进行渠道打包测试。
74+
75+
## 原理
76+
77+
### 原理简述
78+
79+
Apk 在进行 v2 或 v3 签名后,签名工具将会在 Apk 文件的中央位置插入一个 Apk Signing Block(APK 签名分块),签名工具对整个 Apk 文件计算完整性校验数据后,将关键信息(包括 Apk 文件完整性校验数据等)写入此分块,当 Apk 文件安装至设备时,Android 系统将利用签名信息对 Apk 文件进行校验。
80+
81+
![apk-before-after-signing.png](./arts/apk-before-after-signing.png)
82+
83+
[上图引用自 Android Developer 官网]
84+
85+
Apk 的基础文件格式为 ZIP,一个标准的 ZIP 文件结构分为 3 部分,`Contents of ZIP entries` 为 Zip 条目内容区域,记录了 ZIP 中每个文件的信息,`Central Directory` 为 ZIP 中央目标,记录了
86+
每个文件的路径属性等信息,`End of Central Directory` 为 ZIP 中央目录结尾,记录了 ZIP 文件的结构信息,可用于找到 `Central Directory` 块,从而解析出整个 ZIP 文件。
87+
88+
Apk Siging Block(Apk 签名分块)将被写入 `Central Directory` 的前面,我们可以根据 `End of Central Directory` 间接的寻找到它。
89+
90+
签名工具在计算 Apk 文件的完整性信息时,只包括 Apk 文件的 1、3、4 数据块,而不包括 Apk Siging Block(Apk 签名分块)本身,那么我们就可以在其中写入自定义的信息(例如渠道信息),可达到避免重新签名的效果。
91+
92+
那么打包工具要做的工具即:将渠道写入 Apk 签名分块中,在 Apk 安装后的运行时读取渠道信息。
93+
94+
写入渠道首先需要了解 Apk 签名分块的结构,找到写入数据的方法。
95+
96+
经过查阅资料,发现 Apk 签名分块结构如下:
97+
98+
- `size of block in bytes` (excluding this field) (uint64)
99+
- Sequence of uint64-length-prefixed ID-value pairs:
100+
- `ID` (uint32)
101+
- `value` (variable-length: length of the pair - 4 bytes)
102+
- `size of block` in bytes—same as the very first field (uint64)
103+
- `magic` "APK Sig Block 42" (16 bytes)
104+
105+
翻译为中文:
106+
107+
108+
- `块大小` (不包括此字段) (uint64)
109+
- 带有 uint64 长度前缀的 ID-value 键值对序列
110+
- `ID` (uint32)
111+
- `value` (长度:键值对 - 4 bytes)
112+
- `块大小` 值和上面第一个字段相同。(uint64)
113+
- `魔数` "Apk Sig Block 42" (16 bytes)
114+
115+
块大小和魔数都是签名分块的特征,无法携带额外信息。
116+
117+
其中键值对序列用于存放应用 v2 或 v3 签名信息,每一个键值对都表示一个 Apk 签名,其中 `ID` 表示签名的唯一标识,`value` 则为签名的详细信息,一般我们在对 Apk 进行 v2 签名后,`ID-value` 键值对序列中将出现一个 v2 签名的键值对,它的 `ID` 固定值为 `0x7109871a`,如果应用同时进行了 v3 签名(`ID``0xf05368c0`),那么此时键值对序列的大小将为 2。
118+
119+
键值对序列的设计是为了满足多签名,它不仅可以支持官方的 v2 和 v3 签名,也可以支持第三方的自定义签名,即任何人都可以对 Apk 进行签名(用于自定义的校验),例如某手机厂商自己设计了一套签名机制,必须通过它签名后的 Apk 才能按照到此厂商的系统上,Android 官方系统将忽略第三方的签名信息。
120+
121+
那么这里得出结论,可以向它写入自定义的签名数据,或者其他数据。最终打包工具使用了键值对进行渠道的写入,应用安装后在运行时读取 Apk 文件中的渠道数据即可。
122+
123+
### 实现步骤简述
124+
125+
1. 渠道打包
126+
127+
```
128+
复制 Apk 文件 -> 根据文件结构寻找 Apk 签名分块位置 -> 解析 Apk 签名分块 -> 写入渠道
129+
```
130+
131+
2. 渠道读取
132+
133+
```
134+
获取 Apk 路径 -> 读取 Apk 文件 -> 根据文件结构寻找 Apk 签名分块位置 -> 读取渠道字符串
135+
```
136+
137+
## 参考
138+
139+
- Apk 文件结构
140+
141+
[zip 文件格式](https://blog.csdn.net/xiaobing1994/article/details/78367035)
142+
143+
[The structure of a PKZip file](https://users.cs.jmu.edu/buchhofp/forensics/formats/pkzip.html)
144+
145+
- Apk 签名分块结构
146+
147+
[APK 签名方案 v2](https://source.android.google.cn/security/apksigning/v2#apk-signature-scheme-v2-block)
148+
149+
- 原理博客
150+
151+
[Walle - 美团技术团队](https://tech.meituan.com/2017/01/13/android-apk-v2-signature-scheme.html)
152+
153+
- 源代码
154+
155+
[Meituan-Dianping/walle](https://github.com/Meituan-Dianping/walle)
156+
157+
## LICENSE
158+
159+
```
160+
Copyright 2017 Wangruning
161+
162+
Licensed under the Apache License, Version 2.0 (the "License");
163+
you may not use this file except in compliance with the License.
164+
You may obtain a copy of the License at
165+
166+
http://www.apache.org/licenses/LICENSE-2.0
167+
168+
Unless required by applicable law or agreed to in writing, software
169+
distributed under the License is distributed on an "AS IS" BASIS,
170+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
171+
See the License for the specific language governing permissions and
172+
limitations under the License.
173+
```
174+

arts/apk-before-after-signing.png

13.2 KB
Loading

0 commit comments

Comments
 (0)