aboutsummaryrefslogtreecommitdiffstats
path: root/apkbuilder
diff options
context:
space:
mode:
authorThe Android Open Source Project <initial-contribution@android.com>2009-02-20 07:38:28 -0800
committerThe Android Open Source Project <initial-contribution@android.com>2009-02-20 07:38:28 -0800
commit76bc028c745906e691284c685e34e72b5ccf06b5 (patch)
treec6a908295dcbece6bb31f644b3d4698f04abee20 /apkbuilder
parent4fd2474506c1ea3cb96e128d72db2a18ec1e258d (diff)
downloadsdk-76bc028c745906e691284c685e34e72b5ccf06b5.zip
sdk-76bc028c745906e691284c685e34e72b5ccf06b5.tar.gz
sdk-76bc028c745906e691284c685e34e72b5ccf06b5.tar.bz2
auto import from //branches/cupcake/...@132569
Diffstat (limited to 'apkbuilder')
-rw-r--r--apkbuilder/src/com/android/apkbuilder/ApkBuilder.java245
1 files changed, 126 insertions, 119 deletions
diff --git a/apkbuilder/src/com/android/apkbuilder/ApkBuilder.java b/apkbuilder/src/com/android/apkbuilder/ApkBuilder.java
index 8616cda..40abff1 100644
--- a/apkbuilder/src/com/android/apkbuilder/ApkBuilder.java
+++ b/apkbuilder/src/com/android/apkbuilder/ApkBuilder.java
@@ -51,7 +51,7 @@ public final class ApkBuilder {
* A File to be added to the APK archive.
* <p/>This includes the {@link File} representing the file and its path in the archive.
*/
- private final static class ApkFile {
+ public final static class ApkFile {
String archivePath;
File file;
@@ -64,6 +64,8 @@ public final class ApkBuilder {
private JavaResourceFilter mResourceFilter = new JavaResourceFilter();
private boolean mVerbose = false;
private boolean mSignedPackage = true;
+ /** the optional type of the debug keystore. If <code>null</code>, the default */
+ private String mStoreType = null;
/**
* @param args
@@ -71,110 +73,97 @@ public final class ApkBuilder {
public static void main(String[] args) {
new ApkBuilder().run(args);
}
+
+ public void setVerbose(boolean verbose) {
+ mVerbose = verbose;
+ }
+
+ public void setSignedPackage(boolean signedPackage) {
+ mSignedPackage = signedPackage;
+ }
private void run(String[] args) {
if (args.length < 1) {
printUsageAndQuit();
}
-
- // read the first args that should be a file path
- File outFile = getOutFile(args[0]);
-
- ArrayList<FileInputStream> zipArchives = new ArrayList<FileInputStream>();
- ArrayList<File> archiveFiles = new ArrayList<File>();
- ArrayList<ApkFile> javaResources = new ArrayList<ApkFile>();
- ArrayList<FileInputStream> resourcesJars = new ArrayList<FileInputStream>();
- ArrayList<ApkFile> nativeLibraries = new ArrayList<ApkFile>();
-
- // optional store type.
- String storeType = null;
-
- int index = 1;
- do {
- String argument = args[index++];
-
- if ("-v".equals(argument)) {
- mVerbose = true;
- } else if ("-u".equals(argument)) {
- mSignedPackage = false;
- } else if ("-z".equals(argument)) {
- // quick check on the next argument.
- if (index == args.length) printUsageAndQuit();
-
- try {
- FileInputStream input = new FileInputStream(args[index++]);
- zipArchives.add(input);
- } catch (FileNotFoundException e) {
- printAndExit(e.getMessage());
- }
- } else if ("-f". equals(argument)) {
- // quick check on the next argument.
- if (index == args.length) printUsageAndQuit();
-
- archiveFiles.add(getInputFile(args[index++]));
- } else if ("-rf". equals(argument)) {
- // quick check on the next argument.
- if (index == args.length) printUsageAndQuit();
-
- processSourceFolderForResource(args[index++], javaResources);
- } else if ("-rj". equals(argument)) {
- // quick check on the next argument.
- if (index == args.length) printUsageAndQuit();
- String parameter = args[index++];
- File f = new File(parameter);
- if (f.isDirectory()) {
- String[] files = f.list(new FilenameFilter() {
- public boolean accept(File dir, String name) {
- return PATTERN_JAR_EXT.matcher(name).matches();
- }
- });
-
- for (String file : files) {
- try {
- String path = f.getAbsolutePath() + File.separator + file;
- FileInputStream input = new FileInputStream(path);
- resourcesJars.add(input);
- } catch (FileNotFoundException e) {
- printAndExit(e.getMessage());
- }
- }
- } else {
+ try {
+ // read the first args that should be a file path
+ File outFile = getOutFile(args[0]);
+
+ ArrayList<FileInputStream> zipArchives = new ArrayList<FileInputStream>();
+ ArrayList<File> archiveFiles = new ArrayList<File>();
+ ArrayList<ApkFile> javaResources = new ArrayList<ApkFile>();
+ ArrayList<FileInputStream> resourcesJars = new ArrayList<FileInputStream>();
+ ArrayList<ApkFile> nativeLibraries = new ArrayList<ApkFile>();
+
+ int index = 1;
+ do {
+ String argument = args[index++];
+
+ if ("-v".equals(argument)) {
+ mVerbose = true;
+ } else if ("-u".equals(argument)) {
+ mSignedPackage = false;
+ } else if ("-z".equals(argument)) {
+ // quick check on the next argument.
+ if (index == args.length) printUsageAndQuit();
+
try {
- FileInputStream input = new FileInputStream(parameter);
- resourcesJars.add(input);
+ FileInputStream input = new FileInputStream(args[index++]);
+ zipArchives.add(input);
} catch (FileNotFoundException e) {
printAndExit(e.getMessage());
}
+ } else if ("-f". equals(argument)) {
+ // quick check on the next argument.
+ if (index == args.length) printUsageAndQuit();
+
+ archiveFiles.add(getInputFile(args[index++]));
+ } else if ("-rf". equals(argument)) {
+ // quick check on the next argument.
+ if (index == args.length) printUsageAndQuit();
+
+ processSourceFolderForResource(args[index++], javaResources);
+ } else if ("-rj". equals(argument)) {
+ // quick check on the next argument.
+ if (index == args.length) printUsageAndQuit();
+
+ processJarFolder(args[index++], resourcesJars);
+ } else if ("-nf".equals(argument)) {
+ // quick check on the next argument.
+ if (index == args.length) printUsageAndQuit();
+
+ String parameter = args[index++];
+ File f = new File(parameter);
+
+ // compute the offset to get the relative path
+ int offset = parameter.length();
+ if (parameter.endsWith(File.separator) == false) {
+ offset++;
+ }
+
+ processNativeFolder(offset, f, nativeLibraries);
+ } else if ("-storetype".equals(argument)) {
+ // quick check on the next argument.
+ if (index == args.length) printUsageAndQuit();
+
+ mStoreType = args[index++];
+ } else {
+ printAndExit("Unknown argument: " + argument);
}
- } else if ("-nf".equals(argument)) {
- // quick check on the next argument.
- if (index == args.length) printUsageAndQuit();
-
- String parameter = args[index++];
- File f = new File(parameter);
-
- // compute the offset to get the relative path
- int offset = parameter.length();
- if (parameter.endsWith(File.separator) == false) {
- offset++;
- }
-
- processNativeFolder(offset, f, nativeLibraries);
- } else if ("-storetype".equals(argument)) {
- // quick check on the next argument.
- if (index == args.length) printUsageAndQuit();
-
- storeType = args[index++];
- } else {
- printAndExit("Unknown argument: " + argument);
- }
- } while (index < args.length);
-
- createPackage(outFile, zipArchives, archiveFiles, javaResources, resourcesJars,
- nativeLibraries, storeType);
+ } while (index < args.length);
+
+ createPackage(outFile, zipArchives, archiveFiles, javaResources, resourcesJars,
+ nativeLibraries);
+ } catch (IllegalArgumentException e) {
+ printAndExit(e.getMessage());
+ } catch (FileNotFoundException e) {
+ printAndExit(e.getMessage());
+ }
}
+
private File getOutFile(String filepath) {
File f = new File(filepath);
@@ -199,31 +188,30 @@ public final class ApkBuilder {
return f;
}
- private File getInputFile(String filepath) {
+ public static File getInputFile(String filepath) throws IllegalArgumentException {
File f = new File(filepath);
if (f.isDirectory()) {
- printAndExit(filepath + " is a directory!");
+ throw new IllegalArgumentException(filepath + " is a directory!");
}
if (f.exists()) {
if (f.canRead() == false) {
- printAndExit("Cannot read " + filepath);
+ throw new IllegalArgumentException("Cannot read " + filepath);
}
} else {
- printAndExit(filepath + " does not exists!");
+ throw new IllegalArgumentException(filepath + " does not exists!");
}
return f;
}
/**
- * Processes a source folder and add its java resources to the list of {@link ApkFile} to
- * write into the {@link SignedJarBuilder}.
+ * Processes a source folder and adds its java resources to a given list of {@link ApkFile}.
* @param folderPath the path to the source folder.
* @param javaResources the list of {@link ApkFile} to fill.
*/
- private void processSourceFolderForResource(String folderPath,
+ public static void processSourceFolderForResource(String folderPath,
ArrayList<ApkFile> javaResources) {
File folder = new File(folderPath);
@@ -237,12 +225,34 @@ public final class ApkBuilder {
} else {
// not a directory? output error and quit.
if (folder.exists()) {
- printAndExit(folderPath + " is not a folder!");
+ throw new IllegalArgumentException(folderPath + " is not a folder!");
} else {
- printAndExit(folderPath + " does not exist!");
+ throw new IllegalArgumentException(folderPath + " does not exist!");
+ }
+ }
+ }
+
+ public static void processJarFolder(String parameter, ArrayList<FileInputStream> resourcesJars)
+ throws FileNotFoundException {
+ File f = new File(parameter);
+ if (f.isDirectory()) {
+ String[] files = f.list(new FilenameFilter() {
+ public boolean accept(File dir, String name) {
+ return PATTERN_JAR_EXT.matcher(name).matches();
+ }
+ });
+
+ for (String file : files) {
+ String path = f.getAbsolutePath() + File.separator + file;
+ FileInputStream input = new FileInputStream(path);
+ resourcesJars.add(input);
}
+ } else {
+ FileInputStream input = new FileInputStream(parameter);
+ resourcesJars.add(input);
}
}
+
/**
* Processes a {@link File} that could be a {@link ApkFile}, or a folder containing
@@ -252,7 +262,7 @@ public final class ApkBuilder {
* identify a root file.
* @param javaResources the list of {@link ApkFile} object to fill.
*/
- private void processFileForResource(File file, String path,
+ private static void processFileForResource(File file, String path,
ArrayList<ApkFile> javaResources) {
if (file.isDirectory()) {
// a directory? we check it
@@ -292,7 +302,7 @@ public final class ApkBuilder {
* @param f the {@link File} to process
* @param nativeLibraries the array to add native libraries.
*/
- private void processNativeFolder(int offset, File f, ArrayList<ApkFile> nativeLibraries) {
+ public static void processNativeFolder(int offset, File f, ArrayList<ApkFile> nativeLibraries) {
if (f.isDirectory()) {
File[] children = f.listFiles();
@@ -318,13 +328,11 @@ public final class ApkBuilder {
* @param resourcesJars
* @param files
* @param javaResources
- * @param storeType the optional type of the debug keystore. If <code>null</code>, the default
* keystore type of the Java VM is used.
*/
- private void createPackage(File outFile, ArrayList<FileInputStream> zipArchives,
+ public void createPackage(File outFile, ArrayList<FileInputStream> zipArchives,
ArrayList<File> files, ArrayList<ApkFile> javaResources,
- ArrayList<FileInputStream> resourcesJars, ArrayList<ApkFile> nativeLibraries,
- String storeType) {
+ ArrayList<FileInputStream> resourcesJars, ArrayList<ApkFile> nativeLibraries) {
// get the debug key
try {
@@ -337,18 +345,18 @@ public final class ApkBuilder {
DebugKeyProvider keyProvider = new DebugKeyProvider(
null /* osKeyPath: use default */,
- storeType, null /* IKeyGenOutput */);
+ mStoreType, null /* IKeyGenOutput */);
PrivateKey key = keyProvider.getDebugKey();
X509Certificate certificate = (X509Certificate)keyProvider.getCertificate();
if (key == null) {
- printAndExit("Unable to get debug signature key");
+ throw new IllegalArgumentException("Unable to get debug signature key");
}
// compare the certificate expiration date
if (certificate != null && certificate.getNotAfter().compareTo(new Date()) < 0) {
// TODO, regenerate a new one.
- printAndExit("Debug Certificate expired on " +
+ throw new IllegalArgumentException("Debug Certificate expired on " +
DateFormat.getInstance().format(certificate.getNotAfter()));
}
@@ -403,21 +411,20 @@ public final class ApkBuilder {
builder.close();
} catch (KeytoolException e) {
if (e.getJavaHome() == null) {
- printAndExit(e.getMessage(),
- "JAVA_HOME seems undefined, setting it will help locating keytool automatically",
- "You can also manually execute the following command:",
+ throw new IllegalArgumentException(e.getMessage() +
+ "\nJAVA_HOME seems undefined, setting it will help locating keytool automatically\n" +
+ "You can also manually execute the following command\n:" +
e.getCommandLine());
} else {
- printAndExit(e.getMessage(),
- "JAVA_HOME is set to: " + e.getJavaHome(),
- "Update it if necessary, or manually execute the following command:",
+ throw new IllegalArgumentException(e.getMessage() +
+ "\nJAVA_HOME is set to: " + e.getJavaHome() +
+ "\nUpdate it if necessary, or manually execute the following command:\n" +
e.getCommandLine());
}
- System.err.println(e.getMessage());
} catch (AndroidLocationException e) {
- printAndExit(e.getMessage());
+ throw new IllegalArgumentException(e);
} catch (Exception e) {
- printAndExit(e.getMessage());
+ throw new IllegalArgumentException(e);
}
}