androidgradleandroid-gradle-plugin

How to override defaultConfig abiFilters in buildTypes


abiFilters is set in android build.gradle defaultConfig block.

I'd like to exclude x86 from release buildType, but can't find an easy way to do it

Here is the build.gradle:

defaultConfig {
    ndk {
        abiFilters "armeabi", "x86"
        moduleName "cipher_v1"
        cFlags "-DRELEASE=1"
        if (rootProject.ext.has("testCrack")) {
            cFlags += " -DTEST_CRACK"
        }
        if (project.ext.has("authKey") && project.ext.has("androidId")) {
            cFlags += "-DAUTH_KEY=\\\"" + project.ext.authKey + "\\\""
            "-DANDROID_ID=\\\"" + project.ext.androidId + "\\\""
        }
    }
}

buildTypes {
   release {
        ndk {
            abiFilters "armeabi"
        }
    }
}

Here is what I get:

unzip -l base-release.aar|grep cipher
17752  02-01-1980 00:00   jni/armeabi/libcipher_v1.so
17640  02-01-1980 00:00   jni/x86/libcipher_v1.so

Here is what I really want:

unzip -l base-release.aar|grep cipher
17752  02-01-1980 00:00   jni/armeabi/libcipher_v1.so

I'd like to keep a full abiFilters in the defautlConfig block

And specify ones in certain buildType


EDIT 1:

Yes, removing the defaultConfig and setting abiFilters in both debug & release block would work. But my question is how to utilize the defaultConfig


Solution

  • Feeding a command line option, e.g. "no_x86"

    1. Add below to your app/build.gradle

      defaultConfig {
          ndk {
      
              ...
              if (project.hasProperty("no_x86")) {
                  abiFilters "armeabi"
              } else {
                  abiFilters "armeabi", "x86"
              }
      
              ...
          }
      }
      
    2. Use below command to generate the APKs without x86 ABI by feeding option no_x86 to the command.

      ./gradlew assemble -Pno_x86
      

      but don't feed option no_x86 to the command if you want to build APKs with x86 abi. As the defaultConfig is to keep a full abiFilters per your requirement.

      For certain buildType, you can invoke the corresponding build command by feeding or not feeding the -Pno_x86 property. E.g. ./gradlew assembleRelease -Pno_x86

    Reference: https://stackoverflow.com/a/52980193/8034839