break SF dependencies on libdvm and libandroid_runtime

these libraries are only needed for debugging and are now
linked at runtime if needed.

Change-Id: I03f138523c6de166a1e2700d4454d4a854aee145
diff --git a/services/surfaceflinger/Android.mk b/services/surfaceflinger/Android.mk
index 1f7affd..dd0dc16 100644
--- a/services/surfaceflinger/Android.mk
+++ b/services/surfaceflinger/Android.mk
@@ -3,6 +3,7 @@
 
 LOCAL_SRC_FILES:= \
     Client.cpp                              \
+    DdmConnection.cpp                       \
     DisplayDevice.cpp                       \
     EventThread.cpp                         \
     Layer.cpp                               \
@@ -39,6 +40,7 @@
 
 LOCAL_SHARED_LIBRARIES := \
 	libcutils \
+	libdl \
 	libhardware \
 	libutils \
 	libEGL \
@@ -47,13 +49,6 @@
 	libui \
 	libgui
 
-# this is only needed for DDMS debugging
-ifneq ($(TARGET_BUILD_PDK),true)
-	LOCAL_SHARED_LIBRARIES += libdvm libandroid_runtime
-	LOCAL_CFLAGS += -DDDMS_DEBUGGING
-	LOCAL_SRC_FILES += DdmConnection.cpp
-endif
-
 LOCAL_MODULE:= libsurfaceflinger
 
 include $(BUILD_SHARED_LIBRARY)
diff --git a/services/surfaceflinger/DdmConnection.cpp b/services/surfaceflinger/DdmConnection.cpp
index 467a915..0b895e2 100644
--- a/services/surfaceflinger/DdmConnection.cpp
+++ b/services/surfaceflinger/DdmConnection.cpp
@@ -14,16 +14,16 @@
  * limitations under the License.
  */
 
+#include <dlfcn.h>
+
 #include <android_runtime/AndroidRuntime.h>
 
 #include "jni.h"
 #include "DdmConnection.h"
 
-extern "C" jint Java_com_android_internal_util_WithFramework_registerNatives(
-        JNIEnv* env, jclass clazz);
-
 namespace android {
 
+
 void DdmConnection::start(const char* name) {
     JavaVM* vm;
     JNIEnv* env;
@@ -40,12 +40,36 @@
     args.nOptions = 1;
     args.ignoreUnrecognized = JNI_FALSE;
 
+
+    void* libdvm_dso = dlopen("libdvm.so", RTLD_NOW);
+    ALOGE_IF(!libdvm_dso, "DdmConnection: %s", dlerror());
+
+    void* libandroid_runtime_dso = dlopen("libandroid_runtime.so", RTLD_NOW);
+    ALOGE_IF(!libandroid_runtime_dso, "DdmConnection: %s", dlerror());
+
+    if (!libdvm_dso || !libandroid_runtime_dso) {
+        goto error;
+    }
+
+    jint (*JNI_CreateJavaVM)(JavaVM** p_vm, JNIEnv** p_env, void* vm_args);
+    JNI_CreateJavaVM = (typeof JNI_CreateJavaVM)dlsym(libdvm_dso, "JNI_CreateJavaVM");
+    ALOGE_IF(!JNI_CreateJavaVM, "DdmConnection: %s", dlerror());
+
+    jint (*registerNatives)(JNIEnv* env, jclass clazz);
+    registerNatives = (typeof registerNatives)dlsym(libandroid_runtime_dso,
+        "Java_com_android_internal_util_WithFramework_registerNatives");
+    ALOGE_IF(!registerNatives, "DdmConnection: %s", dlerror());
+
+    if (!JNI_CreateJavaVM || !registerNatives) {
+        goto error;
+    }
+
     if (JNI_CreateJavaVM(&vm, &env, &args) == 0) {
         jclass startClass;
         jmethodID startMeth;
 
         // register native code
-        if (Java_com_android_internal_util_WithFramework_registerNatives(env, 0) == 0) {
+        if (registerNatives(env, 0) == 0) {
             // set our name by calling DdmHandleAppName.setAppName()
             startClass = env->FindClass("android/ddm/DdmHandleAppName");
             if (startClass) {
@@ -70,6 +94,15 @@
             }
         }
     }
+    return;
+
+error:
+    if (libandroid_runtime_dso) {
+        dlclose(libandroid_runtime_dso);
+    }
+    if (libdvm_dso) {
+        dlclose(libdvm_dso);
+    }
 }
 
 }; // namespace android
diff --git a/services/surfaceflinger/SurfaceFlinger.cpp b/services/surfaceflinger/SurfaceFlinger.cpp
index c64515d..382e013 100644
--- a/services/surfaceflinger/SurfaceFlinger.cpp
+++ b/services/surfaceflinger/SurfaceFlinger.cpp
@@ -105,18 +105,14 @@
     property_get("debug.sf.showupdates", value, "0");
     mDebugRegion = atoi(value);
 
-#ifdef DDMS_DEBUGGING
     property_get("debug.sf.ddms", value, "0");
     mDebugDDMS = atoi(value);
     if (mDebugDDMS) {
         DdmConnection::start(getServiceName());
     }
-#else
-#warning "DDMS_DEBUGGING disabled"
-#endif
 
-    ALOGI_IF(mDebugRegion,       "showupdates enabled");
-    ALOGI_IF(mDebugDDMS,         "DDMS debugging enabled");
+    ALOGI_IF(mDebugRegion, "showupdates enabled");
+    ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
 }
 
 void SurfaceFlinger::onFirstRef()