在build.gradle中访问versionName [英] Access versionName in build.gradle

查看:728
本文介绍了在build.gradle中访问versionName的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法从xml中访问 build.gradle 中的 versionName 我想用这个值设置一个 TextView 的文本。

Is there a way to access versionName in my build.gradle from xml? I would like to set a TextView's text with this value.

或者可以用版本声明一个字符串在 strings.xml 中,并在 build.gradle

Or maybe declare a string with the version in strings.xml and use that in build.gradle?

有没有简单的像 @android:strings:version

推荐答案

您可以使用 BuildConfig 类来实现此目的。例如:

You can use BuildConfig class for this. For example:

TextView mTv = (TextView) findViewById(R.id.mTv);
mTv.setText(BuildConfig.VERSION_NAME);

Toast.makeText(this, BuildConfig.VERSION_NAME, Toast.LENGTH_SHORT).show();

这篇关于在build.gradle中访问versionName的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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