Search code examples
android-studiogradlebuild.gradlegradle-kotlin-dsl

Error: Can't determine type for tag '<item name="APP_URL" type="String">"https://www.dummyurl_dev.com/"</item>' in build gradleResValues.xml


I tried to keep my app API URL in config.properties in the root folder of the project, and try to read in those value in module-level build.gradle file and assign it to resValue for global access in the application.

but, I am facing an issue. I keep the URL in a property file and try to execute the application based on the build variant, but the compiler will return an error. how to keep the value global?.
Thanks in advance.

Here is what I followed in build.gradle to read config.properties

def configProperties = new Properties()
configProperties.load(new FileInputStream(rootProject.file("config.properties")))

In buildTypes

buildTypes {
          rc {
              resValue("String", "APP_URL", configProperties.getProperty('RC_URL'))
             }
 }

In compile time will showing this error

Error: Build: failed

:uimodule:mergeDevResources

uimodule/build/generated/res/resValues/dev/values/gradleResValues.xml Can't determine type for tag '"https://www.dummyurl_dev.com/"'

Project-Root path: here i created separate property file config.properties

Access properties file in build.gradle: enter image description here

Compiler Error in Build Output enter image description here


Solution

  • Finally, I got the solution for this issue,

    Introduce new variable in buildTypes in module-level build.gradle, replace resValue into buildConfigField and pass the arguments like this "\"$appUrl\"" .Here I using 'RC' as build variant instead of 'dev' for this example.

    build.gradle: module level

    buildTypes {
             rc {
                  def appUrl = configProperties['RC_URL']
                  buildConfigField("String", "APP_URL", "\"$appUrl\"")
                  // following line made this error
                  //resValue("String", "APP_URL", configProperties.getProperty('RC_URL'))
                }
    }
    

    BuildConfig.java: for reference, I added my buildconfig file code here, which is actually done so for.

    public final class BuildConfig {
      public static final boolean DEBUG = Boolean.parseBoolean("true");
      public static final String APPLICATION_ID = "com.mohan.uimodule";
      public static final String BUILD_TYPE = "rc";
      public static final int VERSION_CODE = 1;
      public static final String VERSION_NAME = "1.0";
      
    
    //types of url which i have in config.properties
      public static final String DEV_URL = "https://www.dummyurl_dev.com/";
      public static final String PROD_URL = "https://www.dummyurl_prod.com/";
      public static final String RC_URL = "https://www.dummyurl_rc.com/";
    
    // Fields from build type: rc
      public static final String APP_URL = "https://www.dummyurl_rc.com/";
    }
    

    Solution: this is the expected result I got from buildconfig.java and this is my expectation too

    public static final String APP_URL = "https://www.dummyurl_rc.com/";
    

    Hope this answer will help. happy coding, Thanks :)