Android 用于按架构发布 APK 的 NDK 版本代码方案
Android NDK version code scheme for publishing APK per architecture
我正在使用 Android Studio 2.1 和 NDK。但是我面临着如何为 versionCode
配置的问题。根据 Whosebug 的 this androidbycode blog link and this 回答,我尝试编辑我原来的 bulid.gradle,但它显示同步错误。
Build.gradle(在没有版本合并的情况下工作正常)
apply plugin: 'com.android.model.application'
model {
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.mytestapp"
minSdkVersion.apiLevel 15
targetSdkVersion.apiLevel 23
versionCode 101
versionName "1.0.1"
}
buildTypes {
release {
minifyEnabled true
proguardFiles.add(file("proguard-rules.pro"))
signingConfig = $("android.signingConfigs.myConfig")
}
debug {
debuggable true
}
}
productFlavors {
create("armeabi") {
ndk {
abiFilters.add("armeabi")
versionCode 1
}
}
create("armeabi-v7a") {
ndk {
abiFilters.add("armeabi-v7a")
versionCode 3
}
}
create("x86") {
ndk {
abiFilters.add("x86")
versionCode 6
}
}
create("fat") {
ndk {
abiFilters.add("armeabi")
abiFilters.add("armeabi-v7a")
abiFilters.add("x86")
versionCode 0
}
}
}
}
// You can modify the NDK configuration for each variant.
components.android {
binaries.afterEach { binary ->
binary.mergedNdkConfig.cppFlags.add(
"-DVARIANT=\"" + binary.name + "\"")
}
}
android.signingConfigs {
create("myConfig") {
storeFile "MyKeyStoreLocation"
storePassword "MyStorePassword"
keyAlias "mykeyAlias"
keyPassword "mykeyPassword"
storeType "jks"
}
}
android.ndk {
moduleName = "settings-jni"
}
}
repositories {
mavenCentral()
maven {
url "http://dl.bintray.com/lukaville/maven"
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:design:23.4.0'
compile 'com.google.android.gms:play-services-ads:8.4.0'
compile 'net.zetetic:android-database-sqlcipher:3.4.0@aar'
compile 'com.nbsp:library:1.08'
compile 'com.android.support:recyclerview-v7:23.4.0'
compile 'com.android.support:cardview-v7:23.4.0'
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.mcxiaoke.volley:library:1.+'
compile 'com.daimajia.numberprogressbar:library:1.2@aar'
compile 'com.github.paolorotolo:appintro:3.4.0'
}
我找不到合并 ABI 版本控制代码片段的方法, 如上述 SO 答案中所建议
applicationVariants.all { variant ->
// get the version code of each flavor
def abiVersion = variant.productFlavors.get(0).versionCode
// set the composite code
variant.mergedFlavor.versionCode = abiVersion * 100000 + defaultConfig.versionCode
}
或者,按照上述博客 link 中的建议,使用我的 build.gradle
-
project.ext.versionCodes = ['armeabi': 1, 'armeabi-v7a': 2, 'arm64-v8a': 3, 'mips': 5, 'mips64': 6, 'x86': 8, 'x86_64': 9]
android.applicationVariants.all { variant ->
variant.outputs.each { output ->
output.versionCodeOverride =
project.ext.versionCodes.get(output.getFilter(
com.android.build.OutputFile.ABI), 0) * 10000000 + android.defaultConfig.versionCode
}
}
您可以关注与您遇到的问题相对应的错误:https://code.google.com/p/android/issues/detail?id=192943
我提供了一个解决方案,在您使用 flavors 时仍然适用于您 -
定义一个 versionCodeBase 并用它来为每个 flavor 生成版本代码:
model {
def versionCodeBase = 11;
def versionCodePrefixes = ['armeabi': 1, 'armeabi-v7a': 2, 'arm64-v8a': 3, 'mips': 5, 'mips64': 6, 'x86': 8, 'x86_64': 9];
//...
android.productFlavors {
create ("armv7") {
ndk.abiFilters.add("armeabi-v7a")
versionCode = versionCodePrefixes.get("armeabi-v7a", 0) * 1000000 + versionCodeBase
}
create ("x86") {
ndk.abiFilters.add("x86")
versionCode = versionCodePrefixes.get("x86", 0) * 1000000 + versionCodeBase
}
}
}
我正在使用 Android Studio 2.1 和 NDK。但是我面临着如何为 versionCode
配置的问题。根据 Whosebug 的 this androidbycode blog link and this 回答,我尝试编辑我原来的 bulid.gradle,但它显示同步错误。
Build.gradle(在没有版本合并的情况下工作正常)
apply plugin: 'com.android.model.application'
model {
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.mytestapp"
minSdkVersion.apiLevel 15
targetSdkVersion.apiLevel 23
versionCode 101
versionName "1.0.1"
}
buildTypes {
release {
minifyEnabled true
proguardFiles.add(file("proguard-rules.pro"))
signingConfig = $("android.signingConfigs.myConfig")
}
debug {
debuggable true
}
}
productFlavors {
create("armeabi") {
ndk {
abiFilters.add("armeabi")
versionCode 1
}
}
create("armeabi-v7a") {
ndk {
abiFilters.add("armeabi-v7a")
versionCode 3
}
}
create("x86") {
ndk {
abiFilters.add("x86")
versionCode 6
}
}
create("fat") {
ndk {
abiFilters.add("armeabi")
abiFilters.add("armeabi-v7a")
abiFilters.add("x86")
versionCode 0
}
}
}
}
// You can modify the NDK configuration for each variant.
components.android {
binaries.afterEach { binary ->
binary.mergedNdkConfig.cppFlags.add(
"-DVARIANT=\"" + binary.name + "\"")
}
}
android.signingConfigs {
create("myConfig") {
storeFile "MyKeyStoreLocation"
storePassword "MyStorePassword"
keyAlias "mykeyAlias"
keyPassword "mykeyPassword"
storeType "jks"
}
}
android.ndk {
moduleName = "settings-jni"
}
}
repositories {
mavenCentral()
maven {
url "http://dl.bintray.com/lukaville/maven"
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:design:23.4.0'
compile 'com.google.android.gms:play-services-ads:8.4.0'
compile 'net.zetetic:android-database-sqlcipher:3.4.0@aar'
compile 'com.nbsp:library:1.08'
compile 'com.android.support:recyclerview-v7:23.4.0'
compile 'com.android.support:cardview-v7:23.4.0'
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.mcxiaoke.volley:library:1.+'
compile 'com.daimajia.numberprogressbar:library:1.2@aar'
compile 'com.github.paolorotolo:appintro:3.4.0'
}
我找不到合并 ABI 版本控制代码片段的方法, 如上述 SO 答案中所建议
applicationVariants.all { variant ->
// get the version code of each flavor
def abiVersion = variant.productFlavors.get(0).versionCode
// set the composite code
variant.mergedFlavor.versionCode = abiVersion * 100000 + defaultConfig.versionCode
}
或者,按照上述博客 link 中的建议,使用我的 build.gradle
-
project.ext.versionCodes = ['armeabi': 1, 'armeabi-v7a': 2, 'arm64-v8a': 3, 'mips': 5, 'mips64': 6, 'x86': 8, 'x86_64': 9]
android.applicationVariants.all { variant ->
variant.outputs.each { output ->
output.versionCodeOverride =
project.ext.versionCodes.get(output.getFilter(
com.android.build.OutputFile.ABI), 0) * 10000000 + android.defaultConfig.versionCode
}
}
您可以关注与您遇到的问题相对应的错误:https://code.google.com/p/android/issues/detail?id=192943
我提供了一个解决方案,在您使用 flavors 时仍然适用于您 - 定义一个 versionCodeBase 并用它来为每个 flavor 生成版本代码:
model {
def versionCodeBase = 11;
def versionCodePrefixes = ['armeabi': 1, 'armeabi-v7a': 2, 'arm64-v8a': 3, 'mips': 5, 'mips64': 6, 'x86': 8, 'x86_64': 9];
//...
android.productFlavors {
create ("armv7") {
ndk.abiFilters.add("armeabi-v7a")
versionCode = versionCodePrefixes.get("armeabi-v7a", 0) * 1000000 + versionCodeBase
}
create ("x86") {
ndk.abiFilters.add("x86")
versionCode = versionCodePrefixes.get("x86", 0) * 1000000 + versionCodeBase
}
}
}