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

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

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

    Kotlin example:

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.act_signin)
    
        packageManager.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES).apply {
            findViewById<TextView>(R.id.text_version_name).text = versionName
            findViewById<TextView>(R.id.text_version_code).text =
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) "$longVersionCode" else "$versionCode"
        }
    
        packageManager.getApplicationInfo(packageName, 0).apply{
            findViewById<TextView>(R.id.text_build_date).text =
                SimpleDateFormat("yy-MM-dd hh:mm").format(java.io.File(sourceDir).lastModified())
        }
    }
    

    Don't thank :-)

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

    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. :)

    0 讨论(0)
  • 2020-11-22 11:18
    private String GetAppVersion(){
            try {
                PackageInfo _info = mContext.getPackageManager().getPackageInfo(mContext.getPackageName(), 0);
                return _info.versionName;
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
                return "";
            }
        }
    
        private int GetVersionCode(){
            try {
                PackageInfo _info = mContext.getPackageManager().getPackageInfo(mContext.getPackageName(), 0);
                return _info.versionCode;
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
                return -1;
            }
        }
    
    0 讨论(0)
  • 2020-11-22 11:19
      PackageInfo pinfo = null;
        try {
            pinfo = getPackageManager().getPackageInfo(getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        int versionNumber = pinfo.versionCode;
        String versionName = pinfo.versionName;
    
    0 讨论(0)
  • 2020-11-22 11:21

    To get the app version or build code which is used to identify the apk by its version code. Version code is used to detect the actual build configuration at the time of update, publishing, etc.

    int versionCode = BuildConfig.VERSION_CODE;
    

    Version name is used to show the users or the developers of the development sequence. You can add any kind of version name as you want

    String versionName = BuildConfig.VERSION_NAME;
    
    0 讨论(0)
提交回复
热议问题