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 20:03

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;
查看更多
萌妹纸的霸气范
3楼-- · 2018-12-31 20:04

try this one:

try 
{
    device_version =  getPackageManager().getPackageInfo("com.google.android.gms", 0).versionName;
}
catch (PackageManager.NameNotFoundException e)
{
    e.printStackTrace();
}
查看更多
骚的不知所云
4楼-- · 2018-12-31 20:05

Using Gradle and BuildConfig

Getting the VERSION_NAME from BuildConfig

BuildConfig.VERSION_NAME

Yep, it's that easy now.

Is It Returning an Empty String for VERSION_NAME?

If you're getting a empty string for BuildConfig.VERSION_NAME then read on.

I kept getting an empty string for BuildConfig.VERSION_NAME because I wasn't setting the versionName in my Grade build file (I migrated from ANT to Gradle). So, here are instructions for ensuring you're setting your VERSION_NAME via Gradle.

build.gradle

def versionMajor = 3
def versionMinor = 0
def versionPatch = 0
def versionBuild = 0 // bump for dogfood builds, public betas, etc.

android {

  defaultConfig {
    versionCode versionMajor * 10000 + versionMinor * 1000 + versionPatch * 100 + versionBuild

    versionName "${versionMajor}.${versionMinor}.${versionPatch}"
  }

}

Note: This is from the masterful Jake Wharton.

Removing versionName and versionCode from AndroidManifest.xml

And since you've set the versionName and versionCode in the build.gradle file now, you can also remove them from your AndroidManifest.xml file, if they are there.

查看更多
看风景的人
5楼-- · 2018-12-31 20:05

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
     }
   }
查看更多
其实,你不懂
6楼-- · 2018-12-31 20:06

For xamarin users, use this code to get version name and code

1) Version Name:

public string getVersionName(){
      return Application.Context.ApplicationContext.PackageManager.GetPackageInfo(Application.Context.ApplicationContext.PackageName, 0).VersionName;
}

2) Version Code:

public string getVersionCode(){
      return Application.Context.ApplicationContext.PackageManager.GetPackageInfo(Application.Context.ApplicationContext.PackageName, 0).VersionCode;
}
查看更多
与风俱净
7楼-- · 2018-12-31 20:09

This code was mentioned above in pieces but here it is again all included. You need a try/catch block because it may throw a "NameNotFoundException".

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

I hope this simplifies things for someone down the road. :)

查看更多
登录 后发表回答