所以我试图在我的 Kotlin 代码中访问一个 token key ,但它不会让我导入 BuildConfig。
在我的 main() 中,我有:
val client: DiscordClient = DiscordClientBuilder(BuildConfig.TOKEN_KEY).build()
BuildConfig
在 IntelliJ 中是红色的。我的
build.gradle
如下:defaultConfig {
buildConfigField("String", "TOKEN_KEY", apikeyProperties['TOKEN_KEY'])
}
我到处都看到他们希望 defaultConfig 位于
android {}
中的 stackoverflow 帖子,但我不是在制作 Android 应用程序。我的 token key 在一个名为
apikey.properties
的文件中在已添加到 .gitignore
的根目录中.文件内容:
TOKEN_KEY="token_value"
最佳答案
BuildConfig
是由 Android Gradle 插件生成的类,因此它不是 Gradle 标准功能集的一部分。
At build time, Gradle generates the
BuildConfig
class so your app code can inspect information about the current build.
(来自:Share custom fields and resource values with your app's code)
由于您没有使用 Android 插件,因此不会生成该类并且在 IJ 中为红色。
您有以下选择:
关于gradle - 无法在 gradle 中配置属性文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59652005/