如何从应用程序(布局)XML 变量中获取清单版本号?
我希望有一种方法可以在代码的主要部分引用项目的清单版本号。到目前为止,我一直在做的是将字符串 XML 文件中的版本号链接到清单 (@string/Version)。我想要做的是反过来,将字符串 XML 变量链接到清单中的版本。原因是什么?我只想更改一个位置(清单文件)的版本号。有什么办法可以做到这一点吗?谢谢!
I would like to have a way to reference the project's manifest version number in the main part of the code. What I have been doing up until now is to link the version number in a String XML file to the manifest (@string/Version). What I would like to do is to do it the other way around, link a string XML variable to the version in the manifest. The reason? I'd like to only have to change the version number in one location, the manifest file. Is there any way to do this? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
没有办法直接获取版本,但有两种解决方法可以完成。
版本可以存储在资源字符串中,并通过以下方式放入清单中:
可以创建自定义视图,并将其放入 XML 中。视图将使用它来分配名称:
这些解决方案中的任何一个都允许将版本名称放置在 XML 中。不幸的是,没有一个很好的简单解决方案,例如 android.R.string.version 或类似的东西。
There is not a way to directly get the version out, but there are two work-arounds that could be done.
The version could be stored in a resource string, and placed into the manifest by:
One could create a custom view, and place it into the XML. The view would use this to assign the name:
Either of these solutions would allow for placing the version name in XML. Unfortunately there isn't a nice simple solution, like
android.R.string.version
or something like that.您可以在 XML 资源(例如活动布局)中使用
versionName
。首先在app/build.gradle
中创建一个字符串资源,并在android
节点中使用以下代码片段:因此整个
build.gradle
文件内容可能如下所示:然后您可以在 XML 中使用
@string/versionName
。 Android Studio 会将其标记为红色,但应用程序将顺利编译。例如,可以在app/src/main/res/xml/preferences.xml
中这样使用:You can use the
versionName
in XML resources, such as activity layouts. First create a string resource in theapp/build.gradle
with the following snippet in theandroid
node:So the whole
build.gradle
file contents may look like this:Then you can use
@string/versionName
in the XML. Android Studio will mark it red, but the app will compile without issues. For example, this may be used like this inapp/src/main/res/xml/preferences.xml
:如果您使用 Gradle,则可以使用 build.gradle 文件在编译时以编程方式向 xml 资源添加值。
示例代码摘自:
https://medium.com/ @manas/manage-your-android-app-s-versioncode-versionname-with-gradle-7f9c5dcf09bf
现在根据需要在 XML 中使用
@string/app_version
它将添加
。在调试模式下,将 debug
更改为链接文章中所述的版本名称。IF you are using Gradle you can use the build.gradle file to programmatically add value to the xml resources at compile time.
Example Code extracted from:
https://medium.com/@manas/manage-your-android-app-s-versioncode-versionname-with-gradle-7f9c5dcf09bf
now use
@string/app_version
as needed in XMLIt will add
.debug
to the version name as describe in the linked article when in debug mode.我通过扩展 Preference 类解决了这个问题。
然后在我的偏好 XML 中:
I solved this issue by extending the Preference class.
Then in my preferences XML:
我使用
BuildConfig.VERSION_NAME.toString();
。这和从 packageManager 获取有什么区别?抱歉,没有基于 XML 的解决方案对我有用。
I use
BuildConfig.VERSION_NAME.toString();
. What's the difference between that and getting it from the packageManager?No XML based solutions have worked for me, sorry.
您不能从 XML 使用它。
您需要扩展您在 XML 中使用的小部件,并添加逻辑以使用 Konstantin Burov 的答案中提到的内容设置文本。
You can't use it from the XML.
You need to extend the widget you are using in the XML and add the logic to set the text using what's mentioned on Konstantin Burov's answer.
最简单的解决方案是使用
BuildConfig
。我在应用程序中使用
BuildConfig.VERSION_NAME
。您还可以使用
BuildConfig.VERSION_CODE
获取版本代码。Easiest solution is to use
BuildConfig
.I use
BuildConfig.VERSION_NAME
in my application.You can also use
BuildConfig.VERSION_CODE
to get version code.游戏迟到了,但您可以在没有
@string/xyz
的情况下使用?android:attr
来完成此操作Late to the game, but you can do it without
@string/xyz
by using?android:attr