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.

4

3 Answers 3

1
try {
    PackageInfo pInfo = context.getPackageManager().getPackageInfo(getPackageName(), 0);
    String version = pInfo.versionName;
} catch (PackageManager.NameNotFoundException e) {
    e.printStackTrace();
}
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.

-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 ;)

6
  • versionCode is not a string
    – Tim
    Commented Sep 3, 2018 at 9:29
  • 1
    @TimCastelijns did you read the answer? I said you will have to create one string variable with version code as value!
    – darshan
    Commented Sep 3, 2018 at 9:32
  • 1
    please tell me why would you put a variable that does not represent a string in strings.xml?
    – Tim
    Commented Sep 3, 2018 at 9:33
  • @TimCastelijns <string name="version_code">214</string>
    – darshan
    Commented Sep 3, 2018 at 9:35
  • I said why, not how
    – Tim
    Commented Sep 3, 2018 at 9:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.