Commit 80cafba2 authored by Pavitra Golchha's avatar Pavitra Golchha Committed by Jeffrey Schiller

Replace JarSigner with ApkSigner (#1417)

Replace JarSigner with ApkSigner
parent 71b2e0aa
......@@ -133,6 +133,8 @@ public final class Compiler {
RUNTIME_FILES_DIR + "kawa.jar";
private static final String SIMPLE_ANDROID_RUNTIME_JAR =
RUNTIME_FILES_DIR + "AndroidRuntime.jar";
private static final String APKSIGNER_JAR =
RUNTIME_FILES_DIR + "apksigner.jar";
private static final String LINUX_AAPT_TOOL =
"/tools/linux/aapt";
......@@ -1134,18 +1136,18 @@ public final class Compiler {
reporter.report(95);
}
// Sign the apk file
out.println("________Signing the apk file");
if (!compiler.runJarSigner(apkAbsolutePath, keystoreFilePath)) {
return false;
}
// ZipAlign the apk file
out.println("________ZipAligning the apk file");
if (!compiler.runZipAlign(apkAbsolutePath, tmpDir)) {
return false;
}
// Sign the apk file
out.println("________Signing the apk file");
if (!compiler.runApkSigner(apkAbsolutePath, keystoreFilePath)) {
return false;
}
if (reporter != null) {
reporter.report(100);
}
......@@ -1439,47 +1441,6 @@ public final class Compiler {
return true;
}
private boolean runJarSigner(String apkAbsolutePath, String keystoreAbsolutePath) {
// TODO(user): maybe make a command line flag for the jarsigner location
String javaHome = System.getProperty("java.home");
// This works on Mac OS X.
File jarsignerFile = new File(javaHome + SLASH + "bin" +
SLASH + "jarsigner");
if (!jarsignerFile.exists()) {
// This works when a JDK is installed with the JRE.
jarsignerFile = new File(javaHome + SLASH + ".." + SLASH + "bin" +
SLASH + "jarsigner");
if (System.getProperty("os.name").startsWith("Windows")) {
jarsignerFile = new File(javaHome + SLASH + ".." + SLASH + "bin" +
SLASH + "jarsigner.exe");
}
if (!jarsignerFile.exists()) {
LOG.warning("YAIL compiler - could not find jarsigner.");
err.println("YAIL compiler - could not find jarsigner.");
userErrors.print(String.format(ERROR_IN_STAGE, "JarSigner"));
return false;
}
}
String[] jarsignerCommandLine = {
jarsignerFile.getAbsolutePath(),
"-digestalg", "SHA1",
"-sigalg", "MD5withRSA",
"-keystore", keystoreAbsolutePath,
"-storepass", "android",
apkAbsolutePath,
"AndroidKey"
};
if (!Execution.execute(null, jarsignerCommandLine, System.out, System.err)) {
LOG.warning("YAIL compiler - jarsigner execution failed.");
err.println("YAIL compiler - jarsigner execution failed.");
userErrors.print(String.format(ERROR_IN_STAGE, "JarSigner"));
return false;
}
return true;
}
private boolean runZipAlign(String apkAbsolutePath, File tmpDir) {
// TODO(user): add zipalign tool appinventor->lib->android->tools->linux and windows
// Need to make sure assets directory exists otherwise zipalign will fail.
......@@ -1500,7 +1461,7 @@ public final class Compiler {
}
// TODO: create tmp file for zipaling result
String zipAlignedPath = tmpDir.getAbsolutePath() + SLASH + "zipaligned.apk";
// zipalign -f -v 4 infile.zip outfile.zip
// zipalign -f 4 infile.zip outfile.zip
String[] zipAlignCommandLine = {
getResource(zipAlignTool),
"-f",
......@@ -1530,6 +1491,38 @@ public final class Compiler {
return true;
}
private boolean runApkSigner(String apkAbsolutePath, String keystoreAbsolutePath) {
int mx = childProcessRamMb - 200;
/*
apksigner sign\
--ks <keystore file>\
--ks-key-alias AndroidKey\
--ks-pass pass:android\
<APK>
*/
String[] apksignerCommandLine = {
System.getProperty("java.home") + "/bin/java", "-jar",
"-mx" + mx + "M",
getResource(APKSIGNER_JAR), "sign",
"-ks", keystoreAbsolutePath,
"-ks-key-alias", "AndroidKey",
"-ks-pass", "pass:android",
apkAbsolutePath
};
long startApkSigner = System.currentTimeMillis();
if (!Execution.execute(null, apksignerCommandLine, System.out, System.err)) {
LOG.warning("YAIL compiler - apksigner execution failed.");
err.println("YAIL compiler - apksigner execution failed.");
userErrors.print(String.format(ERROR_IN_STAGE, "APKSIGNER"));
return false;
}
String apkSignerTimeMessage = "APKSIGNER time: " + ((System.currentTimeMillis() - startApkSigner) / 1000.0) + " seconds";
out.println(apkSignerTimeMessage);
LOG.info(apkSignerTimeMessage);
return true;
}
/*
* Loads the icon for the application, either a user provided one or the default one.
*/
......
......@@ -180,6 +180,7 @@
<!-- END Android Support Libraries -->
<copy toFile="${public.deps.dir}/android.jar" file="${lib.dir}/android/android-26/android.jar" />
<copy toFile="${public.deps.dir}/dx.jar" file="${lib.dir}/android/tools/dx.jar" />
<copy toFile="${public.deps.dir}/apksigner.jar" file="${lib.dir}/android/tools/apksigner.jar" />
<copy toFile="${public.deps.dir}/CommonVersion.jar" file="${build.dir}/common/CommonVersion.jar" />
<!-- Add extension libraries here -->
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment