pubspec.yaml
的版本代码为11。
使用
flutter build apk
构建胖 apk 会按预期提供 versionCode
11 的 apk。但是构建一个 armeabi-v7a-release
apk 会得到一个 versionCode
1011 而不是 11 的 apk。
我想将armeabi-v7a的versionCode递增1000的行为更改为android的文档中提到的(感谢@Varun Kumar指出它)。我希望
armeabi-v7a
apk 的版本代码与 pubspec.yaml
中提到的版本代码相对应。如何编辑 this build.gradle 来实现相同的效果?
发现可以通过向
versionCode
添加代码来覆盖输出 .apk 文件的 build.gradle(:app)
,如 android 文档中所述,这允许我们显式修改为每个 abi 分配 versionCode
所遵循的逻辑。
为了避免单独对armeabi-v7a进行1000增量,我将内部if块的逻辑修改为以下内容:
if (baseAbiVersionCode != null ) {
output.versionCodeOverride =
baseAbiVersionCode * 1000 + variant.versionCode
if (baseAbiVersionCode == 1) { // Matches armeabi-v7a in ext.abiCodes
// Uses the default versionCode for armeabi-v7a APKs.
output.versionCodeOverride = variant.versionCode
}
}
注意:这部分有点hacky,因为您最终可能会为 fat apk 和armeabi-v7a ABI 提供相同的
versionCode
。我建议仅当您只想专门针对一个 ABI 时才使用此选项。
根据android文档 像这样更改 app/build.gradle :
android {
...
}
...
// -- > add these lines :
// For each APK output variant, override versionCode with a combination of
// ext.abiCodes * 1000 + variant.versionCode. In this example, variant.versionCode
// is equal to defaultConfig.versionCode. If you configure product flavors that
// define their own versionCode, variant.versionCode uses that value instead.
android.applicationVariants.all { variant ->
// Assigns a different version code for each output APK
// other than the universal APK.
variant.outputs.each { output ->
// Stores the value of ext.abiCodes that is associated with the ABI for this variant.
def baseAbiVersionCode =
// Determines the ABI for this variant and returns the mapped value.
project.ext.abiCodes.get(output.getFilter(com.android.build.OutputFile.ABI))
// Because abiCodes.get() returns null for ABIs that are not mapped by ext.abiCodes,
// the following code doesn't override the version code for universal APKs.
// However, because you want universal APKs to have the lowest version code,
// this outcome is desirable.
if (baseAbiVersionCode != null) {
// Assigns the new version code to versionCodeOverride, which changes the
// version code for only the output APK, not for the variant itself. Skipping
// this step causes Gradle to use the value of variant.versionCode for the APK.
output.versionCodeOverride = variant.versionCode
}
}
}
...
flutter {
...
}
我改变了:
if (baseAbiVersionCode != null) {
// Assigns the new version code to versionCodeOverride, which changes the
// version code for only the output APK, not for the variant itself. Skipping
// this step causes Gradle to use the value of variant.versionCode for the APK.
output.versionCodeOverride =
baseAbiVersionCode * 1000 + variant.versionCode
}
至:
if (baseAbiVersionCode != null) {
// Assigns the new version code to versionCodeOverride, which changes the
// version code for only the output APK, not for the variant itself. Skipping
// this step causes Gradle to use the value of variant.versionCode for the APK.
output.versionCodeOverride = variant.versionCode
}