summaryrefslogtreecommitdiffstats
path: root/dx
diff options
context:
space:
mode:
authorJesse Wilson <jessewilson@google.com>2011-03-07 23:42:34 -0800
committerAndroid (Google) Code Review <android-gerrit@google.com>2011-03-07 23:42:34 -0800
commit87508f44a0ace550ae397ab83fc2a0fe1967259f (patch)
treec2663e98567fb10bc78b372f3ea23334c46093cd /dx
parent8f7a48e6159d4c81eeaf4962230e79e52a203ccb (diff)
parentd2b3f499be6f5b1d4076b1244be3d1501d178315 (diff)
downloadandroid_dalvik-87508f44a0ace550ae397ab83fc2a0fe1967259f.tar.gz
android_dalvik-87508f44a0ace550ae397ab83fc2a0fe1967259f.tar.bz2
android_dalvik-87508f44a0ace550ae397ab83fc2a0fe1967259f.zip
Merge "Permit dx to build resource-only dex files." into dalvik-dev
Diffstat (limited to 'dx')
-rw-r--r--dx/src/com/android/dx/command/dexer/Main.java75
-rw-r--r--dx/src/com/android/dx/dex/file/DexFile.java7
2 files changed, 60 insertions, 22 deletions
diff --git a/dx/src/com/android/dx/command/dexer/Main.java b/dx/src/com/android/dx/command/dexer/Main.java
index b1216686f..6e8fa7eed 100644
--- a/dx/src/com/android/dx/command/dexer/Main.java
+++ b/dx/src/com/android/dx/command/dexer/Main.java
@@ -59,6 +59,7 @@ import java.util.jar.Attributes;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
+import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
/**
@@ -208,13 +209,18 @@ public class Main {
return 0; // this was a no-op incremental build
}
- byte[] outArray = writeDex();
+ // this array is null if no classes were defined
+ byte[] outArray = null;
+
+ if (!outputDex.isEmpty()) {
+ outArray = writeDex();
- if (outArray == null) {
- return 2;
+ if (outArray == null) {
+ return 2;
+ }
}
- if (args.incremental && incrementalOutFile.exists()) {
+ if (args.incremental) {
outArray = merge(outArray, incrementalOutFile);
}
@@ -225,7 +231,7 @@ public class Main {
if (!createJar(args.outName, outArray)) {
return 3;
}
- } else if (args.outName != null) {
+ } else if (outArray != null && args.outName != null) {
OutputStream out = openOutput(args.outName);
out.write(outArray);
closeOutput(out);
@@ -236,27 +242,50 @@ public class Main {
/**
* Merges the dex files {@code update} and {@code base}, preferring
- * {@code update}'s definition for types defined in both dex files. Returns
- * the bytes of the merged dex file.
+ * {@code update}'s definition for types defined in both dex files.
+ *
+ * @return the bytes of the merged dex file, or null if both the update
+ * and the base dex do not exist.
*/
private static byte[] merge(byte[] update, File base) throws IOException {
- DexBuffer dexA = new DexBuffer();
- dexA.loadFrom(new ByteArrayInputStream(update));
+ DexBuffer dexA = null;
+ DexBuffer dexB = null;
- DexBuffer dexB = new DexBuffer();
- if (args.jarOutput) {
- ZipFile zipFile = new ZipFile(base);
- dexB.loadFrom(zipFile.getInputStream(zipFile.getEntry(DexFormat.DEX_IN_JAR_NAME)));
- zipFile.close();
+ if (update != null) {
+ dexA = new DexBuffer();
+ dexA.loadFrom(new ByteArrayInputStream(update));
+ }
+
+ if (base.exists()) {
+ if (args.jarOutput) {
+ ZipFile zipFile = new ZipFile(base);
+ ZipEntry entry = zipFile.getEntry(DexFormat.DEX_IN_JAR_NAME);
+ if (entry != null) {
+ dexB = new DexBuffer();
+ dexB.loadFrom(zipFile.getInputStream(entry));
+ zipFile.close();
+ }
+ } else {
+ InputStream in = new FileInputStream(base);
+ dexB = new DexBuffer();
+ dexB.loadFrom(in);
+ in.close();
+ }
+ }
+
+ DexBuffer result;
+ if (dexA == null && dexB == null) {
+ return null;
+ } else if (dexA == null) {
+ result = dexB;
+ } else if (dexB == null) {
+ result = dexA;
} else {
- InputStream in = new FileInputStream(base);
- dexB.loadFrom(in);
- in.close();
+ result = new DexMerger(dexA, dexB).merge();
}
- DexBuffer merged = new DexMerger(dexA, dexB).merge();
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
- merged.writeTo(bytesOut);
+ result.writeTo(bytesOut);
return bytesOut.toByteArray();
}
@@ -555,8 +584,8 @@ public class Main {
* Creates a jar file from the resources and given dex file array.
*
* @param fileName {@code non-null;} name of the file
- * @param dexArray {@code non-null;} array containing the dex file
- * to include
+ * @param dexArray array containing the dex file to include, or null if the
+ * output contains no class defs.
* @return whether the creation was successful
*/
private static boolean createJar(String fileName, byte[] dexArray) {
@@ -571,7 +600,9 @@ public class Main {
OutputStream out = openOutput(fileName);
JarOutputStream jarOut = new JarOutputStream(out, manifest);
- outputResources.put(DexFormat.DEX_IN_JAR_NAME, dexArray);
+ if (dexArray != null) {
+ outputResources.put(DexFormat.DEX_IN_JAR_NAME, dexArray);
+ }
try {
for (Map.Entry<String, byte[]> e :
diff --git a/dx/src/com/android/dx/dex/file/DexFile.java b/dx/src/com/android/dx/dex/file/DexFile.java
index 1cc9358bd..73f08647a 100644
--- a/dx/src/com/android/dx/dex/file/DexFile.java
+++ b/dx/src/com/android/dx/dex/file/DexFile.java
@@ -134,6 +134,13 @@ public final class DexFile {
}
/**
+ * Returns true if this dex doesn't contain any class defs.
+ */
+ public boolean isEmpty() {
+ return classDefs.items().isEmpty();
+ }
+
+ /**
* Adds a class to this instance. It is illegal to attempt to add more
* than one class with the same name.
*