Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does BuildConfig.VERSION_CODE not get changed if we are using multiple APK's

I am creating multiple apk's for each ABI, I did this to assign separate version code to each apk.

ext.abiCodes = ['armeabi-v7a':1, mips:2, x86:3]

import com.android.build.OutputFile
android.applicationVariants.all { variant ->
    variant.outputs.each { output ->
        def baseAbiVersionCode =
                project.ext.abiCodes.get(output.getFilter(OutputFile.ABI))

        if (baseAbiVersionCode != null) {
            output.versionCodeOverride =
                    baseAbiVersionCode * 1000 + variant.versionCode
        }
    }
} 

This is my dafaultConfig, and the following versionCode is for universalApk

 defaultConfig {
        versionCode 74
        versionName "1.0.2"
}

I append the versionCode with each service request, I also display the versionCode in one of the activity. So i need the correct versionCode to perform these operations, I use this code to get versionCode

int versionCode = BuildConfig.VERSION_CODE;

Now the problem is that, it displays the versionCode that is stated in defaultConfig and not the one that actually represents the build.

I need to know that how can i get the versionCode that is assigned to this build, that may be some number greater than 1000, and not the one that is assigned in dafaultConfig

like image 568
dev90 Avatar asked Jan 05 '18 10:01

dev90


2 Answers

To check version code you can use PackageInfo and PackageManager

try {
    PackageInfo pInfo = this.getPackageManager().getPackageInfo(getPackageName(), 0);
    String version = pInfo.versionName;   //version name
    int verCode = pInfo.versionCode;      //version code
} catch (PackageManager.NameNotFoundException e) {
    e.printStackTrace();
}
like image 71
Ranjan Avatar answered Oct 04 '22 15:10

Ranjan


You should not read build config file, build config file will display the default configuration.

You should use PackageInfo

  try {
            PackageInfo pInfo = this.getPackageManager().getPackageInfo(getPackageName(), 0);
            String version = pInfo.versionName;
            Log.e("name","name"+version);
            Log.e("Code","code"+pInfo.versionCode);
        }catch (Exception e){
            e.printStackTrace();
        }

Which will display 1001 for armeabi-v7a, 1002 for mips and 1003 for x86

like image 24
lib4 Avatar answered Oct 04 '22 13:10

lib4