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

前端 未结 30 1763
刺人心
刺人心 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:22

    Here is the method for getting the version code:

    public String getAppVersion() {
        String versionCode = "1.0";
        try {
            versionCode = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return versionCode;
    }
    
    0 讨论(0)
  • 2020-11-22 11:23

    Here is a clean solution, based on the solution of scottyab (edited by Xavi). It shows how to get the context first, if it's not provided by your method. Furthermore it uses multiple lines instead of calling multiple methods per line. This makes it easier when you have to debug your application.

    Context context = getApplicationContext(); // or activity.getApplicationContext()
    PackageManager packageManager = context.getPackageManager();
    String packageName = context.getPackageName();
    
    String myVersionName = "not available"; // initialize String
    
    try {
        myVersionName = packageManager.getPackageInfo(packageName, 0).versionName;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    

    Now that you received the version name in the String myVersionName, you can set it to a TextView or whatever you like..

    // set version name to a TextView
    TextView tvVersionName = (TextView) findViewById(R.id.tv_versionName);
    tvVersionName.setText(myVersionName);
    
    0 讨论(0)
  • 2020-11-22 11:23

    First:

    import android.content.pm.PackageManager.NameNotFoundException;
    

    and then use this:

    PackageInfo pInfo = null;
    try {
         pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
    } catch (NameNotFoundException e) {
         e.printStackTrace();
                }
    String versionName = pInfo.versionName;
    
    0 讨论(0)
  • 2020-11-22 11:25

    If you're using PhoneGap, then create a custom PhoneGap plugin:

    Create a new class in your app's package:

    package com.Demo; //replace with your package name
    
    import org.json.JSONArray;
    
    import android.content.pm.PackageInfo;
    import android.content.pm.PackageManager;
    import android.content.pm.PackageManager.NameNotFoundException;
    
    import com.phonegap.api.Plugin;
    import com.phonegap.api.PluginResult;
    import com.phonegap.api.PluginResult.Status;
    
    public class PackageManagerPlugin extends Plugin {
    
        public final String ACTION_GET_VERSION_NAME = "GetVersionName";
    
        @Override
        public PluginResult execute(String action, JSONArray args, String callbackId) {
            PluginResult result = new PluginResult(Status.INVALID_ACTION);
            PackageManager packageManager = this.ctx.getPackageManager();
    
            if(action.equals(ACTION_GET_VERSION_NAME)) {
                try {
                    PackageInfo packageInfo = packageManager.getPackageInfo(
                                                  this.ctx.getPackageName(), 0);
                    result = new PluginResult(Status.OK, packageInfo.versionName);
                }
                catch (NameNotFoundException nnfe) {
                    result = new PluginResult(Status.ERROR, nnfe.getMessage());
                }
            }
    
            return result;
        }
    }
    

    In the plugins.xml, add the following line:

    <plugin name="PackageManagerPlugin" value="com.Demo.PackageManagerPlugin" />
    

    In your deviceready event, add the following code:

    var PackageManagerPlugin = function() {
    
    };
    PackageManagerPlugin.prototype.getVersionName = function(successCallback, failureCallback) {
        return PhoneGap.exec(successCallback, failureCallback, 'PackageManagerPlugin', 'GetVersionName', []);
    };
    PhoneGap.addConstructor(function() {
        PhoneGap.addPlugin('packageManager', new PackageManagerPlugin());
    });
    

    Then, you can get the versionName attribute by doing:

    window.plugins.packageManager.getVersionName(
        function(versionName) {
            //do something with versionName
        },
        function(errorMessage) {
            //do something with errorMessage
        }
    );
    

    Derived from here and here.

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

    Use:

    try {
        PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
        String version = pInfo.versionName;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    

    And you can get the version code by using this

    int verCode = pInfo.versionCode;
    
    0 讨论(0)
  • 2020-11-22 11:27

    Use BuildConfig class

    String versionName = BuildConfig.VERSION_NAME;
    int versionCode = BuildConfig.VERSION_CODE;
    

    build.gradle(app)

     defaultConfig {
        applicationId "com.myapp"
        minSdkVersion 19
        targetSdkVersion 27
        versionCode 17
        versionName "1.0"
       }
    
    0 讨论(0)
提交回复
热议问题