Skip to content

Commit 28b389a

Browse files
committed
feat: check signature version when buildType is add channel file to META-INF
1 parent 92cf656 commit 28b389a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+8026
-2176
lines changed

ApkMultiChannelPlugin.jar

99.3 KB
Binary file not shown.

README.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
**Key Alias:** 密钥别名 <br/>
4040
**Key Password:** 密钥密码 <br/>
4141

42-
**Zipalign Path:** zipalign文件的路径(用于优化 apk;zipalign 可以确保所有未压缩的数据均是以相对于文件开始部分的特定字节对齐开始,这样可减少应用消耗的 RAM 量。)<br/>
42+
**Zipalign Path:** zipalign 文件的路径(用于优化 apk;zipalign 可以确保所有未压缩的数据均是以相对于文件开始部分的特定字节对齐开始,这样可减少应用消耗的 RAM 量。)<br/>
4343
**Signer Version:** 选择签名版本:apksigner 和 jarsigner <br/>
4444
**Build Type:** 打包方式 <br/>
4545

@@ -70,7 +70,8 @@
7070
同时替换 AndroidManifest.xml,最后重新签名。
7171

7272
#### add channel file to META-INF
73-
复制1个 apk,然后直接添加空文件到其 META-INF 目录(不重新签名)。读取渠道:[ChannelHelper](https://gist.github.com/nukc/f777b54232be56f04171bcef56a627e1)
73+
复制1个 apk,先检查签名版本,如果未签名则进行签名(配置选择 jarsigner 则在渠道打包前签名,apksigner 则是添加空文件到其 META-INF 目录后再签名)。
74+
读取渠道:[ChannelHelper](https://gist.github.com/nukc/f777b54232be56f04171bcef56a627e1)
7475

7576
#### write zip comment
7677
先判断选中的 apk 中 comment 是否含有 SIGN 字节,如果有则不进行渠道打包并提示;之后检查是否是 v2 签名,如果是 v2,则复制1个不带签名文件的 apk 到 temp 文件夹并重新签名为 v1,
@@ -79,10 +80,7 @@
7980

8081
## 以后要加的功能
8182

82-
- 如果 buildType 选择美团方案在 META-INF 目录写入空文件:
83-
- 自定义空文件名的前辍(目前是 ```c_```
84-
- 在打包之前先判断选中的apk是否已经签名,如果没有则先签名
85-
- 添加支持选择项目路径外的apk文件进行多渠道打包
83+
- 添加支持选择项目路径外的 apk 文件进行多渠道打包
8684
- buildType 添加支持美团新一代渠道包生成方式 Walle
8785

8886
有什么问题欢迎大家在 [Issues](https://github.com/nukc/ApkMultiChannelPlugin/issues) 中提问
@@ -95,6 +93,7 @@
9593
- [apksigner](https://developer.android.com/studio/command-line/apksigner.html)
9694
- [packer-ng-plugin](https://github.com/mcxiaoke/packer-ng-plugin)
9795
- [新一代开源Android渠道包生成工具Walle](http://tech.meituan.com/android-apk-v2-signature-scheme.html)
96+
- [apksig](https://android.googlesource.com/platform/tools/apksig/)
9897

9998
同时感谢 [dim](https://github.com/zzz40500)[区长](https://github.com/lizhangqu) 的指点迷津。
10099

apksig/src/com/android/apksig/ApkSigner.java

Lines changed: 1090 additions & 0 deletions
Large diffs are not rendered by default.

signer/src/com/android/apksigner/core/ApkSignerEngine.java renamed to apksig/src/com/android/apksig/ApkSignerEngine.java

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,28 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.android.apksigner.core;
17+
package com.android.apksig;
1818

19+
import com.android.apksig.apk.ApkFormatException;
20+
import com.android.apksig.util.DataSink;
21+
import com.android.apksig.util.DataSource;
1922
import java.io.Closeable;
2023
import java.io.IOException;
2124
import java.security.InvalidKeyException;
25+
import java.security.NoSuchAlgorithmException;
2226
import java.security.SignatureException;
2327
import java.util.List;
2428

25-
import com.android.apksigner.core.util.DataSink;
26-
import com.android.apksigner.core.util.DataSource;
27-
2829
/**
2930
* APK signing logic which is independent of how input and output APKs are stored, parsed, and
3031
* generated.
3132
*
3233
* <p><h3>Operating Model</h3>
3334
*
3435
* The abstract operating model is that there is an input APK which is being signed, thus producing
35-
* an output APK. In reality, there may be just an output APK being built from scratch, or the input APK and
36-
* the output APK may be the same file. Because this engine does not deal with reading and writing
37-
* files, it can handle all of these scenarios.
36+
* an output APK. In reality, there may be just an output APK being built from scratch, or the input
37+
* APK and the output APK may be the same file. Because this engine does not deal with reading and
38+
* writing files, it can handle all of these scenarios.
3839
*
3940
* <p>The engine is stateful and thus cannot be used for signing multiple APKs. However, once
4041
* the engine signed an APK, the engine can be used to re-sign the APK after it has been modified.
@@ -88,7 +89,7 @@
8889
* documentation of each method about the deadlines for performing the tasks requested by the
8990
* method.
9091
*
91-
* <p><h3 id="incremental">Incremental Operation</h3>
92+
* <p><h3 id="incremental">Incremental Operation</h3></a>
9293
*
9394
* The engine supports incremental operation where a signed APK is produced, then modified and
9495
* re-signed. This may be useful for IDEs, where an app is frequently re-signed after small changes
@@ -108,6 +109,8 @@
108109
* not invoked. In this mode, the engine has less control over output because it cannot request that
109110
* some JAR entries are not output. Nevertheless, the engine will attempt to make the output APK
110111
* signed and will report an error if cannot do so.
112+
*
113+
* @see <a href="https://source.android.com/security/apksigning/index.html">Application Signing</a>
111114
*/
112115
public interface ApkSignerEngine extends Closeable {
113116

@@ -118,9 +121,12 @@ public interface ApkSignerEngine extends Closeable {
118121
* @param apkSigningBlock APK signing block of the input APK. The provided data source is
119122
* guaranteed to not be used by the engine after this method terminates.
120123
*
124+
* @throws IOException if an I/O error occurs while reading the APK Signing Block
125+
* @throws ApkFormatException if the APK Signing Block is malformed
121126
* @throws IllegalStateException if this engine is closed
122127
*/
123-
void inputApkSigningBlock(DataSource apkSigningBlock) throws IllegalStateException;
128+
void inputApkSigningBlock(DataSource apkSigningBlock)
129+
throws IOException, ApkFormatException, IllegalStateException;
124130

125131
/**
126132
* Indicates to this engine that the specified JAR entry was encountered in the input APK.
@@ -176,19 +182,25 @@ InputJarEntryInstructions.OutputPolicy inputJarEntryRemoved(String entryName)
176182
/**
177183
* Indicates to this engine that all JAR entries have been output.
178184
*
179-
*
180185
* @return request to add JAR signature to the output or {@code null} if there is no need to add
181186
* a JAR signature. The request will contain additional JAR entries to be output. The
182187
* request must be fulfilled before
183188
* {@link #outputZipSections(DataSource, DataSource, DataSource)} is invoked.
184189
*
190+
* @throws ApkFormatException if the APK is malformed in a way which is preventing this engine
191+
* from producing a valid signature. For example, if the engine uses the provided
192+
* {@code META-INF/MANIFEST.MF} as a template and the file is malformed.
193+
* @throws NoSuchAlgorithmException if a signature could not be generated because a required
194+
* cryptographic algorithm implementation is missing
185195
* @throws InvalidKeyException if a signature could not be generated because a signing key is
186196
* not suitable for generating the signature
187-
* @throws SignatureException if an error occurred while generating the JAR signature
197+
* @throws SignatureException if an error occurred while generating a signature
188198
* @throws IllegalStateException if there are unfulfilled requests, such as to inspect some JAR
189199
* entries, or if the engine is closed
190200
*/
191-
OutputJarSignatureRequest outputJarEntries() throws InvalidKeyException, SignatureException;
201+
OutputJarSignatureRequest outputJarEntries()
202+
throws ApkFormatException, NoSuchAlgorithmException, InvalidKeyException,
203+
SignatureException, IllegalStateException;
192204

193205
/**
194206
* Indicates to this engine that the ZIP sections comprising the output APK have been output.
@@ -207,16 +219,23 @@ InputJarEntryInstructions.OutputPolicy inputJarEntryRemoved(String entryName)
207219
* {@link #outputDone()} is invoked.
208220
*
209221
* @throws IOException if an I/O error occurs while reading the provided ZIP sections
222+
* @throws ApkFormatException if the provided APK is malformed in a way which prevents this
223+
* engine from producing a valid signature. For example, if the APK Signing Block
224+
* provided to the engine is malformed.
225+
* @throws NoSuchAlgorithmException if a signature could not be generated because a required
226+
* cryptographic algorithm implementation is missing
210227
* @throws InvalidKeyException if a signature could not be generated because a signing key is
211228
* not suitable for generating the signature
212-
* @throws SignatureException if an error occurred while generating the APK's signature
229+
* @throws SignatureException if an error occurred while generating a signature
213230
* @throws IllegalStateException if there are unfulfilled requests, such as to inspect some JAR
214231
* entries or to output JAR signature, or if the engine is closed
215232
*/
216233
OutputApkSigningBlockRequest outputZipSections(
217234
DataSource zipEntries,
218235
DataSource zipCentralDirectory,
219-
DataSource zipEocd) throws IOException, InvalidKeyException, SignatureException;
236+
DataSource zipEocd)
237+
throws IOException, ApkFormatException, NoSuchAlgorithmException,
238+
InvalidKeyException, SignatureException, IllegalStateException;
220239

221240
/**
222241
* Indicates to this engine that the signed APK was output.

0 commit comments

Comments
 (0)