0

How to get app versionCode which is inside app build.gradle from xml file? I know how to get it from java code, but I need to get it from xml file. Is it possible?

defaultConfig {
    applicationId "com.example"
    minSdkVersion 15
    targetSdkVersion 27
    versionCode 47 // I want to access this versionCode
    versionName "1.4.2"
    multiDexEnabled true
}

I have a file called remote_config_defaults.xml file under res/xml/ where I increment version code every time manually.

<?xml version="1.0" encoding="utf-8"?>
<!-- START xml_defaults -->
<defaultsMap>
    <entry>
        <key>APP_VERSION_CODE</key>
        <value>47</value> //here I want to get versionCode
    </entry>
</defaultsMap>

I use this file for Firebase remote config. I need to access versionCode from this file.

Bek
  • 7,790
  • 4
  • 18
  • 31
  • *I need to get it from xml file.* - are you sure? Why? This is not xml btw – Tim Sep 03 '18 at 09:26
  • 2
    Possible duplicate of [How can you get the Manifest Version number from the App's (Layout) XML variables?](https://stackoverflow.com/questions/4471025/how-can-you-get-the-manifest-version-number-from-the-apps-layout-xml-variable) – Napster Sep 03 '18 at 09:27
  • @Tim updated my post. – Bek Sep 03 '18 at 09:37
  • This is still not possible! You'll still have to fetch the value "programmatically" – Darshan Sep 03 '18 at 09:46

3 Answers3

1
try {
    PackageInfo pInfo = context.getPackageManager().getPackageInfo(getPackageName(), 0);
    String version = pInfo.versionName;
} catch (PackageManager.NameNotFoundException e) {
    e.printStackTrace();
}
Mohit Kalia
  • 351
  • 1
  • 6
0

As far as I know, there is no way to bind these two values. The version code from gradle file will be generated during the build.

Mike
  • 2,547
  • 3
  • 16
  • 30
-1

You will have to create a String resource which will have the versionCode & then you can easily get it like @string/versionCode...

but you will have to update that string resource with the version code everytime you update your App...

Doing this programmatically will be more good & hassle free ;)

Darshan
  • 4,020
  • 2
  • 18
  • 49