gpt4 book ai didi

android - 无法获取未知属性 'localProperties'

转载 作者:行者123 更新时间:2023-12-03 05:31:14 26 4
gpt4 key购买 nike

我想在 build.gradle 中读取 local.properties 中定义的属性(如 here 所述)
所以在根 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']
}
}

但同步后出现以下错误:

Could not get unknown property 'localProperties' for DefaultConfig_Decorated{name=main, ...} of type com.android.build.gradle.internal.dsl.DefaultConfig.



我哪里错了?如何解决这个问题呢?

最佳答案

我遇到了一些问题。变量 localProperties不是内置的,而是由您自己定义的。您可以添加

Properties localProperties = new Properties()
File file = project.rootProject.file('local.properties')
if (file.exists()) {
localProperties.load(file.newDataInputStream())
}
给你在 defaultConfig block 中的 build.gradle。

关于android - 无法获取未知属性 'localProperties',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59241018/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com