3

I have an Android app that I have been able to successfully build with Gradle (using Gradle 0.7). Now I want to set up Gradle to build two separate .APKs, one which had only ARM native libraries and the other with only Intel x86 native libraries. I have tried using productFlavors as in the following example:
How to configure NDK with Android Gradle plugin 0.7
However, with productFlavors specified in my Gradle script, the .APKs don't include any libraries at all (the libs dir itself is not in the .APK). Without productFlavors, the libraries are included - although the resulting APK will contain both ARM and x86 libraries.

With Gradle, it seems that our libraries are being placed in the following directories: build/javaResources/release/lib/armeabi-v7a
build/javaResources/release/lib/x86
build/javaResources/debug/lib/armeabi-v7a
build/javaResources/debug/lib/x86
Without productFlavors in the Gradle script, Gradle seems to know to look in those directories for the libraries. So I'm at a loss as to what's going on. Why would the inclusion of productFlavors prevent Gradle from looking there for the libraries?

Community
  • 1
  • 1
CalicoSkies
  • 331
  • 1
  • 3
  • 15

1 Answers1

4

About the example you are referring to, are you using the snippet from the main post or from its best answer ? The one in the main post is a bit outdated, maybe that's why it's not working with your project.

You have to use the abiFilter property inside your productFlavors, and put your native libraries inside jniLibs folder. Here is a sample build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 20
    buildToolsVersion "20"

    defaultConfig{
        minSdkVersion 15
        targetSdkVersion 20
        versionCode 101
        versionName "1.0.1"
    }

   flavorDimensions "abi"

    productFlavors {
        x86 {
            flavorDimension "abi"
            ndk {
                abiFilter "x86"
            }
            versionCode 5
        }
        arm {
            flavorDimension "abi"
            ndk {
                abiFilter "armeabi-v7a"
            }
            versionCode 2
        }
        all {
            flavorDimension "abi"
            versionCode 0
        }
    }

    // make per-variant version code
    applicationVariants.all { variant ->
        // get the version code of each flavor
        def abiVersion = variant.productFlavors.get(0).versionCode

        // set the composite code
        variant.mergedFlavor.versionCode = abiVersion * 100000 + defaultConfig.versionCode
    }
}

note that in this example, I also modify the versionCode of the app for each productFlavor, so it properly works with the multiple APKs handling functionality on the Play Store.

You can get more background information on how all this is working here: http://ph0b.com/android-studio-gradle-and-ndk-integration/

ph0b
  • 14,353
  • 4
  • 43
  • 41
  • Thanks for your reply. I've tried putting the libraries in the src/main/jniLibs folder, as described on the link I was reading, but it still didn't pick up the libs from there. Still, I have to wonder if there is a setting to tell Gradle where to find the libs if you want to put them in a different directory? – CalicoSkies Sep 17 '14 at 15:38
  • weird, have you deactivated the previous integration method ? You can change the libs directory like so: `android { ... sourceSets.main { jniLibs.srcDir 'src/main/libs' } }`. Also, you need to use at least the android plugin 0.7.3 for this to work (and the latest would be better) – ph0b Sep 18 '14 at 07:58
  • In my cased just adding to BuilTypes works and app crashes on add to flavors. – Mahdi Sep 05 '18 at 07:29