谷歌市場應用的多apk發佈(減小apk的大小)

在谷歌市場發佈應用的時候,有時候由於apk太大,需要分abi或者screen,對於不同的機種,打不同的apk包,來減少apk的大小


而谷歌市場的發佈,支持如下種類的多apk發佈:

  • Support different OpenGL texture compression formats with each APK.
  • Support different screen sizes and densities with each APK.
  • Support different device feature sets with each APK.
  • Support different platform versions with each APK.
  • Support different CPU architectures with each APK (such as for ARM, x86, and MIPS, when your app uses the Android NDK).
參考 https://developer.android.com/google/play/publishing/multiple-apks.html#HowItWorks

對於一個應用,要支持多apk,需要apk的簽名和app id一致,而Version code必須不同

用AS,可以通過如下gradle代碼達到這個目的,參考 https://developer.android.com/studio/build/configure-apk-splits.html


多apk abi示例:


apply plugin: 'com.android.application'

android {
    compileSdkVersion xx
    buildToolsVersion "xxx"
    defaultConfig {
        //......
    }

    buildTypes {
        //...
    }
    splits {
        abi {
            enable true
            reset()
            include 'armeabi', 'armeabi-v7a','arm64-v8a','x86_64','mips'
            
           // universalApk true
        }
    }
}

接下來一段是對打出來的多個apk包,設置不同的versionCode,基本是原文照抄

ext.abiCodes = ['armeabi':1, 'armeabi-v7a':2, 'arm64-v8a':3,'mips64':4,'mips':5,'x86':6,'x86_64':7]

import com.android.build.OutputFile

//https://developer.android.com/studio/build/configure-apk-splits.html
//
// 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(OutputFile.ABI))

        // Because abiCodes.get() returns null for ABIs that are not mapped by ext.abiCodes,
        // the following code does not override the version code for universal APKs.
        // However, because we 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 simply
            // causes Gradle to use the value of variant.versionCode for the APK.
            output.versionCodeOverride =
                    baseAbiVersionCode * 1000 + variant.versionCode
        }
    }
}

對於打出來的多個apk包,假設基本的versionCode爲1,則他們的versionCode爲1001,2001,3001,4001等等


接下來,可以對這多個同一應用的多個apk,發佈到google市場了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章