How to parse XML with XMLParser to obtain a string resource?

27 views Asked by At

I am creating different product flavours for my jetpack compose application.

    flavorDimensions += "environment"
    productFlavors {
        create("production") {
            dimension = "environment"
            applicationIdSuffix = ".prod"
            buildConfigField("String", "API_BASE_URL", "\"https://api.prod.example.com/\"")
        }

        create("development") {
            dimension = "environment"
            applicationIdSuffix = ".dev"
            var configXml = XmlParser().parse(project.file("src/production/res/xml/prod_config.xml"))
            buildConfigField("String", "API_BASE_URL", "\"https://api.dev.example.com/\"")
        }
    }

However, instead of placing my api_base_url value directly in my build.gradle file, i want to access them in respective config.xml files in the different source sets following the flavours.

My XML file is as such

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="api_base_url">https://api.prod.example.com</string>
</resources>

How do i use XMLParser or XMLSlurper to parse the xml file to obtain the base api url values?

0

There are 0 answers