如何获得应用程序版本

时间:2021-09-05 16:45:25

Can anyone tell me how to get applcation version in Android?

谁能告诉我如何在Android中获得应用版本吗?

10 个解决方案

#1


132  

This page has a tips on how to do it from java:

这个页面有一个关于如何从java实现它的提示:

PackageManager manager = context.getPackageManager();
PackageInfo info = manager.getPackageInfo(
    context.getPackageName(), 0);
String version = info.versionName;

Also, this link has official information on how to properly set up your application versioning.

此外,这个链接还有关于如何正确设置应用程序版本控制的官方信息。

#2


66  

I am using Android Studio, I realized I could use one line code to get this.

我正在使用Android Studio,我意识到我可以用一行代码来获取这个。

/*version name*/
BuildConfig.VERSION_NAME

/*version code*/
BuildConfig.VERSION_CODE

Edited:

编辑:

If you are using other android libraries, make sure you import BuildConfig from your application package. This is similar to the automatically generated R class for identifying resources.

如果您正在使用其他android库,请确保从您的应用程序包中导入BuildConfig。这类似于用于标识资源的自动生成的R类。

#3


12  

To get application information:

获取应用信息:

PackageManager manager = this.getPackageManager();
try {
   PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
   String packageName = info.packageName;
   int versionCode = info.versionCode;
   String versionName = info.versionName;
   } catch (NameNotFoundException e) {
   // TODO Auto-generated catch block
   }

#4


12  

public int getVersion(Context context) {
        try {
            PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_META_DATA);
            return pInfo.versionCode;
        } catch (NameNotFoundException e) {
            return 0;
        }
    }
}

More info on this link

更多关于这个链接的信息

#5


5  

The easiest and best answer i found is to just import your BuildConfig

我找到的最简单、最好的答案是只导入构建配置

import your.package.BuildConfig;

then just

那就

String verName = BuildConfig.VERSION_NAME;
int verCode = BuildConfig.VERSION_CODE;

cheers :)

欢呼:)

#6


2  

In you (Module : app) gradle define version name and version code

在你(模块:app)中定义版本名称和版本代码

defaultConfig {
        applicationId "com.sing.smart"
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 1
        versionName "1.1.0"
    }

versionCode

versionCode

The versionCode is integer value used to easily differentiate between app versions.

versionCode是一个整数值,用来方便区分不同的应用程序版本。

App developers must increment this value when they release updates to their apps in Android Market, so it can determine if users are using an old version of the app, and offer them to update it.

应用开发人员在Android市场发布应用更新时,必须增加这个价值,这样就可以确定用户是否在使用该应用的旧版本,并向他们提供更新。

versionName

versionName

The versionName is a string containing a regular “release version” as seen in other desktop applications, such as “1.4.5” or “3.7”.

versionName是一个包含常规“发布版本”的字符串,如“1.4.5”或“3.7”。

The versionName is just a “human readable” version code.

versionName只是一个“可读的”版本代码。

PackageInfo pinfo = getPackageManager().getPackageInfo(getPackageName(), 0);  
int versionNumber = pinfo.versionCode;  
String versionName = pinfo.versionName;

#7


1  

If you need it for scripting purposes (not for programming) you can use this command:

如果您需要它用于脚本目的(不是用于编程),您可以使用以下命令:

 adb shell "pm dump com.example.your.package.name | grep \"versionCode\"" | awk '{print $1}' | awk -F '=' '{print $2}'

#8


1  

int versionCode = BuildConfig.VERSION_CODE;

int versionCode = BuildConfig.VERSION_CODE;

String versionName = BuildConfig.VERSION_NAME;

字符串versionName = BuildConfig.VERSION_NAME;

#9


1  

If you are using eclipse try this:

如果您正在使用eclipse,请尝试以下操作:

 PackageInfo packageInfo = this.getPackageManager().getPackageInfo(getPackageName(), 0);
        String version = packageInfo.versionName;

In Android Studio :

在Android工作室:

int versionCode = BuildConfig.VERSION_CODE;

make sure you have mansion version code in your module-level build.gradle file

确保在模块级构建中有大厦版本代码。gradle文件

#10


0  

On Nativescript (with Typescript) one can do like this:

在Nativescript(带打字稿)上,我们可以这样做:

import {Observable} from 'data/observable';
import * as applicationModule from 'application'

export class AboutViewModel extends Observable {
    public version: string;

    constructor() {
        super();
        let manager = applicationModule.android.context.getPackageManager();
        let info = manager.getPackageInfo(applicationModule.android.context.getPackageName(), 0);
        this.version = info.versionName;
    }
}

