Merge "Only load libRSSupport and librsjni when necessary." into jb-mr2-dev
diff --git a/renderscript/v8/java/src/android/support/v8/renderscript/RenderScript.java b/renderscript/v8/java/src/android/support/v8/renderscript/RenderScript.java
index 6ea47c5..750b6f4 100644
--- a/renderscript/v8/java/src/android/support/v8/renderscript/RenderScript.java
+++ b/renderscript/v8/java/src/android/support/v8/renderscript/RenderScript.java
@@ -57,22 +57,9 @@
      */
     @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"})
     static boolean sInitialized;
+    static Object lock = new Object();
     native static void _nInit();
 
-
-    static {
-        sInitialized = false;
-        try {
-            System.loadLibrary("RSSupport");
-            System.loadLibrary("rsjni");
-            _nInit();
-            sInitialized = true;
-        } catch (UnsatisfiedLinkError e) {
-            Log.e(LOG_TAG, "Error loading RS jni library: " + e);
-            throw new RSRuntimeException("Error loading RS jni library: " + e);
-        }
-    }
-
     // Non-threadsafe functions.
     native int  nDeviceCreate();
     native void nDeviceDestroy(int dev);
@@ -888,6 +875,19 @@
             android.util.Log.v(LOG_TAG, "RS native mode");
             return RenderScriptThunker.create(ctx, sdkVersion);
         }
+        synchronized(lock) {
+            if (sInitialized == false) {
+                try {
+                    System.loadLibrary("RSSupport");
+                    System.loadLibrary("rsjni");
+                    _nInit();
+                    sInitialized = true;
+                } catch (UnsatisfiedLinkError e) {
+                    Log.e(LOG_TAG, "Error loading RS jni library: " + e);
+                    throw new RSRuntimeException("Error loading RS jni library: " + e);
+                }
+            }
+        }
 
         android.util.Log.v(LOG_TAG, "RS compat mode");
         rs.mDev = rs.nDeviceCreate();