android - 无法获取未知属性“localProperties”
问题描述
我想在build.gradle中读取local.properties中定义的属性(如此处所述)所以在根build.gradle文件中我有以下内容:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
def localProperties = new Properties()
if (project.rootProject.file('local.properties').canRead()) {
localProperties.load(project.rootProject.file("local.properties").newDataInputStream())
}
buildscript {
ext {
...
}
repositories {
google()
jcenter()
maven {
url "https://maven.google.com"
}
}
dependencies {
classpath "com.android.tools.build:gradle:$gradleVersion"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
maven {
url "https://maven.google.com"
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
在模块的build.gradle文件中,我有以下内容:
android {
...
defaultConfig {
...
buildConfigField "String", "TOKEN", localProperties['token']
}
}
但同步后出现以下错误:
无法获取 com.android.build.gradle.internal.dsl.DefaultConfig 类型的 DefaultConfig_Decorated{name=main, ...} 的未知属性“localProperties”。
我哪里错了?如何解决这个问题呢?
解决方案
我遇到了一些问题。变量localProperties
不是内置的,而是您自己定义的。您可以添加
Properties localProperties = new Properties()
File file = project.rootProject.file('local.properties')
if (file.exists()) {
localProperties.load(file.newDataInputStream())
}
给你在 defaultConfig 块中的 build.gradle。
推荐阅读
- shell - 如何防止我的脚本自行符号链接?
- c# - 如何为 Unity Shader 实现简单的高度图
- c# - 如何从 Docker 日志中的 ASP.Net MVC 应用程序获取日志
- javascript - 更改用于分隔具有相同名称标识符的表单输入字段的字符?
- django - 动态更新 form_list 以指向某个步骤的某个子类(仅在运行时已知),并呈现该表单
- python - 通过 Numpy/Pandas 使用 (nx 1) 数据创建多项式的 anxm 数组
- sql - 如何两次输出同一列,受具有 COUNT 函数的不同 WHERE 子句的影响
- modelica - OpenModelica 中 StopTime 过高时 FFT 的仿真结果错误或没有
- django - Django 新创建的实例 - DateTime 字段类型为“str”
- php - 具有两个字段的数组上的 Laravel 验证存在于数据库行上