Skip to content

Commit

Permalink
Develop (#23)
Browse files Browse the repository at this point in the history
* Change append mode

* Add New Exception and messages

* Add test resource

* Handle changed bundle exception

<mahboobe.haddadi@cafebazaar.ir>
  • Loading branch information
mahboobehad committed Oct 26, 2021
1 parent c838b9b commit 347d77f
Show file tree
Hide file tree
Showing 7 changed files with 224 additions and 192 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Bundle signer is a command-line tool. You need to install **Java 8 or above** to
shows how to generate signed binary file of your bundle application assuming you are using Java keystore to sign APK
files of your application:
```sh
java -jar bundlesigner-0.1.7.jar genbin -v --bundle app.aab --bin . --v2-signing-enabled true --v3-signing-enabled false --ks key.jks
java -jar bundlesigner-0.1.8.jar genbin -v --bundle app.aab --bin . --v2-signing-enabled true --v3-signing-enabled false --ks key.jks
```
This generates signed digest of the provided bundle. The output of this command is a binary file that contains signed
digest of all APK files that can be extracted from the bundle. Signing is performed using one or more signers, each
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>ir.cafebazaar</groupId>
<artifactId>bundlesigner</artifactId>
<version>0.1.7</version>
<version>0.1.8</version>

<properties>
<maven.compiler.target>1.8</maven.compiler.target>
Expand Down
17 changes: 11 additions & 6 deletions src/main/java/ir/cafebazaar/bundlesigner/BundleSignerTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import ir.cafebazaar.apksig.apk.MinSdkVersionException;
import ir.cafebazaar.bundlesigner.command.GenBinCommand;
import ir.cafebazaar.bundlesigner.command.SignBundleCommand;
import ir.cafebazaar.bundlesigner.command.SignBundleException;
import org.apache.log4j.Logger;
import org.conscrypt.OpenSSLProvider;
import shadow.bundletool.com.android.utils.FileUtils;
Expand Down Expand Up @@ -141,30 +142,34 @@ public static void main(String[] params) throws Exception {
"Unsupported command: " + cmd + ". See --help for supported commands");
}
} catch (ParameterException | OptionsParser.OptionsException e) {
exitMessage = Arrays.toString(e.getStackTrace());
exitMessage = e.getMessage() + "\n" + Arrays.toString(e.getStackTrace());
exitCode = 2;
} catch (MinSdkVersionException e) {
exitMessage = "Failed to determine APK's minimum supported platform version"
+ ". Use --min-sdk-version to override";
exitCode = 3;
} catch (InvalidBundleException e) {
exitMessage = Arrays.toString(e.getStackTrace());
exitMessage = e.getMessage() + "\n" + Arrays.toString(e.getStackTrace());
exitCode = 5;
} catch (BundleToolIOException e) {
exitMessage = Arrays.toString(e.getStackTrace());
exitMessage = e.getMessage() + "\n" + Arrays.toString(e.getStackTrace());
exitCode = 6;

} catch (ApkFormatException e) {
exitMessage = Arrays.toString(e.getStackTrace());
exitMessage = e.getMessage() + "\n" + Arrays.toString(e.getStackTrace());
exitCode = 7;

}catch (SignBundleException e){
exitMessage = e.getMessage() + "\n" + Arrays.toString(e.getStackTrace());
exitCode = 8;

} catch (RuntimeException e) {
exitMessage = Arrays.toString(e.getStackTrace());
exitMessage = e.getMessage() + "\n" + Arrays.toString(e.getStackTrace());
exitCode = 4;

} finally {
if (!exitMessage.isEmpty())
System.err.println(exitMessage);
logger.info(exitMessage);
System.exit(exitCode);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ private void appendSignToFile(File file, String apkName, String sign) throws IOE
}

private void generateFinalBinFile(File binV1, File binV2V3) throws IOException {
PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(bin, true), StandardCharsets.UTF_8));
PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(bin, false), StandardCharsets.UTF_8));

String implementationVersion = getClass().getPackage().getImplementationVersion();
writer.println(String.format("version: %s", implementationVersion));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,21 @@ private void extractAndSignApks(String apksPath) throws Exception {

File apk = new File(TMP_DIR_PATH + File.separator + apkSetEntry.getFileName());
List<ApkSigner.SignerConfig> signerConfigs = new ArrayList<>(0);

String apkDigest;
try{
apkDigest = apkSignV1.get(apkName);
}
catch (Exception e){
String msg = String.format("Digest of %s not found in bin file.", apkName);
throw new SignBundleException(msg);
}

ApkSigner.Builder apkSignerBuilder =
new ApkSigner.Builder(signerConfigs, true)
.setInputApk(apk)
.setOutputApk(v1SignedApk)
.setSignDigest(apk.getPath(), apkSignV1.get(apkName));
.setSignDigest(apk.getPath(), apkDigest);
apkSignV1.remove(apkName);

ApkSigner apkSigner = apkSignerBuilder.build();
Expand All @@ -164,11 +174,19 @@ private void extractAndSignApks(String apksPath) throws Exception {
V2V3SignedApk = new File(outputPath + File.separator + apkName);
}

String apkDigestV2V3;
try{
apkDigestV2V3 = apkSignV2V3.get(apkName);
} catch (Exception e){
String msg = String.format("Digest of %s not found in bin file.", apkName);
throw new SignBundleException(msg);
}

apkSignerBuilder =
new ApkSigner.Builder(new ArrayList<>(0), true)
.setInputApk(v1SignedApk)
.setOutputApk(V2V3SignedApk)
.setSignDigest(v1SignedApk.getPath(), apkSignV2V3.get(apkName))
.setSignDigest(v1SignedApk.getPath(), apkDigestV2V3)
.setOtherSignersSignaturesPreserved(false)
.setV1SigningEnabled(false)
.setV2SigningEnabled(signV2Enabled)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ir.cafebazaar.bundlesigner.command;

public class SignBundleException extends Exception{
private static final long serialVersionUID = 1L;

SignBundleException(String message){
super(message);
}
}
Loading

0 comments on commit 347d77f

Please sign in to comment.