如何获取 Android 应用程序的内部版本号?
我需要弄清楚如何获取或创建我的 Android 应用程序的内部版本号。我需要在用户界面中显示内部版本号。
我是否必须对 AndroidManifest.xml 执行某些操作?
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
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
如果您使用 Gradle 插件/Android Studio,自版本 0.7.0< /a>,版本代码和版本名称在
BuildConfig
中静态可用。 确保导入应用程序的包,而不是另一个BuildConfig
:不需要 Context 对象!
另请确保在
build.gradle
文件而不是AndroidManifest.xml
中指定它们。If you're using the Gradle plugin/Android Studio, as of version 0.7.0, version code and version name are available statically in
BuildConfig
. Make sure you import your app's package, and not anotherBuildConfig
:No Context object needed!
Also make sure to specify them in your
build.gradle
file instead of theAndroidManifest.xml
.使用:
并且可以通过使用它来获取版本代码
Use:
And you can get the version code by using this
如果您只想要版本名称,则版本稍短。
Slightly shorter version if you just want the version name.
您需要两个部分:
versionCode 是一个数字,您提交到市场的应用程序的每个版本都需要具有比上一个更高的数字。
VersionName 是一个字符串,可以是您想要的任何内容。您可以在此处将应用程序定义为“1.0”或“2.5”或“2 Alpha EXTREME!”或其他什么。
例子:
科特林:
Java:
There are two parts you need:
versionCode is a number, and every version of the app you submit to the market needs to have a higher number than the last.
VersionName is a string and can be anything you want it to be. This is where you define your app as "1.0" or "2.5" or "2 Alpha EXTREME!" or whatever.
Example:
Kotlin:
Java:
使用 Gradle 和 BuildConfig
从 BuildConfig 获取 VERSION_NAME
是的,现在就这么简单。
它是否为 VERSION_NAME 返回空字符串?
如果您得到的
BuildConfig.VERSION_NAME
为空字符串,请继续阅读。我一直得到
BuildConfig.VERSION_NAME
的空字符串,因为我没有在我的 Grade 构建文件中设置versionName
(我是从 Ant到摇篮)。因此,以下说明可确保您通过 Gradle 设置VERSION_NAME
。文件 build.gradle
注意:这是来自大师 Jake沃顿商学院。
从
AndroidManifest.xml
中删除versionName
和versionCode
因为您已经设置了现在
build.gradle
文件中的versionName
和versionCode
,您也可以从AndroidManifest.xml
文件中删除它们,如果他们在那里的话。Using Gradle and BuildConfig
Getting the VERSION_NAME from BuildConfig
Yep, it's that easy now.
Is it returning an empty string for VERSION_NAME?
If you're getting an empty string for
BuildConfig.VERSION_NAME
then read on.I kept getting an empty string for
BuildConfig.VERSION_NAME
, because I wasn't setting theversionName
in my Grade build file (I migrated from Ant to Gradle). So, here are instructions for ensuring you're setting yourVERSION_NAME
via Gradle.File build.gradle
Note: This is from the masterful Jake Wharton.
Removing
versionName
andversionCode
fromAndroidManifest.xml
And since you've set the
versionName
andversionCode
in thebuild.gradle
file now, you can also remove them from yourAndroidManifest.xml
file, if they are there.这是一个干净的解决方案,基于scottyab 的解决方案(由 Xavi 编辑)。它展示了如何首先获取上下文(如果您的方法未提供)。此外,它使用多行而不是每行调用多个方法。当您必须调试应用程序时,这会让您变得更容易。
现在您已收到字符串
myVersionName
中的版本名称,您可以将其设置为 TextView 或您喜欢的任何内容。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.
Now that you received the version name in the String
myVersionName
, you can set it to a TextView or whatever you like..使用以下命令获取应用程序版本或构建代码,用于通过版本代码识别 APK 文件。版本代码用于检测更新、发布等时的实际构建配置。
版本名称用于向用户或开发人员显示开发顺序。您可以根据需要添加任何类型的版本名称。
Use the following to get the app version or build code which is used to identify the APK file by its version code. The version code is used to detect the actual build configuration at the time of update, publishing, etc.
The 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.
Kotlin 的单行代码
Java 的单行代码
确保将
BuildConfig
导入到您的类中。Kotlin one-liners
Java one-liners
Make sure to import
BuildConfig
into your class.使用 BuildConfig 类:
文件 build.gradle (应用程序)
Use the BuildConfig class:
File build.gradle (app)
截至 2020 年:从 API 28 (Android 9 (Pie)) 开始, “versionCode”已弃用,因此我们可以使用“longVersionCode”。
Kotlin 中的示例代码
As in 2020: As of API 28 (Android 9 (Pie)), "versionCode" is deprecated so we can use "longVersionCode".
Sample code in Kotlin
这个问题有两种不同的情况,任何答案都没有正确解决。
场景 1:您没有使用模块
如果您没有使用模块,您可以访问您的 BuildConfig 文件并立即获取您的版本代码:
这是有效的,因为这是您的应用程序级别的 BuildConfig 文件,因此它将包含对您的应用程序版本代码
场景 2:您的应用程序有许多模块,并且您假装从模块层次结构中较低的模块访问应用程序版本代码
对于您来说,有许多具有给定层次结构的模块是正常的,例如 app -> >数据->域->在这种情况下,如果您从“ui”模块访问 BuildConfig 文件,它不会为您提供应用程序版本代码的引用,而是该模块的版本代码。
为了获取应用程序版本代码,您可以使用以下给定代码:
首先是一个获取 PackageInfo 的扩展函数
获取版本代码的扩展函数
版本名称的方法类似:
There are two different scenarios in this question that are not properly addressed in any of the answers.
Scenario 1: You are not using modules
If you are not making use of modules, you can access your BuildConfig file and immeditally get your version code with:
This is valid because this is your app level BuildConfig file and therefor it will contain the reference to your application version code
Scenario 2: Your app has many modules and you pretend to access the application version code from a lower module in your module hierarchy
It is normal for you to have many modules with a given hierarchy such as app -> data -> domain -> ui, etc. In this case, if you access the BuildConfig file from the "ui" module it will not give you a reference to the app version code but to the version code of that module.
In order to get the application version code you can use the following given code:
First an extension function to get the PackageInfo
Extension function to get the version code
The approach for version name is similar:
如果您使用的是 PhoneGap,则创建一个自定义 PhoneGap 插件:
在应用程序包中创建一个新类:
在 plugins.xml 中,添加以下行:
在您的 deviceready事件,添加以下代码:
然后,您可以通过以下方式获取versionName属性:
Derived from 此处以及此处。
If you're using PhoneGap, then create a custom PhoneGap plugin:
Create a new class in your app's package:
In the plugins.xml, add the following line:
In your deviceready event, add the following code:
Then, you can get the versionName attribute by doing:
Derived from here and here.
不,您不需要对 AndroidManifest.xml 执行任何操作
基本上,您应用的版本名称和版本代码位于 应用级 Gradle 文件内的 defaultConfig 标记下:
< em>注意:当您希望将应用程序上传到Play商店时,可以使用任何名称作为版本名称,但如果该应用程序已在Play商店中,则版本代码必须与当前版本代码不同。< /em>
只需使用以下代码片段即可获取版本代码 &应用中任意位置的版本名称:
No, you don't need to do anything with AndroidManifest.xml
Basically, your app's version name and version code are inside the app level Gradle file, under defaultConfig tag:
Note: When you wish to upload an app to the play store, it can give any name as the version name, but the version code has to be different than the current version code if this app is already in the play store.
Simply use the following code snippet to get the version code & version name from anywhere in your app:
对于 API 28 (Android 9 (Pie)),PackageInfo.versionCode已弃用,因此请使用下面的代码:
For API 28 (Android 9 (Pie)), the PackageInfo.versionCode is deprecated, so use this code below:
版本名称:BuildConfig.VERSION_NAME
版本代码:BuildConfig.VERSION_CODE
Version name: BuildConfig.VERSION_NAME
Version code: BuildConfig.VERSION_CODE
如果您想在 XML 内容上使用它,请在 Gradle 文件中添加以下行:
然后在 XML 内容上使用它,如下所示:
If you want to use it on XML content then add the below line in your Gradle file:
And then use it on your XML content like this:
对于 Xamarin 用户,请使用此代码获取版本名称和代码
版本名称:< /p>
版本代码:
For Xamarin users, use this code to get version name and code
Version Name:
Version code:
2024 年 5 月
要检索
VERSION_CODE
,只需使用BuildConfig
类,如以下代码:请记住,
BuildConfig
类是为所有 Android 自动生成的默认情况下使用模块(Gradle 插件 8.0.0+),但是如果您使用较旧版本的插件或在您的项目中使用自定义字段 BuildConfig 类,您需要显式启用模块的build.gradle
文件中的 BuildConfig。 (就像 compose 项目)要解决在
Jetpack Compose
项目中导入BuildConfig
的问题,请按照以下步骤操作:1。在 android 块内的
build.gradle
(应用程序模块)中添加以下代码。2。将您的项目与 Gradle 文件(带有向下箭头的大象按钮)同步。此步骤确保构建系统应用更改并生成所需的代码。
3。 (如果问题仍然存在)从“文件”菜单运行“使缓存无效并重新启动”。
4。最后,从“构建”菜单运行“重建项目”。
⭕ 不要忘记导入
import com.xxx.zzz.BuildConfig
(有时 IDE 不会为您执行此操作)May 2024
To retrieve the
VERSION_CODE
, simply you can use theBuildConfig
class like the following code:Just remember, The
BuildConfig
class is automatically generated for all Android modules by default (Gradle Plugin 8.0.0+), but If you are using an older version of the plugin or using custom fields in your BuildConfig class, you will need to explicitly enable BuildConfig in your module'sbuild.gradle
file. (like as compose projects)To fix the issue of importing
BuildConfig
in yourJetpack Compose
project, follow these steps:1. Add the below code in the
build.gradle
(app module) within android block.2. Sync your project with the Gradle files(the elephant button with a down arrow). This step ensures that the build system applies the changes and generates the required code.
3. (If the problem still persists) Run "invalidate cache and restart" from the "File" menu.
4. Finally, Run "Rebuild Project" from the "Build" menu.
⭕ Don't forget to import
import com.xxx.zzz.BuildConfig
(Sometimes IDE doesn't do it for you)始终使用
try catch
块来执行此操作:Always do it with a
try catch
block:获取版本号的方法如下:
Here is the method for getting the version code:
我通过使用 Preference 类解决了这个问题。
I have solved this by using the Preference class.
有一些方法可以通过编程方式获取
versionCode
和versionName
。从
PackageManager
获取版本。这是大多数情况下的最佳方法。<前><代码>尝试{
String versionName = packageManager.getPackageInfo(packageName, 0).versionName;
int versionCode = packageManager.getPackageInfo(packageName, 0).versionCode;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
从生成的
BuildConfig.java
中获取它。但请注意,如果您在库中访问此值,它将返回使用此库的库版本,而不是应用程序版本。因此仅在非库项目中使用!除了在库项目中使用第二种方式之外,还有一些细节。在新的 Android Gradle 插件 (3.0.0+) 中,一些 功能已删除。因此,目前,即为不同的口味设置不同的版本无法正常工作。
错误的方式:
上面的代码将在
BuildConfig
中正确设置值,但从PackageManager
您将收到0
和null
如果您没有在default
配置中设置版本。因此,您的应用在设备上将具有0
版本代码。有一个解决方法 - 手动设置输出
apk
文件的版本:There are some ways to get
versionCode
andversionName
programmatically.Get version from
PackageManager
. This is the best way for most cases.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!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:
Code above will correctly set values in
BuildConfig
, but fromPackageManager
you'll receive0
andnull
if you didn't set version indefault
configuration. So your app will have0
version code on device.There is a workaround - set version for output
apk
file manually:上面已经多次提到了这段代码,但这里再次将其全部包含在内。您需要一个 try/catch 块,因为它可能会抛出“NameNotFoundException”。
我希望这能为以后的人简化事情。 :)
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".
I hope this simplifies things for someone down the road. :)
对于不需要应用程序 UI 的 BuildConfig 信息,但想要使用此信息来设置 CI 的人 作业配置或其他,像我一样:
只要你成功构建了项目,在你的项目目录下就会有一个自动生成的文件,BuildConfig.java。
{WORKSPACE}/build/ generated/source/buildConfig/{debug|release}/{PACKAGE}/BuildConfig.java
通过Python脚本或其他工具分割您需要的信息。这是一个例子:
For someone who doesn’t need the BuildConfig information for application's UI, however wants to use this information for setting a CI job configuration or others, like me:
There is an automatically generated file, BuildConfig.java, under your project directory as long as you build your project successfully.
{WORKSPACE}/build/generated/source/buildConfig/{debug|release}/{PACKAGE}/BuildConfig.java
Split information you need by a Python script or other tools. Here’s an example:
首先:
然后使用这个:
First:
and then use this:
试试这个:
Try this one:
科特林示例:
Kotlin example:
内部 Fragment 使用示例。
Example for inside Fragment usage.