在 vlc-android-sdk 上使用 Gradle 风格

Using Gradle flavors on vlc-android-sdk

在我的应用程序中,我使用了 "de.mrmaffen:vlc-android-sdk:2.0.6" 库,它占用了大量存储空间,但我找不到更小的替代方案。
所以我决定 使用拆分或产品口味,但我无法让它工作。
我尝试按照文档进行操作,但没有成功。
任何帮助表示赞赏。

自己构建 LibVLC Android SDK

如果您使用的是"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


构建特定版本的 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  

确保您在依赖项方面遵循了 Android 编译说明并检查 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.

通过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'
}

我正在使用这个版本。记得将 JCenter 添加到项目的 build.gradle 文件中:

allprojects {
    repositories {
        jcenter()
    }
}

VLC Android 通过 JCenter 的 SDK 支持下一个 ABI:armeabi-v7a、arm64-v8a、x86 和 x86_64。

您可以在应用的 build.gradle 文件中过滤特定的 ABI(x86_64 和 arm64-v8a 被排除在外):

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

可以在 mrmaffen's repository 和最近的 Android Studio 版本中检查包含的文件:


为 ABI 配置多个 APK

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
    }
  }
}

构建多个 APK

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.


配置构建变体

This page builds on the Configure Your Build Overview 向您展示如何配置构建变体以创建不同版本的应用。


合并 abi 过滤器和构建变体

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

通过 ABI 和密度拆分的多 APK

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.


推荐媒介 post 选择支持的 ABI

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

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.


解决部分设备上的UnsatisfiedLinkError问题

As explained here,64 位处理器生成并检查 arm64 文件夹以加载本机库。

如果你的项目没有arm64文件夹。这是解决方案:

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

您可以使用 app bundle,这样您就不需要处理多个 apk。 Google Play 会自动为您处理。此外,这种方法可以提供额外的好处。更多详情:https://developer.android.com/guide/app-bundle