Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio create a build variant / type excluding jniLibs?

I have an Android application that uses a native JNI library. I put it into app/src/main/jniLibs/armeabi-v7a without any gradle configuration and Android studio happily bundles it into the APK.

I have a requirement where all native libraries cannot be bundled with a certain distribution. Is it possible to create a build variant or build type that simply excludes all native libraries (maybe even by name .so).

The fact that the native library is missing in this distribution doesn't matter because it's not used. The alternative is to physically remove the files, run the build, put them back. However, this is painful and error prone.

like image 843
Elliot Chance Avatar asked Jun 23 '15 07:06

Elliot Chance


People also ask

How do I create a build variant?

You can change the build variant to whichever one you want to build and run—just go to Build > Select Build Variant and select one from the drop-down menu. To start customizing each build variant with its own features and resources, however, you'll need to know how to create and manage source sets.

How do I change a variant in build?

NOTE: By default, the Android Studio will generate "debug" and "release" Build Types for your project. So, to change a Build Type, all you need to do is just select your Build Type from the Build Variant and after the project sync, you are good to go.

What is flavorDimensions?

Flavor Dimensions is a way to group flavors by a name. For now, we're using just a single group. Add the following line in your defaultConfig block: flavorDimensions "default" Now syncing the gradle would give you the following product flavors: Android Build Variants combine build types and product flavors.


1 Answers

From your build.gradle, we may be able to know what is to be done precisely.

I have used productFlavours, combined with flavorDimensions to implement builds which may or may not include jni libraries.

From what i understood, gist of it is: productFlavors enable you to have n variants of x, y... type, adding flavorDimensions would enable you to have n variants of xy type.

Eg. Inside build.gradle,

    flavorDimensions "abi", "version"  //this is what can help you build with/w/o jni libraries

    productFlavors {
        devel {
            flavorDimension "abi" //keep a dimension common with arm, armv7
            applicationId "com.packagename.dev"
        }
        prod {
       flavorDimension "version"
    // this would be your build w/o the ndk support then
            applicationId "com.packageName"
        }
        armv7 {
            ndk {
                flavorDimension "abi"
                abiFilter "armeabi-v7a"
            }
        }
        arm {
            ndk {
                flavorDimension "abi"
                abiFilter "armeabi"
            }
        }

    }    

As you can see, you will have multiple build variants , product flavors depending on flavorDimension.

prod flavor would be a build variant or build type that simply excludes all native libraries

Sources for topics ndk, jniLibs, buildFlavours... :
- Mastering "Product Flavors" on Android
- ndk-with-android-studio
- multi flavor setup

like image 73
Pararth Avatar answered Oct 01 '22 15:10

Pararth