用于发布APK每个架构的Android NDK版本代码方案

时间:2016-05-16 17:41:04

标签: android android-ndk versioning

我正在使用带有NDK的Android Studio 2.1。但我遇到了有关如何配置versionCode的问题。来自Stackoverflow的this androidbycode博客链接和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
    }

或者,正如上面提到的博客链接所示,我的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
        }
    }

1 个答案:

答案 0 :(得分:1)

您可以按照与您遇到的问题相对应的此错误:https://code.google.com/p/android/issues/detail?id=192943

我提供了一个解决方案,当您使用口味时仍然适用于您 - 定义 versionCodeBase 并使用它来生成每种风格的版本代码:

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
        }
    }
}