Merge "Dexmaker: Update attachJvmtiAgent API use"
diff --git a/dexmaker-mockito-inline/src/main/java/com/android/dx/mockito/inline/JvmtiAgent.java b/dexmaker-mockito-inline/src/main/java/com/android/dx/mockito/inline/JvmtiAgent.java
index 1b4a550..aca7a0c 100644
--- a/dexmaker-mockito-inline/src/main/java/com/android/dx/mockito/inline/JvmtiAgent.java
+++ b/dexmaker-mockito-inline/src/main/java/com/android/dx/mockito/inline/JvmtiAgent.java
@@ -34,7 +34,7 @@
  * Interface to the native jvmti agent in agent.cc
  */
 class JvmtiAgent {
-    private static final String AGENT_LIB_NAME = "dexmakerjvmtiagent";
+    private static final String AGENT_LIB_NAME = "libdexmakerjvmtiagent.so";
 
     private static final Object lock = new Object();
 
@@ -65,32 +65,14 @@
                     + "by a BaseDexClassLoader");
         }
 
-        // Currently Debug.attachJvmtiAgent requires a file in the right directory
-        File copiedAgent = File.createTempFile("agent", ".so");
-        copiedAgent.deleteOnExit();
-
-        try (InputStream is = new FileInputStream(
-                ((BaseDexClassLoader) cl).findLibrary(AGENT_LIB_NAME))) {
-            try (OutputStream os = new FileOutputStream(copiedAgent)) {
-                byte[] buffer = new byte[64 * 1024];
-
-                while (true) {
-                    int numRead = is.read(buffer);
-                    if (numRead == -1) {
-                        break;
-                    }
-                    os.write(buffer, 0, numRead);
-                }
-            }
-        }
-
         try {
             /*
              * TODO (moltmann@google.com): Replace with regular method call once the API becomes
              *                             public
              */
             Class.forName("android.os.Debug").getMethod("attachJvmtiAgent", String.class,
-                    String.class).invoke(null, copiedAgent.getAbsolutePath(), null);
+                    String.class, ClassLoader.class).invoke(null, AGENT_LIB_NAME,
+                    null, cl);
         } catch (InvocationTargetException e) {
             loadJvmtiException = e.getCause();
         } catch (IllegalAccessException | ClassNotFoundException | NoSuchMethodException e) {
@@ -99,7 +81,7 @@
 
         if (loadJvmtiException != null) {
             if (loadJvmtiException instanceof IOException) {
-                throw (IOException)loadJvmtiException;
+                throw new IOException(cl.toString(), loadJvmtiException);
             } else {
                 throw new IOException("Could not load jvmti plugin",
                         loadJvmtiException);