android - 如何为具有不同 cpu 架构的相同 apk 文件创建不同的版本号

标签 android performance gradle apk

为了减少 Apk 大小,我需要为每个 CPU 架构创建不同的 apk。这是我正在使用的方法;

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

这将创建 2 个 apk 文件,两者的大小都小于 universalApk .所以这解决了我的 apk 大小问题。

现在的问题是如何将它们都上传到 Play store .

This表示两者的版本代码apk's应该不同。

在将更新的 apk 添加到 playstore 之前,我打开应用程序级别build.gradle并更新 versionCode属性;
    defaultConfig {
      versionName "1.0.2"
      versionCode 78
    }

所以现在当我创建多个 apk 时,两者都将具有相同的 versionCode .
我的问题是如何将不同的 versionCode 分配给两个生成的 apk,或者我需要在更改它们的 versionCode 后一一创建两个 apk值(value)。

最佳答案

对的,这是可能的

这是我的 gradle 文件,它创建了 3 个具有不同版本代码的不同 apk 文件。您将在下面的 gradle 文件中找到有值(value)的评论。

apply plugin: 'com.android.application'

android {

    splits {
        abi {
            enable true
            reset()
            include 'x86', 'armeabi-v7a', 'armeabi'
            universalApk false
        }
    }
    signingConfigs {
        release {
            keyAlias 'androiddebugkey'
            keyPassword ''
            storeFile file('/Users/anasabubacker/StackOverflow/signing/android_debug.keystore')
            storePassword 'Android'
        }
    }
    compileSdkVersion 26
    buildToolsVersion "27.0.2"
    defaultConfig {
        applicationId "lib4.com.stackoverflow"
        minSdkVersion 17
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        debug {
            debuggable false
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            proguardFile '/Users/anasabubacker/StackOverflow/proguard-android.txt'
        }
    }
}

// Map for the version code that gives each ABI a value.
ext.abiCodes = ['armeabi-v7a':1, x86:2, armeabi:3]

// For per-density APKs, create a similar map like this:
// ext.densityCodes = ['mdpi': 1, 'hdpi': 2, 'xhdpi': 3]

import com.android.build.OutputFile

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


dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support:design:26.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

关于android - 如何为具有不同 cpu 架构的相同 apk 文件创建不同的版本号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48034785/

相关文章:

sql-server - 通过大量连接提高 View 的性能

c# - ASP.NET 应用程序 - 关于如何加速它的想法

android - 在应用程序启动时突然收到 Firebase java.lang.IllegalAccessError

android - Android EditText 是否具有带清除按钮的输入类型?

android - BroadcastReceiver onReceive超时

c# - 高效实现 "ThenBy"排序

android - Gradle 的依赖缓存可能已损坏(这有时会在网络连接超时后发生。)

gradle - svnant的嵌套复制任务无法在gradle中工作

android - 使用 OpenGL ES 2.0 绘制 2D 图像

Android - 应在此处传递已解析的颜色而不是资源 ID : `getResources().getColor(R.attr.colorPrimary)`