Merge "Deactivate multi-thread in multi-dex mode" into klp-dev
diff --git a/dx/src/com/android/dx/dex/cf/CfTranslator.java b/dx/src/com/android/dx/dex/cf/CfTranslator.java
index 8ab1c84..92ea0f7 100644
--- a/dx/src/com/android/dx/dex/cf/CfTranslator.java
+++ b/dx/src/com/android/dx/dex/cf/CfTranslator.java
@@ -142,20 +142,18 @@
         ConstantPool constantPool = cf.getConstantPool();
         int constantPoolSize = constantPool.size();
 
-        synchronized (dexFile) {
-            for (int i = 0; i < constantPoolSize; i++) {
-                Constant constant = constantPool.getOrNull(i);
-                if (constant instanceof CstMethodRef) {
-                    methodIdsSection.intern((CstBaseMethodRef) constant);
-                } else if (constant instanceof CstInterfaceMethodRef) {
-                    methodIdsSection.intern(((CstInterfaceMethodRef) constant).toMethodRef());
-                } else if (constant instanceof CstFieldRef) {
-                    fieldIdsSection.intern((CstFieldRef) constant);
-                } else if (constant instanceof CstEnumRef) {
-                    fieldIdsSection.intern(((CstEnumRef) constant).getFieldRef());
-                } else if (constant instanceof CstType) {
-                    typeIdsSection.intern((CstType) constant);
-                }
+        for (int i = 0; i < constantPoolSize; i++) {
+            Constant constant = constantPool.getOrNull(i);
+            if (constant instanceof CstMethodRef) {
+                methodIdsSection.intern((CstBaseMethodRef) constant);
+            } else if (constant instanceof CstInterfaceMethodRef) {
+                methodIdsSection.intern(((CstInterfaceMethodRef) constant).toMethodRef());
+            } else if (constant instanceof CstFieldRef) {
+                fieldIdsSection.intern((CstFieldRef) constant);
+            } else if (constant instanceof CstEnumRef) {
+                fieldIdsSection.intern(((CstEnumRef) constant).getFieldRef());
+            } else if (constant instanceof CstType) {
+                typeIdsSection.intern((CstType) constant);
             }
         }
 
@@ -197,9 +195,7 @@
                 if (annotations.size() != 0) {
                     out.addFieldAnnotations(field, annotations);
                 }
-                synchronized (fieldIdsSection) {
-                    fieldIdsSection.intern(field);
-                }
+                fieldIdsSection.intern(field);
             } catch (RuntimeException ex) {
                 String msg = "...while processing " + one.getName().toHuman() +
                     " " + one.getDescriptor().toHuman();
@@ -368,9 +364,7 @@
                 if (list.size() != 0) {
                     out.addParameterAnnotations(meth, list);
                 }
-                synchronized (methodIds) {
-                  methodIds.intern(meth);
-                }
+                methodIds.intern(meth);
             } catch (RuntimeException ex) {
                 String msg = "...while processing " + one.getName().toHuman() +
                     " " + one.getDescriptor().toHuman();
diff --git a/dx/src/com/android/dx/dex/file/FieldIdsSection.java b/dx/src/com/android/dx/dex/file/FieldIdsSection.java
index 27d946d..f422bab 100644
--- a/dx/src/com/android/dx/dex/file/FieldIdsSection.java
+++ b/dx/src/com/android/dx/dex/file/FieldIdsSection.java
@@ -20,6 +20,7 @@
 import com.android.dx.rop.cst.CstFieldRef;
 import com.android.dx.util.AnnotatedOutput;
 import com.android.dx.util.Hex;
+
 import java.util.Collection;
 import java.util.TreeMap;
 
@@ -94,7 +95,7 @@
      * @param field {@code non-null;} the reference to intern
      * @return {@code non-null;} the interned reference
      */
-    public FieldIdItem intern(CstFieldRef field) {
+    public synchronized FieldIdItem intern(CstFieldRef field) {
         if (field == null) {
             throw new NullPointerException("field == null");
         }
diff --git a/dx/src/com/android/dx/dex/file/MethodIdsSection.java b/dx/src/com/android/dx/dex/file/MethodIdsSection.java
index d99198b..254d7fe 100644
--- a/dx/src/com/android/dx/dex/file/MethodIdsSection.java
+++ b/dx/src/com/android/dx/dex/file/MethodIdsSection.java
@@ -20,6 +20,7 @@
 import com.android.dx.rop.cst.CstBaseMethodRef;
 import com.android.dx.util.AnnotatedOutput;
 import com.android.dx.util.Hex;
+
 import java.util.Collection;
 import java.util.TreeMap;
 
@@ -94,7 +95,7 @@
      * @param method {@code non-null;} the reference to intern
      * @return {@code non-null;} the interned reference
      */
-    public MethodIdItem intern(CstBaseMethodRef method) {
+    public synchronized MethodIdItem intern(CstBaseMethodRef method) {
         if (method == null) {
             throw new NullPointerException("method == null");
         }
diff --git a/dx/src/com/android/dx/dex/file/TypeIdsSection.java b/dx/src/com/android/dx/dex/file/TypeIdsSection.java
index ef47262..d9ab274 100644
--- a/dx/src/com/android/dx/dex/file/TypeIdsSection.java
+++ b/dx/src/com/android/dx/dex/file/TypeIdsSection.java
@@ -128,7 +128,7 @@
      * @param type {@code non-null;} the type to intern
      * @return {@code non-null;} the interned reference
      */
-    public TypeIdItem intern(CstType type) {
+    public synchronized TypeIdItem intern(CstType type) {
         if (type == null) {
             throw new NullPointerException("type == null");
         }
diff --git a/dx/src/com/android/dx/merge/DexMerger.java b/dx/src/com/android/dx/merge/DexMerger.java
index 8080947..507c076 100644
--- a/dx/src/com/android/dx/merge/DexMerger.java
+++ b/dx/src/com/android/dx/merge/DexMerger.java
@@ -28,6 +28,7 @@
 import com.android.dex.SizeOf;
 import com.android.dex.TableOfContents;
 import com.android.dex.TypeList;
+
 import java.io.File;
 import java.io.IOException;
 import java.util.ArrayList;
@@ -1049,7 +1050,12 @@
                     + contents.methodIds.size * SizeOf.MEMBER_ID_ITEM
                     + contents.classDefs.size * SizeOf.CLASS_DEF_ITEM;
             mapList = SizeOf.UINT + (contents.sections.length * SizeOf.MAP_ITEM);
-            typeList += contents.typeLists.byteCount;
+            typeList += fourByteAlign(contents.typeLists.byteCount); // We count each dex's
+            // typelists section as realigned on 4 bytes, because each typelist of each dex's
+            // typelists section is aligned on 4 bytes. If we didn't, there is a case where each
+            // size of both dex's typelists section is a multiple of 2 but not a multiple of 4,
+            // and the sum of both sizes is a multiple of 4 but would not be sufficient to write
+            // each typelist aligned on 4 bytes.
             stringData += contents.stringDatas.byteCount;
             annotationsDirectory += contents.annotationsDirectories.byteCount;
             annotationsSet += contents.annotationSets.byteCount;