Merge "Issue: ProxyBuilder.getMethodsToProxyRecursive() was not returning list of methods in deterministic order."
diff --git a/src/main/java/com/google/dexmaker/stock/ProxyBuilder.java b/src/main/java/com/google/dexmaker/stock/ProxyBuilder.java
index b3a8d8a..f943840 100644
--- a/src/main/java/com/google/dexmaker/stock/ProxyBuilder.java
+++ b/src/main/java/com/google/dexmaker/stock/ProxyBuilder.java
@@ -38,6 +38,7 @@
 import java.lang.reflect.UndeclaredThrowableException;
 import java.util.Arrays;
 import java.util.Collections;
+import java.util.Comparator;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
@@ -613,6 +614,16 @@
         for (MethodSetEntry entry : methodsToProxy) {
             results[i++] = entry.originalMethod;
         }
+
+        // Sort the results array so that they are returned by this method
+        // in a deterministic fashion.
+        Arrays.sort(results, new Comparator<Method>() {
+            @Override
+            public int compare(Method method1, Method method2) {
+                return method1.toString().compareTo(method2.toString());
+            }
+        });
+
         return results;
     }
 
diff --git a/src/test/java/com/google/dexmaker/stock/ProxyBuilderTest.java b/src/test/java/com/google/dexmaker/stock/ProxyBuilderTest.java
index 4c02f83..e6912c4 100644
--- a/src/test/java/com/google/dexmaker/stock/ProxyBuilderTest.java
+++ b/src/test/java/com/google/dexmaker/stock/ProxyBuilderTest.java
@@ -19,10 +19,12 @@
 import com.google.dexmaker.DexMakerTest;
 import java.io.File;
 import java.io.IOException;
+import java.lang.reflect.Field;
 import java.lang.reflect.InvocationHandler;
 import java.lang.reflect.Method;
 import java.lang.reflect.UndeclaredThrowableException;
 import java.util.Arrays;
+import java.util.Map;
 import java.util.Random;
 import java.util.concurrent.Callable;
 import java.util.concurrent.atomic.AtomicInteger;