Android Productflavors build all - android

I would like to have multiple android builds, but it isn't building the way I would like.
I am using Android Studio to make my app and build it with the following productFlavors in the gradle file:
android {
productFlavors {
armv7 {
ndk {
abiFilters "armeabi-v7a", "x86"
}
}
}
}
When I have Android Studio build the application, it only builds the x86 version. If I remove the x86 from the flavors, it builds armeabi-v7a. How can I get it to build both versions?

You should probably do this to get both:
android {
productFlavors {
armv7 {
ndk {
abiFilters "armeabi-v7a"
}
}
x86 {
ndk {
abiFilters "x86"
}
}
}
}

Related

Building all ABIs but packaging only a subset

I am attempting to build all ABIs for my project but would only like to package a subset of them into my app. For example, I would like to build "x86", "x86_64", "armeabi-v7a" and "arm64-v8a" but only package "x86" (for example).
Reading this document (https://developer.android.com/studio/projects/gradle-external-native-builds.html#jniLibs) under section "Specify ABIs", it seems very possible using the snippet they provided as an example. However, this does not seem to be working for me.
My code snippet is as follows.
android {
defaultConfig {
ndk {
abiFilters 'armeabi-v7a'
}
externalNativeBuild {
cmake {
abiFilters 'armeabi-v7a', 'x86'
}
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}
In the snippet above, from my understanding of the document, it should build both armeabi-v7a and x86, but only package armeabi-v7a in my APK. This is not working though.
I am using Android plugin 3.1.0 and NDK 16.1.4479499
what you are looking for, is controlled by splits.
splits {
abi {
enable true
reset()
include 'armeabi-v7a'
universalApk false //don't generate an additional APK that contains all the ABIs
}
}

How to specify common gradle properties in root gradle.build script

I have a large number of gradle modules I use to build my android applications and libraries. My application is decomposed into multiple java & C++ libraries. Each module might be a java library, or an APK project. For each module representing an APK, I have a build.gradle which specifies various productFlavors sections that control how to build them. Example:
flavorDimensions "mode"
productFlavors {
arm {
dimension "mode"
externalNativeBuild {
ndk { abiFilters "armeabi-v7a" }
}
}
x86 {
dimension "mode"
externalNativeBuild {
ndk { abiFilters "x86" }
}
}
full {
dimension "mode"
externalNativeBuild {
ndk { abiFilters "x86", "armeabi-v7a" }
}
}
}
What I'm trying to avoid is duplicating the above configuration in each leaf build.gradle that represents an APK output. How can I store the product flavors at the root level and somehow have those transitive to the leave build.gradle files so I do not need to duplicate them?
The support library does something very similar, and you can see their method here (in particular, the SupportLibraryPlugin).
At a high level the strategy is to create a Gradle plugin in your buildSrc directory that you apply to each of your projects. This plugin will apply the common configuration.
Your plugin might look something like this:
[project]/buildSrc/src/main/groovy/com/example/ConfigurationPlugin.groovy
class ConfigurationPlugin implements Plugin<Project> {
#Override
public void apply(Project project) {
LibraryExtension library = project.extensions.findByType(LibraryExtension.class);
library.flavorDimensions "mode"
library.productFlavors {
arm {
dimension "mode"
externalNativeBuild {
ndk { abiFilters "armeabi-v7a" }
}
}
x86 {
dimension "mode"
externalNativeBuild {
ndk { abiFilters "x86" }
}
}
full {
dimension "mode"
externalNativeBuild {
ndk { abiFilters "x86", "armeabi-v7a" }
}
}
}
}
}
[project]/builSrc/build.gradle
apply plugin: 'groovy'
repositories {
google()
}
dependencies {
compile 'com.android.tools.build:gradle:3.0.0-beta7'
}
Library module build.gradle:
apply plugin: 'com.android.library'
apply plugin: ConfigurationPlugin
android {
// ...
}
You can reference another Gradle project within your project using ':project-name', e.g. compile ':pluginproject'. You'll also want to update settings.gradle to include the other project. The project will be built when it is needed to apply the plugin.

How to set CmakeLists path in product flavor for each Android ABI?

I need to have a separate CMakeLists.txt for each Android ABI. I tried to use product flavor to set the path for CMakeLists.txt. But I am getting following error on running ./gradlew assembleDebug or any other gradle command from command line.
Could not find method path() for arguments [CMakeLists.txt] on object
of type
com.android.build.gradle.internal.dsl.ExternalNativeCmakeOptions.
Here is how I have set product flavor in build.gradle.
productFlavors {
arm64_v8a {
ndk {
abiFilters "arm64-v8a"
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}
x86_64 {
ndk {
abiFilters "x86_64"
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}
}
NOTE - I had initially named the files as "CMakeLists_arm64-v8a.txt" and "CMakeLists_x86_64.txt". But that was failing so tried same name.
How to fix this or is there a workaround for this?
No, you cannot have CMakeLists.txt paths different for different flavors and/or ABIs, but you can use the arguments to add conditionals in your cmake script, for example like this:
flavorDimensions "abi"
productFlavors {
arm64_v8a {
dimension "abi"
ndk {
abiFilters "arm64-v8a"
}
externalNativeBuild {
cmake {
arguments "-DFLAVOR=ARM"
}
}
}
x86_64 {
dimension "abi"
ndk {
abiFilters "x86_64"
}
externalNativeBuild {
cmake {
arguments "-DFLAVOR=x86"
}
}
}
}
Now you can check this in your CMakeLists.txt:
if (FLAVOR STREQUAL 'ARM')
include(arm.cmake)
endif()
But in your case, you can rely on the argument that is defined by Android Studio, and don't need your own parameter:
if (ANDROID_ABI STREQUAL 'arm64-v8a')
include(arm.cmake)
endif()
Actually, you probably don't need separate productFlavors at all, but rather use splits to produce thin APKs for each ABI.

How to setup Android library module with productflavors and jni

I'm trying to include mupdf as an android library module with precompiled .so binaries into my project.
I have an application module which includes my library module like so.
dependencies {
compile project(':mupdf')
}
In my mupdf library module I have my compiled binaries like so.
mupdf
- jniLibs
- arm64-v8a
libmupdf.so
- armeabi
libmupdf.so
- armeabi-v7a
libmupdf.so
- x86
libmupdf.so
- x86_64
libmupdf.so
My build.gradle for the mupdf library project:
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
publishNonDefault true
productFlavors {
x86 {
ndk {
abiFilter "x86"
}
}
x64 {
ndk {
abiFilter "x86_64"
}
}
arm {
ndk {
abiFilter "armeabi"
}
}
arm_v7a {
ndk {
abiFilter "armeabi-v7a"
}
}
arm64_v8a {
ndk {
abiFilter "arm64-v8a"
}
}
fat
}
}
dependencies{
compile project(':core')
}
The problem is soon as I add the product flavors my app doesn't compile because it doesn't find any of the java files from the mupdf module.
If I delete the product flavors part from the gradle file it compiles. But it crashes at runtime because it can't resolve/load the libmupdf library.
You can do this without product flavors. Just remove them and call System.loadLibrary("mupdf") on startup/at some point before the library is used. This call will figure out which native lib to use for the current devices architecture.
Indeed I have to build a fat module for the .aar library and apply the productflavors in my app.gradle.
I was missing this in my library's build.gradle
sourceSets.main {
jni.srcDirs = [] // This prevents the auto generation of Android.mk
jniLibs.srcDir 'src/main/jni'
}
Without this no methods were able to be resolved.

Exclude abi from apk

In my application I use renderscript which has native code for x86, armeabi-v7a and mips (~2.7Mb each). Also I read that the mips architecture has just a few devices. So I'd like to bundle my application in two APKs: universal (eg. x86 and armeabi-v7a) and mips. I found that split section helps to create apk for mips, but universal apk still contains mips architecture. So my question is how to exclude abi from result apk?
Thanks
You can try setting up another flavor that contains everything but MIPS. In the build.gradle file from one of the test projects that are part of the Android Gradle plugin sources, I found this:
apply from: "../commonHeader.gradle"
buildscript { apply from: "../commonBuildScript.gradle", to: buildscript }
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion = rootProject.ext.buildToolsVersion
productFlavors {
x86 {
ndk {
abiFilter "x86"
}
}
arm {
ndk {
abiFilters "armeabi-v7a", "armeabi"
}
}
mips {
ndk {
abiFilter "mips"
}
}
}
}
It looks like their arm flavor basically includes the two common ARM ABIs. You could probably define an "universal" flavor containing x86 and armeabi-v7a.
They have another test project, whose build.gradle contains:
splits {
abi {
enable true
reset()
include 'x86', 'armeabi-v7a', 'mips'
}
}
You might be able to use something similar, and drop the mips from there.

Categories

Resources