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

标签 android-studio gradle build.gradle gradle-kotlin-dsl

我试图将我的应用 API URL 保留在 中配置属性在项目的根文件夹中,并尝试在 中读取这些值模块级 build.gradle 文件并将其分配给 resValue 用于应用程序中的全局访问。
但是,我面临一个问题。我将 URL 保存在属性文件中并尝试基于构建变体执行应用程序,但编译器将返回错误。如何保持全局值(value)?
提前致谢。
这是我在 中关注的内容build.gradle 阅读配置属性

def configProperties = new Properties()
configProperties.load(new FileInputStream(rootProject.file("config.properties")))
构建类型
buildTypes {
          rc {
              resValue("String", "APP_URL", configProperties.getProperty('RC_URL'))
             }
 }
在编译时会显示这个错误
错误:
构建:失败
:uimodule:mergeDevResources
uimodule/build/generated/res/resValues/dev/values/gradleResValues.xml
无法确定标签“https://www.dummyurl_dev.com/”的类型
项目根路径:
here i created separate property file config.properties
访问 build.gradle 中的属性文件:
enter image description here
构建输出中的编译器错误
enter image description here

最佳答案

最后,我得到了这个问题的解决方案,
在模块级 build.gradle 的 buildTypes 中引入新变量,将 resValue 替换为 buildConfigField 并像这样传递参数 "\"$appUrl\"" .这里我用 'RC' 作为构建变体而不是 '开发' 对于这个例子。
build.gradle: 模块级

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:
作为引用,我在这里添加了我的 buildconfig 文件代码,实际上是这样做的。
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/";
}
解决方案:
这是我从 得到的预期结果buildconfig.java 这也是我的期望
public static final String APP_URL = "https://www.dummyurl_rc.com/";
希望这个答案会有所帮助。编码愉快,谢谢:)

关于android-studio - 错误 : Can't determine type for tag '<item name="APP_URL"type ="String" >"https://www.dummyurl_dev.com/"</item>' in build gradleResValues. xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67000811/

相关文章:

android - 无法合并dex Android

android - build 项目 : outOfMemory (Android studio 3. 4)

android - Kotlin 中的 generateStubs 配置是什么?

android - 无法在 gradle 文件中应用 'android-reporting'

java - 我不能在泛型中使用继承类代替母类

android - 找不到 Gradle DSL 方法 : 'deleteAllActions()'

android - Gradle + AndroidAnnotations 生成重复的类错误 - 需要在每次构建之前清理项目

android - 具有相同包但具有不同签名的应用程序

android - gradle 为 keystore 文件设置绝对路径值

android - 混合版本可能导致运行时崩溃