[Android] LibVLC: link with true compat lib for old devices (SDK 7 & 8)

Thomas Guillem git at videolan.org
Wed Feb 3 16:57:06 CET 2016


vlc-android | branch: master | Thomas Guillem <thomas at gllm.fr> | Wed Feb  3 16:41:31 2016 +0100| [baacbe2b8306bd86cb862e91f4f7752da2979303] | committer: Thomas Guillem

LibVLC: link with true compat lib for old devices (SDK 7 & 8)

Build the true libcompat.7.so and copy it into assets folder.
On first run, the lib will be copied from the assets to the data dir.
This lib will be linked instead of the dummy one.

> https://code.videolan.org/videolan/vlc-android/commit/baacbe2b8306bd86cb862e91f4f7752da2979303
---

 compile-libvlc.sh                                  | 11 +++++
 libvlc/build.gradle                                |  2 +-
 .../src/org/videolan/vlc/util/VLCInstance.java     | 53 ++++++++++++++++++++++
 3 files changed, 65 insertions(+), 1 deletion(-)

diff --git a/compile-libvlc.sh b/compile-libvlc.sh
index de55dd5..b02bd32 100755
--- a/compile-libvlc.sh
+++ b/compile-libvlc.sh
@@ -676,6 +676,17 @@ $ANDROID_NDK/ndk-build -C libvlc \
 
 checkfail "ndk-build failed"
 
+if [ "${ANDROID_API}" = "android-9" ] && [ "${ANDROID_ABI}" = "armeabi-v7a" -o "${ANDROID_ABI}" = "armeabi" ] ; then
+    $ANDROID_NDK/ndk-build -C libvlc \
+        APP_BUILD_SCRIPT=libcompat/Android.mk \
+        APP_PLATFORM=${ANDROID_API} \
+        APP_ABI="armeabi" \
+        NDK_PROJECT_PATH=libcompat \
+        NDK_TOOLCHAIN_VERSION=${GCCVER} \
+        NDK_DEBUG=${NDK_DEBUG}
+    checkfail "ndk-build compat failed"
+fi
+
 DBG_LIB_DIR=libvlc/jni/obj/local/${ANDROID_ABI}
 OUT_LIB_DIR=libvlc/jni/libs/${ANDROID_ABI}
 VERSION=$(grep "android:versionName" vlc-android/AndroidManifest.xml|cut -d\" -f 2)
diff --git a/libvlc/build.gradle b/libvlc/build.gradle
index 8c916f9..bfa82af 100644
--- a/libvlc/build.gradle
+++ b/libvlc/build.gradle
@@ -14,7 +14,7 @@ android {
         aidl.srcDirs = ['src']
         renderscript.srcDirs = ['src']
         res.srcDirs = ['res']
-        assets.srcDirs = ['assets']
+        assets.srcDirs = ['assets', 'libcompat/libs/armeabi']
     }
     }
 
diff --git a/vlc-android/src/org/videolan/vlc/util/VLCInstance.java b/vlc-android/src/org/videolan/vlc/util/VLCInstance.java
index dc45ded..5185b3e 100644
--- a/vlc-android/src/org/videolan/vlc/util/VLCInstance.java
+++ b/vlc-android/src/org/videolan/vlc/util/VLCInstance.java
@@ -22,6 +22,7 @@ package org.videolan.vlc.util;
 
 import android.content.Context;
 import android.content.Intent;
+import android.os.Build;
 import android.util.Log;
 
 import org.videolan.libvlc.LibVLC;
@@ -31,11 +32,59 @@ import org.videolan.vlc.VLCCrashHandler;
 import org.videolan.vlc.gui.CompatErrorActivity;
 import org.videolan.vlc.gui.NativeCrashActivity;
 
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
 public class VLCInstance {
     public final static String TAG = "VLC/UiTools/VLCInstance";
 
     private static LibVLC sLibVLC = null;
 
+    public static void linkCompatLib(Context context) {
+        final File outDir = new File(context.getFilesDir(), "compat");
+        if (!outDir.exists())
+            outDir.mkdir();
+        final File outFile = new File(outDir, "libcompat.7.so");
+
+        /* The file may had been already copied from the asset, try to load it */
+        if (outFile.exists()) {
+            try {
+                System.load(outFile.getPath());
+                return;
+            } catch (UnsatisfiedLinkError ule) {
+                /* the file can be invalid, try to copy it again */
+            }
+        }
+
+        /* copy libcompat.7.so from assert to a data dir */
+        InputStream is = null;
+        FileOutputStream fos = null;
+        boolean success = false;
+        try {
+            is = VLCApplication.getAppResources().getAssets().open("libcompat.7.so");
+            fos = new FileOutputStream(outFile);
+            final byte[] buffer = new byte[16*1024];
+            int read;
+            while ((read = is.read(buffer)) != -1)
+                fos.write(buffer, 0, read);
+            success = true;
+        } catch (IOException e) {
+        } finally {
+            Util.close(is);
+            Util.close(fos);
+        }
+
+        /* load the lib coming from the asset */
+        if (success) {
+            try {
+                System.load(outFile.getPath());
+            } catch (UnsatisfiedLinkError ule) {
+            }
+        }
+    }
+
     /** A set of utility functions for the VLC application */
     public synchronized static LibVLC get() throws IllegalStateException {
         if (sLibVLC == null) {
@@ -46,6 +95,10 @@ public class VLCInstance {
                 Log.e(TAG, VLCUtil.getErrorMsg());
                 throw new IllegalStateException("LibVLC initialisation failed: " + VLCUtil.getErrorMsg());
             }
+            if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.FROYO) {
+                Log.w(TAG, "linking with true compat lib...");
+                linkCompatLib(context);
+            }
 
             sLibVLC = new LibVLC(VLCOptions.getLibOptions());
             LibVLC.setOnNativeCrashListener(new LibVLC.OnNativeCrashListener() {



More information about the Android mailing list