2

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.

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

Is there nothing simple like @android:strings:version?

Milad Faridnia
  • 9,113
  • 13
  • 65
  • 78
Mark Molina
  • 5,057
  • 8
  • 41
  • 69
  • possible duplicate of [Get application version programatically in android](http://stackoverflow.com/questions/6593592/get-application-version-programatically-in-android) – Eugen Martynov Jun 13 '14 at 11:30
  • Take a look http://stackoverflow.com/questions/6593592/get-application-version-programatically-in-android – Eugen Martynov Jun 13 '14 at 11:30
  • Read the question. I'm asking for the versionname from build.gradle so I can access it in my xml – Mark Molina Jun 13 '14 at 11:37
  • So you want to edit xml instead of build.gradle? So gradle will put this value in your AndroidManifest – Eugen Martynov Jun 13 '14 at 11:41
  • 3
    You can use gradle.properties inside your root project and define: VERSION_NAME=1.2.1 VERSION_CODE=26 Then in your build.gradle you can use: versionName project.VERSION_NAME versionCode Integer.parseInt(project.VERSION_CODE) – G3M Jun 15 '14 at 19:51
  • Thanks. Please post as an answer so I can accept it. – Mark Molina Jun 16 '14 at 06:35

1 Answers1

3

You can use BuildConfig class for this. For example:

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

or

Toast.makeText(this, BuildConfig.VERSION_NAME, Toast.LENGTH_SHORT).show();
Milad Faridnia
  • 9,113
  • 13
  • 65
  • 78
Victor Gomes
  • 463
  • 6
  • 15