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

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

    Slightly shorter version if you just want the version name.

    String versionName = context.getPackageManager()
        .getPackageInfo(context.getPackageName(), 0).versionName;
    
    0 讨论(0)
  • 2020-11-22 11:33

    If you want to use it on xml then add below line on your gradle file:

    applicationVariants.all { variant ->
        variant.resValue "string", "versionName", variant.versionName
    }
    

    And then use it on your xml like this:

    <TextView
            android:gravity="center_horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/versionName" />
    
    0 讨论(0)
  • 2020-11-22 11:33
     package com.sqisland.android.versionview;
    
    import android.app.Activity;
    import android.content.pm.PackageInfo;
    import android.content.pm.PackageManager;
    import android.os.Bundle;
    import android.widget.TextView;
    
    public class MainActivity extends Activity {
      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        TextView textViewversionName = (TextView) findViewById(R.id.text);
    
        try {
            PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
            textViewversionName.setText(packageInfo.versionName);
    
        }
        catch (PackageManager.NameNotFoundException e) {
    
        }
    
      }
    }
    
    0 讨论(0)
  • 2020-11-22 11:33

    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
    
    0 讨论(0)
  • 2020-11-22 11:34

    There are some ways to get versionCode and versionName programmatically.

    1. Get version from PackageManager. This is the best way for most cases.
    try {
        String versionName = packageManager.getPackageInfo(packageName, 0).versionName;
        int versionCode = packageManager.getPackageInfo(packageName, 0).versionCode;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    
    1. Get it from generated BuildConfig.java. But notice, that if you'll access this values in library it will return library version, not apps one, that uses this library. So use only in non-library projects!
    String versionName = BuildConfig.VERSION_NAME;
    int versionCode = BuildConfig.VERSION_CODE;
    

    There are some details, except of using second way in library project. In new android gradle plugin (3.0.0+) some functionalities removed. So, for now, i.e. setting different version for different flavors not working correct.

    Incorrect way:

    applicationVariants.all { variant ->
        println('variantApp: ' + variant.getName())
    
        def versionCode = {SOME_GENERATED_VALUE_IE_TIMESTAMP}
        def versionName = {SOME_GENERATED_VALUE_IE_TIMESTAMP}
    
        variant.mergedFlavor.versionCode = versionCode
        variant.mergedFlavor.versionName = versionName
    }
    

    Code above will correctly set values in BuildConfig, but from PackageManager you'll receive 0 and null if you didn't set version in default configuration. So your app will have 0 version code on device.

    There is a workaround - set version for output apk file manually:

    applicationVariants.all { variant ->
        println('variantApp: ' + variant.getName())
    
        def versionCode = {SOME_GENERATED_VALUE_IE_TIMESTAMP}
        def versionName = {SOME_GENERATED_VALUE_IE_TIMESTAMP}
    
        variant.outputs.all { output ->
            output.versionCodeOverride = versionCode
            output.versionNameOverride = versionName
        }
    }
    
    0 讨论(0)
  • 2020-11-22 11:34

    As in 2020 : API 28 "versionCode" is deprecated so we can use "longVersionCode"

    Sample code in kotlin

      val manager = context?.packageManager
            val info = manager?.getPackageInfo(
                context?.packageName, 0
            )
    
            val versionName = info?.versionName
            val versionNumber = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                info?.longVersionCode
            } else {
                info?.versionCode
            }
    
    0 讨论(0)
提交回复
热议问题