Android gradle apk拆分,设置versionCode

时间:2016-03-29 13:55:05

标签: android android-gradle

我正在尝试将应用程序拆分为abi特定的apks,但所有apks的版本号都是相同的。他们需要不同才能上传到游戏商店。我错过了什么吗?

splits {
    abi {
        enable true
        reset()
        include 'x86', 'armeabi', 'armeabi-v7a', 'arm64-v8a'
        universalApk true
    }
}

3 个答案:

答案 0 :(得分:5)

我想如果你想做一个快速的谷歌搜索,你会得到所有的答案。无论如何,这就是你的gradle应该是这样的。您错过了为不同的体系结构构建分配标识符。

    android 
    {

    def versionMajor = 1
        def versionMinor = 0
        def versionPatch = 0
        def versionBuild = 0

        defaultConfig {
            applicationId "com.example.app"
            minSdkVersion 15
            targetSdkVersion 22

            versionCode versionMajor * 10000 + versionMinor * 1000 + versionPatch * 100 + versionBuild
            versionName "${versionMajor}.${versionMinor}.${versionPatch}"
        }

 splits {
        abi {
            enable true
            reset()
            include 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a'
            universalApk true
        }
    }    

    // map for the version code
        project.ext.versionCodes = ['armeabi': 1, 'armeabi-v7a': 2, 'arm64-v8a': 3, 'mips': 5, 'mips64': 6, 'x86': 8, 'x86_64': 9]

        android.applicationVariants.all { variant ->
            // assign different version code for each output
            variant.outputs.each { output ->
                output.versionCodeOverride =
                        project.ext.versionCodes.get(output.getFilter(com.android.build.OutputFile.ABI), 0) * 1000000 + android.defaultConfig.versionCode
            }
        }
    }

答案 1 :(得分:4)

我认为这可能会更清洁一些。它虽然可能不是100%自动化,但我不喜欢* 1000000和字典查找技巧:

ext {
    versionCodeBase = 30 // 1 higher than current highest versionCode in Play store
}

android {
    splits {
        density {
            enable true // just enable them all
        }
        abi {
            enable true
            universalApk false
        }
    }

    android.applicationVariants.all { variant ->
        variant.outputs.eachWithIndex { output, outputIndex ->
            output.versionCodeOverride = project.ext.versionCodeBase + outputIndex
            println variant.buildType.name + ' - ' + output.getFilter(OutputFile.ABI) + ' - ' + output.getFilter(OutputFile.DENSITY) + ': ' + output.versionCodeOverride
        }
    }
}

注意我使用eachWithIndex循环变体。

这是有效的,因为密度和abi不会在用户的设备上发生变化。如果您要按minSdkVersion允许单独的apk文件,则需要在此脚本中完成更多工作。

Lemme也只是添加了脚本的输出:

