Using Gradle flavors on vlc-android-sdk

2.4k Views Asked by At

In my app I'm using "de.mrmaffen:vlc-android-sdk:2.0.6" library and it's taking much storage but I can't find a smaller alternative.
So I decided as CommonsWare mentioned to use splits or product flavors but I can't get it to work.
I tried to follow the docs with no luck.
Any help appreciated.

2

There are 2 best solutions below

0
On BEST ANSWER

Building the LibVLC Android SDK yourself

If you are using "de.mrmaffen:vlc-android-sdk:2.0.6", buid the LibVLC Android SDK yourself

As explained here, afterwards simply run this Gradle command: ./gradlew buildLibVlc


Building a specific version of the LibVLC Android SDK

cd vlc-android          // if this folder doesn't exist yet, simply run ./gradlew cloneVlcAndroid
git tag                 // to list all release versions
git checkout {tag-name} // to checkout the git repo at the given tag
cd ..
./gradlew buildLibVlc   // build it  

Make sure you followed the Android compile instructions in terms of dependencies and check this:

  1. git clone repo.
  2. open up a command prompt in this repo. "cd" or change directory into it.
  3. git clone .
  4. change directory into the vlc-android directory.
  5. git tag
  6. git checkout <tag_version>. In this case, 2.1.2.
  7. cd ..
  8. comment out both the compile/build of the build.gradle script that you don't need.
  9. ./gradlew buildLibVlc
  10. you should have a successful build with both the Java sources and shared object (*.so) files in the jniLibs folder.
  11. create a libs folder right next to the jniLibs folder if you updated the gradle version.

Get it via Maven CentralJCenter

Just add this dependency to your project and you're good to go.

dependencies {
    implementation 'de.mrmaffen:libvlc-android:2.1.12@aar'
}

I'm using this version. Remember to add JCenter to your project's build.gradle file:

allprojects {
    repositories {
        jcenter()
    }
}

VLC Android SDK via JCenter supports the next ABIs: armeabi-v7a, arm64-v8a, x86 and x86_64.

You can filter specific ABIs in your app's build.gradle file (x86_64 and arm64-v8a are excluded):

android {
    ndk {
        abiFilters "armeabi-v7a", "x86"
    }
}

It's possible to check included files in mrmaffen's repository and via recent Android Studio versions:

enter image description here


Configure multiple APKs for ABIs

Add an abi block inside your splits block. In your abi block, provide a list of desired ABIs. Source

android {
  ...
  splits {

    // Configures multiple APKs based on ABI.
    abi {

      // Enables building multiple APKs per ABI.
      enable true

      // By default all ABIs are included, so use reset() and include to specify that we only
      // want APKs for x86 and armeabi-v7a.

      // Resets the list of ABIs that Gradle should create APKs for to none.
      reset()

      // Specifies a list of ABIs that Gradle should create APKs for.
      include "x86", "armeabi-v7a"

      // Specifies that we do not want to also generate a universal APK that includes all ABIs.
      universalApk false
    }
  }
}

Build multiple APKs

Click Build > Build APK to build all APKs for the currently selected module in the Project pane.

Gradle creates the APKs for each ABI into the project's build/outputs/apk/ directory.


Configure Build Variants

This page builds on the Configure Your Build Overview to show you how you can configure build variants to create different versions of your app.


Combine abi filters and build variants

Exclude abi from apk

android {
    productFlavors {
        x86 {
            ndk {
                abiFilter "x86"
            }
        }
        arm {
            ndk {
                abiFilters "armeabi-v7a", "armeabi"
            }
        }
    }
}

Multi-APK through ABI and density splits

How to reduce the number of APKs with ABI splits

Here’s a code snippet that you can use to set version codes for ABI splits.

Give the x86_64 and x86 higher version numbers than ARM, as many x86 devices can run ARM code through an emulation layer, although with lower performance.

If you don’t wish to manage too many APKs, target the most popular ones (usually ARM and maybe x86) with a split APK and serve a universal APK to everyone else.

It’s vital that you publish the universal APK on the Play Store with a lower version number than all other ABI-specific packages.

If you need more flexibility for your Multi-APK setup, check out Multi-APK through product flavors.


Recommended Medium post to choose supported ABIs

In this post we will understand what is ABI, problems with the ABI split, and alternative way to avoid those problems... For instance, Android supports the following ABIs :

mips, mips64, X86, X86–64, arm64-v8a, armeabi, armeabi-v7a

enter image description here

So you have to take a call on supporting 64-bit libraries based on size vs performance criteria but mips, mips-64, and armeabi should be removed without any hesitation.


Solve UnsatisfiedLinkError Problem on some devices

As explained here, 64-bit processors generate and check arm64 folder to load native libraries.

If your project does not have arm64 folder. Here is the solution:

build.gradle

defaultConfig {
    ...

    ndk {
        abiFilters "armeabi-v7a", "x86"
    }

}

You need to add this filters(abiFilters) to your app module’s build.gradle files. So when your device try to run your app, it will check gradle file and understands that it should not generate any folder and use existing native library resources. Boom. Almost solved. But still there is one more thing. Add this line to your gradle.properties to use deprecated Ndk.

gradle.properties

android.useDeprecatedNdk=true
0
On

You can use app bundle so you don't need to handle multiple apks. Google Play will handle it for you automatically. Plus this approach can provide additional benefits. For more details: https://developer.android.com/guide/app-bundle