How to get the build/version number of your Android application?

前端 未结 30 1745
刺人心
刺人心 2020-11-22 11:00

I need to figure out how to get or make a build number for my Android application. I need the build number to display in the UI.

Do I have to do something with

相关标签:
30条回答
  • 2020-11-22 11:27

    I have SOLVE this by using Preference class.

    package com.example.android;
    
    import android.content.Context;
    import android.preference.Preference;
    import android.util.AttributeSet;
    
    public class VersionPreference extends Preference {
        public VersionPreference(Context context, AttributeSet attrs) {
            super(context, attrs);
            String versionName;
            final PackageManager packageManager = context.getPackageManager();
            if (packageManager != null) {
                try {
                    PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
                    versionName = packageInfo.versionName;
                } catch (PackageManager.NameNotFoundException e) {
                    versionName = null;
                }
                setSummary(versionName);
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-22 11:27

    As I had to get only version code and check whether app is updated or not, if yes, I had to launch the playstore to get updated one. I did this way.

    public class CheckForUpdate {
    
    public static final String ACTION_APP_VERSION_CHECK="app-version-check";
    
    public static void launchPlayStoreApp(Context context)
    {
    
        final String appPackageName = context.getPackageName(); // getPackageName() from Context or Activity object
        try {
            context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
        } catch (android.content.ActivityNotFoundException anfe) {
            context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
        }
    
    }
    
    public static int getRemoteVersionNumber(Context context)
    {
        int versionCode=0;
        try {
            PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            String version = pInfo.versionName;
            versionCode=pInfo.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionCode;
    }
    
    }
    

    Then I saved version code using shared preference by creating an util class.

    public class PreferenceUtils {
    
    // this is for version code
    private  final String APP_VERSION_CODE = "APP_VERSION_CODE";
    private  SharedPreferences sharedPreferencesAppVersionCode;
    private SharedPreferences.Editor editorAppVersionCode;
    private static Context mContext;
    
    public PreferenceUtils(Context context)
    {
        this.mContext=context;
        // this is for app versioncode
        sharedPreferencesAppVersionCode=mContext.getSharedPreferences(APP_VERSION_CODE,MODE_PRIVATE);
        editorAppVersionCode=sharedPreferencesAppVersionCode.edit();
    }
    
    public void createAppVersionCode(int versionCode) {
    
        editorAppVersionCode.putInt(APP_VERSION_CODE, versionCode);
        editorAppVersionCode.apply();
    }
    
    public int getAppVersionCode()
    {
        return sharedPreferencesAppVersionCode.getInt(APP_VERSION_CODE,0); // as default  version code is 0
         }
       }
    
    0 讨论(0)
  • 2020-11-22 11:29

    try this one:

    try 
    {
        device_version =  getPackageManager().getPackageInfo("com.google.android.gms", 0).versionName;
    }
    catch (PackageManager.NameNotFoundException e)
    {
        e.printStackTrace();
    }
    
    0 讨论(0)
  • 2020-11-22 11:30

    There are two parts you need: android:versionCode android:versionName

    versionCode is a number, and every version of the app you submit to the Market needs to have a higher number then the last.

    VersionName is a string, and can be anything you want it to be. This is where you define your app as "1.0" or "2.5" or "2 Alpha EXTREME!" or whatever.

    Example:

    Kotlin:

    val manager = this.packageManager
    val info = manager.getPackageInfo(this.packageName, PackageManager.GET_ACTIVITIES)
    toast("PackageName = " + info.packageName + "\nVersionCode = "
                + info.versionCode + "\nVersionName = "
                + info.versionName + "\nPermissions = " + info.permissions)
    

    Java:

    PackageManager manager = this.getPackageManager();
    PackageInfo info = manager.getPackageInfo(this.getPackageName(), PackageManager.GET_ACTIVITIES);
    Toast.makeText(this,
         "PackageName = " + info.packageName + "\nVersionCode = "
           + info.versionCode + "\nVersionName = "
           + info.versionName + "\nPermissions = " + info.permissions, Toast.LENGTH_SHORT).show();
    
    0 讨论(0)
  • 2020-11-22 11:31

    Someone who does’t need BuildConfig info for application's UI however wants to use these info for setting a CI job configuration or others, like me.

    There is a automatically generated file, BuildConfig.java, under your project directory as long as you build your project successfully.

    {WORKSPACE}/build/generated/source/buildConfig/{debug|release}/{PACKAGE}/BuildConfig.java

    /**
    * Automatically generated file. DO NOT MODIFY
    */
    package com.XXX.Project;
    
    public final class BuildConfig {
        public static final boolean DEBUG = Boolean.parseBoolean("true");
        public static final String APPLICATION_ID = "com.XXX.Project";
        public static final String BUILD_TYPE = "debug";
        public static final String FLAVOR = "";
        public static final int VERSION_CODE = 1;
        public static final String VERSION_NAME = "1.0.0";
    }
    

    Split information you need by python script or other tools. Here’s an example:

    import subprocess
    #find your BuildConfig.java
    _BuildConfig = subprocess.check_output('find {WORKSPACE} -name BuildConfig.java', shell=True).rstrip()
    #get the version name
    _Android_version = subprocess.check_output('grep -n "VERSION_NAME" '+_BuildConfig, shell=True).split('"')[1]
    print('Android version :’+_Android_version)
    

    Please excuse my limited English ability, but hope this helps.

    0 讨论(0)
  • 2020-11-22 11:32

    No,you don't need to do anything with AndroidManifest.xml

    Basically your app's version name & version code inside app level gradle file, under defaultConfig tag:

    defaultConfig {  
       versionCode 1  
       versionName "1.0"  
    }  
    

    Note: When you wish to upload app in playstore can give any name as version name but version code have to be different than current version code if this app already in play store.

    Simply use the following code snippet to get version code & version name from anywhere in your app:

    try {  
        PackageInfo pInfo =   context.getPackageManager().getPackageInfo(context.getPackageName(), 0);  
        String version = pInfo.versionName;  
        int verCode = pInfo.versionCode;  
    } catch (PackageManager.NameNotFoundException e) {  
        e.printStackTrace();  
    }  
    
    0 讨论(0)
提交回复
热议问题