debug - mips64 - null: 30
debug - x86_64 - null: 31
debug - x86 - null: 32
debug - armeabi-v7a - null: 33
debug - armeabi - null: 34
debug - mips - null: 35
debug - arm64-v8a - null: 36
debug - mips64 - xxxhdpi: 37
debug - x86_64 - xxxhdpi: 38
debug - x86 - xxxhdpi: 39
debug - armeabi-v7a - xxxhdpi: 40
debug - armeabi - xxxhdpi: 41
debug - mips - xxxhdpi: 42
debug - arm64-v8a - xxxhdpi: 43
debug - mips64 - mdpi: 44
debug - x86_64 - mdpi: 45
debug - x86 - mdpi: 46
debug - armeabi-v7a - mdpi: 47
debug - armeabi - mdpi: 48
debug - mips - mdpi: 49
debug - arm64-v8a - mdpi: 50
debug - mips64 - ldpi: 51
debug - x86_64 - ldpi: 52
debug - x86 - ldpi: 53
debug - armeabi-v7a - ldpi: 54
debug - armeabi - ldpi: 55
debug - mips - ldpi: 56
debug - arm64-v8a - ldpi: 57
debug - mips64 - xxhdpi: 58
debug - x86_64 - xxhdpi: 59
debug - x86 - xxhdpi: 60
debug - armeabi-v7a - xxhdpi: 61
debug - armeabi - xxhdpi: 62
debug - mips - xxhdpi: 63
debug - arm64-v8a - xxhdpi: 64
debug - mips64 - hdpi: 65
debug - x86_64 - hdpi: 66
debug - x86 - hdpi: 67
debug - armeabi-v7a - hdpi: 68
debug - armeabi - hdpi: 69
debug - mips - hdpi: 70
debug - arm64-v8a - hdpi: 71
debug - mips64 - xhdpi: 72
debug - x86_64 - xhdpi: 73
debug - x86 - xhdpi: 74
debug - armeabi-v7a - xhdpi: 75
debug - armeabi - xhdpi: 76
debug - mips - xhdpi: 77
debug - arm64-v8a - xhdpi: 78
release - mips64 - null: 30
release - x86_64 - null: 31
release - x86 - null: 32
release - armeabi-v7a - null: 33
release - armeabi - null: 34
release - mips - null: 35
release - arm64-v8a - null: 36
release - mips64 - xxxhdpi: 37
release - x86_64 - xxxhdpi: 38
release - x86 - xxxhdpi: 39
release - armeabi-v7a - xxxhdpi: 40
release - armeabi - xxxhdpi: 41
release - mips - xxxhdpi: 42
release - arm64-v8a - xxxhdpi: 43
release - mips64 - mdpi: 44
release - x86_64 - mdpi: 45
release - x86 - mdpi: 46
release - armeabi-v7a - mdpi: 47
release - armeabi - mdpi: 48
release - mips - mdpi: 49
release - arm64-v8a - mdpi: 50
release - mips64 - ldpi: 51
release - x86_64 - ldpi: 52
release - x86 - ldpi: 53
release - armeabi-v7a - ldpi: 54
release - armeabi - ldpi: 55
release - mips - ldpi: 56
release - arm64-v8a - ldpi: 57
release - mips64 - xxhdpi: 58
release - x86_64 - xxhdpi: 59
release - x86 - xxhdpi: 60
release - armeabi-v7a - xxhdpi: 61
release - armeabi - xxhdpi: 62
release - mips - xxhdpi: 63
release - arm64-v8a - xxhdpi: 64
release - mips64 - hdpi: 65
release - x86_64 - hdpi: 66
release - x86 - hdpi: 67
release - armeabi-v7a - hdpi: 68
release - armeabi - hdpi: 69
release - mips - hdpi: 70
release - arm64-v8a - hdpi: 71
release - mips64 - xhdpi: 72
release - x86_64 - xhdpi: 73
release - x86 - xhdpi: 74
release - armeabi-v7a - xhdpi: 75
release - armeabi - xhdpi: 76
release - mips - xhdpi: 77
release - arm64-v8a - xhdpi: 78

答案 2 :(得分:3)

出于某种原因,在使用提供的解决方案时我总是遇到错误。我有多个productFlavors,我的解决方案现在看起来像这样:

android {
    flavorDimensions 'app', 'store'
    productFlavors {
        appFlavorOne {
            dimension 'app'
            applicationId "com.my.application.id"
            versionCode 123
            versionName '1.2.3'
        }
        appFlavorTwo {
            dimension 'app'
            applicationId "com.my.other.application.id"
            versionCode 100
            versionName '1.0.0'
        }
    }
    splits {
        abi {
            enable true
            reset()
            include 'armeabi', 'armeabi-v7a', 'arm64-v8a', 'x86'
            universalApk true
        }
    }

    // APK Splits based on ABI, map for the version code. Uncomment for appFlavorTwo Builds!
    project.ext.versionCodes = ['null':0, 'armeabi': 1, 'armeabi-v7a': 2, 'arm64-v8a': 3, 'x86': 4]
    android.applicationVariants.all { variant ->
        // assign different version code for each output
        variant.outputs.each { output ->
            output.versionCodeOverride = android.productFlavors.appFlavorOne.versionCode + project.ext.versionCodes.get(output.getFilter(com.android.build.OutputFile.ABI), 0)
            output.versionNameOverride = android.productFlavors.appFlavorOne.versionName + ' (' + (project.ext.versionCodes.get(output.getFilter(com.android.build.OutputFile.ABI), 0) == 0 ? 'universal' : output.getFilter(com.android.build.OutputFile.ABI)) + ')'
            println output.versionNameOverride + ' (' + output.versionCodeOverride + ')'
        }
    }
}