How to get the build/version number of your Androi

2018-12-31 19:45发布

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 AndroidManifest.xml?

28条回答
其实,你不懂
2楼-- · 2018-12-31 19:46
try {
        PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
        String versionName = packageInfo.versionName;
        int versionCode = packageInfo.versionCode;
        //binding.tvVersionCode.setText("v" + packageInfo.versionName);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
查看更多
不再属于我。
3楼-- · 2018-12-31 19:47

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);
        }
    }
}
查看更多
妖精总统
4楼-- · 2018-12-31 19:50

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.

查看更多
听够珍惜
5楼-- · 2018-12-31 19:50

Here is the simple way to get version name and version code

String version_name=BuildConfig.VERSION_NAME;
int version_code=BuildConfig.VERSION_CODE; 
查看更多
伤终究还是伤i
6楼-- · 2018-12-31 19:52

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);
查看更多
余欢
7楼-- · 2018-12-31 19:52

Example for inside Fragment usage.

        import android.content.pm.PackageManager;
        .......

        private String VersionName;
        private String VersionCode;
        .......


        Context context = getActivity().getApplicationContext();

        /*Getting Application Version Name and Code*/
        try
        {

             VersionName = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;

             /*I find usefull to convert vervion code into String, so it's ready for TextViev/server side checks*/ 

             VersionCode = Integer.toString(context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode);
        } catch (PackageManager.NameNotFoundException e)
        {
             e.printStackTrace();
        }

// DO SOMETHING USEFULL WITH THAT
查看更多
登录 后发表回答