如何在构建类型中覆盖默认配置的 abiFilters?

6
abiFilters 是在安卓 build.gradle 文件的 defaultConfig 块中设置的。
我想在发布版本(release buildType)中排除 x86,但是找不到简单的方法来实现。
这是 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"
        }
    }
}

这是我得到的内容:
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

我真正想要的是:

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

我希望在defaultConfig块中保留完整的abiFilters,并在特定的buildType中指定它们。
编辑1:
是的,删除defaultConfig并在debugrelease块中设置abiFilters会起作用。但我的问题是如何利用defaultConfig

回答了自己的问题,但愿意看看是否有更好的解决方案。仍然不知道如何完全覆盖默认配置中的abiFilters。 - Felix.D
3个回答

6
给命令行加一个选项,例如 "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

参考: https://dev59.com/P6_la4cB1Zd3GeqPrVVo#52980193

此链接提供了关于在Java中使用try-with-resources语句的信息。该语句可确保资源(例如文件或套接字)在使用后正确关闭,并且可以消除繁琐的异常处理代码。使用此语句时,只需在try语句之后列出要使用的资源即可。在代码块结束时,将自动关闭它们。

1
做得好的变通方法。在build.gradle文件中覆盖default/flavor/buildType属性本身是很烦人的,但对于外部调用者来说却很简单。 - sakiM

2
android {
    buildTypes {
        debug {
            ndk {
                abiFilters "armeabi", "x86"
            }
        }
        release {
            ndk {
                abiFilters "armeabi"
            }
        }
    }
}

productFlavors还支持维度abi


这样做是可行的,但我想在defaultConfig中保持默认的abiFilters。只需要调整一些buildTypes或flavors即可。 - Felix.D
1
@Felix.D 当仅配置构建类型为“release”时,可能需要进行“reset()”。这至少适用于“splits”,其中甚至可以构建两个单独的包。正如问题所描述的那样,它听起来像是“abiFilters”只会添加过滤器,而不是按预期覆盖它们。 - Martin Zeitler
这是完全相反的方法,但当defaultConfig只配置了armeabi,而构建类型为debug时添加x86,这可能会起作用。 - Martin Zeitler
谢谢,你的评论帮我找到了解决方案。 - Felix.D

2

感谢Martin,我从这里找到了可行的解决方案:

当defaultConfig只配置了armeabi,并且构建类型为debug时添加x86,则可能会起作用

我意识到将想要的abi添加到debug中为了删除发布版中不需要的abi而采取的解决方法

对我有效

defaultConfig {
    ndk {
      //abiFilters "armeabi", "x86"
        abiFilters "armeabi"
    }
}

buildTypes {
    debug {
        ndk {
          //abiFilters "armeabi", "x86"
            abiFilters "x86"
        }
    }
    release {
        //ndk {
        //    abiFilters "armeabi"
        //}
   }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接