UPDATE - since the time of this posting, there have been much progress in the gradle build process, thus this answer might not be the recommended best practice and new changes might even brake it. Use your own discretion.
To do that, first, you must put the native libraries in the following folder hierarchy separately
lib
-armeabi
-arm.so
-*.so
-
lib
-x86
-x86.so
-*.so
then zip the lib(without 's') folders (e.g. arm.zip and x86.zip) and rename the 'zip' extension to 'jar' (e.g. arm.jar and x86.jar). Put these jars in a appropriate folders (e.g. armeabi/libs and x86/libs). Now we are going to include the dependencies for each flavor. But we cannot use "compile file '....'". We have to use "flavorCompile file '...'"
e.g.
flavorGroups 'abi'
productFlavors {
arm {
flavorGroup 'abi'
dependencies {
armCompile files('arm/libs/armeabi.jar')
}
}
x86 {
flavorGroup 'abi'
dependencies {
x86Compile files('x86/libs/x86.jar')
}
}
}
====
Here is more complex environment. You not only have processor architecture variants but you also have debug libraries(.jar,.so) for the processors. The example here has as Debug.jar for Arm debug and NonDebug.jar for Arm release; and *.so for both Arm and X86. Such configuration can be achieved by using gradle ExtraPropertiesExtension Please read my SO answer here, https://mcmap.net/q/411126/-build-variants-in-gradle-for-a-library-project-in-android , to understand how the debug folders can be structured.
android {
compileSdkVersion 18
buildToolsVersion "19.0.0"
final DEBUG_ROOT = "build-types/debug"
final RELEASE_ROOT = "build-types/release"
project.ext.set("projRoot", "")
buildTypes {
debug {
project.projRoot = DEBUG_ROOT
dependencies {
debugCompile files(DEBUG_ROOT+"/libs/Debug.jar")
}
}
release {
project.projRoot = RELEASE_ROOT
dependencies {
releaseCompile files(RELEASE_ROOT+"/libs/NonDebug.jar")
}
runProguard true
proguardFile 'proguard.cfg'
}
}
sourceSets {
final PROJ_ROOT = project.ext.get("projRoot")
final BUILD_TYPE_RES = PROJ_ROOT + "/res"
main {
manifest.srcFile 'src/main/AndroidManifest.xml'
java.srcDirs = ['src/main/java']
//resources.srcDirs = ['src/main']
//aidl.srcDirs = ['src/main']
//renderscript.srcDirs = ['src/main']
res.srcDirs = ['src/main/res',BUILD_TYPE_RES]
assets.srcDirs = ['src/main/assets']
}
flavorGroups 'abi'
productFlavors {
arm {
flavorGroup 'abi'
final ARM_LIB_PATH = PROJ_ROOT + "/arm/libs/armeabi.jar"
dependencies {
armCompile files(ARM_LIB_PATH)
}
}
x86 {
flavorGroup 'abi'
final X86_LIB_PATH = PROJ_ROOT + "/x86/libs/x86.jar"
dependencies {
x86Compile files(X86_LIB_PATH)
}
}
}
// Move the tests to tests/java, tests/res, etc...
instrumentTest.setRoot('tests')
// Move the build types to build-types/<type>
// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
// This moves them out of them default location under src/<type>/... which would
// conflict with src/ being used by the main source set.
// Adding new build types or product flavors should be accompanied
// by a similar customization.
debug.setRoot(DEBUG_ROOT)
release.setRoot(RELEASE_ROOT)
}
}