#1


132  

This page has a tips on how to do it from java:

这个页面有一个关于如何从java实现它的提示:

PackageManager manager = context.getPackageManager();
PackageInfo info = manager.getPackageInfo(
    context.getPackageName(), 0);
String version = info.versionName;

Also, this link has official information on how to properly set up your application versioning.

此外,这个链接还有关于如何正确设置应用程序版本控制的官方信息。

#2


66  

I am using Android Studio, I realized I could use one line code to get this.

我正在使用Android Studio,我意识到我可以用一行代码来获取这个。

/*version name*/
BuildConfig.VERSION_NAME

/*version code*/
BuildConfig.VERSION_CODE

Edited:

编辑:

If you are using other android libraries, make sure you import BuildConfig from your application package. This is similar to the automatically generated R class for identifying resources.

如果您正在使用其他android库,请确保从您的应用程序包中导入BuildConfig。这类似于用于标识资源的自动生成的R类。

#3


12  

To get application information:

获取应用信息:

PackageManager manager = this.getPackageManager();
try {
   PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
   String packageName = info.packageName;
   int versionCode = info.versionCode;
   String versionName = info.versionName;
   } catch (NameNotFoundException e) {
   // TODO Auto-generated catch block
   }

#4


12  

public int getVersion(Context context) {
        try {
            PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_META_DATA);
            return pInfo.versionCode;
        } catch (NameNotFoundException e) {
            return 0;
        }
    }
}

More info on this link

更多关于这个链接的信息

#5


5  

The easiest and best answer i found is to just import your BuildConfig

我找到的最简单、最好的答案是只导入构建配置

import your.package.BuildConfig;

then just

那就

String verName = BuildConfig.VERSION_NAME;
int verCode = BuildConfig.VERSION_CODE;

cheers :)

欢呼:)

#6


2  

In you (Module : app) gradle define version name and version code

在你(模块:app)中定义版本名称和版本代码

defaultConfig {
        applicationId "com.sing.smart"
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 1
        versionName "1.1.0"
    }

versionCode

versionCode

The versionCode is integer value used to easily differentiate between app versions.

versionCode是一个整数值,用来方便区分不同的应用程序版本。

App developers must increment this value when they release updates to their apps in Android Market, so it can determine if users are using an old version of the app, and offer them to update it.

应用开发人员在Android市场发布应用更新时,必须增加这个价值,这样就可以确定用户是否在使用该应用的旧版本,并向他们提供更新。

versionName

versionName

The versionName is a string containing a regular “release version” as seen in other desktop applications, such as “1.4.5” or “3.7”.

versionName是一个包含常规“发布版本”的字符串,如“1.4.5”或“3.7”。

The versionName is just a “human readable” version code.

versionName只是一个“可读的”版本代码。

PackageInfo pinfo = getPackageManager().getPackageInfo(getPackageName(), 0);  
int versionNumber = pinfo.versionCode;  
String versionName = pinfo.versionName;

#7


1  

If you need it for scripting purposes (not for programming) you can use this command:

如果您需要它用于脚本目的(不是用于编程),您可以使用以下命令:

 adb shell "pm dump com.example.your.package.name | grep \"versionCode\"" | awk '{print $1}' | awk -F '=' '{print $2}'

#8


1  

int versionCode = BuildConfig.VERSION_CODE;

int versionCode = BuildConfig.VERSION_CODE;

String versionName = BuildConfig.VERSION_NAME;

字符串versionName = BuildConfig.VERSION_NAME;

#9


1  

If you are using eclipse try this:

如果您正在使用eclipse,请尝试以下操作:

 PackageInfo packageInfo = this.getPackageManager().getPackageInfo(getPackageName(), 0);
        String version = packageInfo.versionName;

In Android Studio :

在Android工作室:

int versionCode = BuildConfig.VERSION_CODE;

make sure you have mansion version code in your module-level build.gradle file

确保在模块级构建中有大厦版本代码。gradle文件

#10


0  

On Nativescript (with Typescript) one can do like this:

在Nativescript(带打字稿)上,我们可以这样做:

import {Observable} from 'data/observable';
import * as applicationModule from 'application'

export class AboutViewModel extends Observable {
    public version: string;

    constructor() {
        super();
        let manager = applicationModule.android.context.getPackageManager();
        let info = manager.getPackageInfo(applicationModule.android.context.getPackageName(), 0);
        this.version = info.versionName;
    }
}