【问题标题】:How to override defaultConfig abiFilters in buildTypes如何在 buildTypes 中覆盖 defaultConfig abiFilters
【发布时间】:2018-11-15 14:56:08
【问题描述】:

abiFilters 在 android build.gradle defaultConfig 块中设置。

我想从发布 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

我想在defautlConfig 块中保留一个完整的abiFilters

并在某些buildType中指定一个


编辑 1:

是的,删除 defaultConfig 并在 debugrelease 块中设置 abiFilters 会起作用。但我的问题是如何使用defaultConfig

【问题讨论】:

  • 回答了我自己的问题,但愿意看看是否有更好的解决方案。仍然不知道如何从 defaultConfig 中完全覆盖 abiFilters

标签: android gradle android-gradle-plugin


【解决方案1】:

提供命令行选项,例如“no_x86

  1. 在下面添加到您的app/build.gradle

    defaultConfig {
        ndk {
    
            ...
            if (project.hasProperty("no_x86")) {
                abiFilters "armeabi"
            } else {
                abiFilters "armeabi", "x86"
            }
    
            ...
        }
    }
    
  2. 通过将选项 no_x86 提供给命令,使用以下命令生成不带 x86 ABI 的 APK。

    ./gradlew assemble -Pno_x86
    

    但如果您想使用 x86 abi 构建APK,请不要将选项no_x86 提供给命令。因为defaultConfig 是根据您的要求保留完整的 abiFilters。

    对于某些buildType,您可以通过输入或不输入-Pno_x86 属性来调用相应的构建命令。例如。 ./gradlew assembleRelease -Pno_x86

参考:https://stackoverflow.com/a/52980193/8034839

【讨论】:

  • 做得好的解决方法。 build.gradle 中的覆盖 default/flavor/buildType 属性本身很烦人,但对于外部调用者来说很简单。
【解决方案2】:
android {
    buildTypes {
        debug {
            ndk {
                abiFilters "armeabi", "x86"
            }
        }
        release {
            ndk {
                abiFilters "armeabi"
            }
        }
    }
}

productFlavors 也将支持维度abi

【讨论】:

  • 这可行,但我想在 defautConfig 中维护一个默认的 abiFilters。并且只调整一些 buildTypes 或风味
  • @Felix.D 仅配置构建类型release 时,可能需要reset()。这至少适用于splits,甚至可以构建两个单独的包。正如所描述的问题,听起来好像abiFilters 只会添加过滤器,但不会像预期的那样覆盖它们。
  • 这是完全相反的方法,但是当defaultConfig 只配置armeabi 并且构建类型debug 然后添加x86,这可能会奏效。
  • 谢谢,您的评论让我找到了解决方案
【解决方案3】:

感谢 Martin,我从中找到了一个可行的解决方案:

when defaultConfig would only have armeabi configured and build-type debug would then add x86, this might work out

我意识到adding wanted abi to debugremoving unwanted abi for release 的解决方法

为我工作

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

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

【讨论】:

    猜你喜欢
    • 2013-07-11
    • 1970-01-01
    • 2012-12-17
    • 2020-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-26
    • 2011-03-18
    相关资源
    最近更新 更多