如何从应用程序的(布局)XML 变量中获取清单版本号? [英] How can you get the Manifest Version number from the App's (Layout) XML variables?

查看:28
本文介绍了如何从应用程序的(布局)XML 变量中获取清单版本号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有一种方法可以在代码的主要部分引用项目的清单版本号.到目前为止,我一直在做的是将 String 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!

推荐答案

没有直接获取版本的方法,但有两种解决方法.

There is not a way to directly get the version out, but there are two work-arounds that could be done.

  1. 版本可以存储在资源字符串中,并通过以下方式放入清单:

  1. The version could be stored in a resource string, and placed into the manifest by:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.somepackage"
     android:versionName="@string/version" android:versionCode="20">

  • 可以创建自定义视图,并将其放入 XML.视图将使用它来分配名称:

  • One could create a custom view, and place it into the XML. The view would use this to assign the name:

    context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
    

  • 这些解决方案中的任何一个都允许将版本名称放在 XML 中.不幸的是,没有一个很好的简单解决方案,比如 android.R.string.version 或类似的东西.

    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 变量中获取清单版本号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