Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace JarSigner with ApkSigner #1417

Merged
merged 5 commits into from Apr 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -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";
Expand Down Expand Up @@ -1134,18 +1136,18 @@ public static boolean compile(Project project, Set<String> compTypes, Map<String
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);
}
Expand Down Expand Up @@ -1439,47 +1441,6 @@ private boolean generateClasses(File classesDir) {
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.
Expand All @@ -1500,7 +1461,7 @@ private boolean runZipAlign(String apkAbsolutePath, File tmpDir) {
}
// 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",
Expand Down Expand Up @@ -1530,6 +1491,38 @@ private boolean runZipAlign(String apkAbsolutePath, File tmpDir) {
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.
*/
Expand Down
1 change: 1 addition & 0 deletions appinventor/components/build.xml
Expand Up @@ -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 -->
Expand Down
Binary file added appinventor/lib/android/tools/apksigner.jar
Binary file not shown.