简体   繁体   English

如果 Android Gradle Build 中不存在默认属性文件,我该如何创建它

[英]How can I create a default properties file if it doesn't exist in Android Gradle Build

Currently, my android project loads two parameters from a local properties files to populate some Build.Config constants.目前,我的 android 项目从本地属性文件加载两个参数来填充一些Build.Config常量。 The purpose of having the separate local.properties file is to keep it out of source control.拥有单独的local.properties文件的目的是使其不受源代码控制。 (this file is ignored by git). (此文件被 git 忽略)。 The values in it are of no value to production builds and may be frequently changed by a developer.其中的值对生产构建没有价值,开发人员可能会经常更改。 I don't want a change of these values to constitute a change in build.gradle .我不希望这些值的变化构成build.gradle的变化。 I also never want it to change just because a developer checks out a different git branch.我也不希望它仅仅因为开发人员签出不同的 git 分支而改变。

My problem is that since this property file is not in source control, a fresh clone and checkout will fail to build because the file does not exist.我的问题是,由于此属性文件不在源代码管理中,新的克隆和检出将无法构建,因为该文件不存在。 In the case where the file does not exist, I want the script to create it and save default parameters to it.在文件不存在的情况下,我希望脚本创建它并将默认参数保存到它。

ERROR: C:\\Users\\Me\\AndroidStudioProjects\\MyAwesomeApp\\app\\local.properties (The system cannot find the file specified)错误:C:\\Users\\Me\\AndroidStudioProjects\\MyAwesomeApp\\app\\local.properties(系统找不到指定的文件)

My current build.gradle which reads from a properties file:我当前的build.gradle从属性文件中读取:

Properties properties = new Properties()
properties.load(project.file('local.properties').newDataInputStream())
def spoofVin = properties.getProperty('spoof.vin', '12345678901234567')
def spoofId = properties.getProperty('spoof.id', '999999999999')
buildConfigField("String", "SPOOF_VIN", '"' + spoofVin + '"')
buildConfigField("String", "SPOOF_ID", '"' + spoofId + '"')

Example app/local.properties file:示例app/local.properties文件:

#Change these variables to spoof different IDs and VINs. Don't commit this file to source control.
#Wed Nov 18 12:13:30 CST 2020
spoof.id=999999999999
spoof.vin=12345678901234567

I'm posting my own solution below, to hopefully help someone else out if they have the same needs.我在下面发布了我自己的解决方案,希望可以帮助其他有相同需求的人。 I'm not a Gradle pro, so if you know an even better way to do this, post your solution.我不是 Gradle 专家,因此如果您知道更好的方法,请发布您的解决方案。

The following code is what I've found works to accomplish this task.以下代码是我发现可以完成此任务的代码。 The properties.store method conveniently lets me add a string comment to the top of the properties.local file too. properties.store方法也方便地让我在 properties.local 文件的顶部添加字符串注释。

//The following are defaults for new clones of the project.
//To change the spoof parameters, edit local.properties
def defaultSpoofVin = '12345678901234567'
def defaultSpoofId = '999999999999'
def spoofVinKey = 'spoof.vin'
def spoofIdKey = 'spoof.id'

Properties properties = new Properties()
File propertiesFile = project.file('local.properties')
if (!propertiesFile.exists()) {
    //Create a default properties file
    properties.setProperty(spoofVinKey, defaultSpoofVin)
    properties.setProperty(spoofIdKey, defaultSpoofId)

    Writer writer = new FileWriter(propertiesFile, false)
    properties.store(writer, "Change these variables to spoof different IDs and VINs. Don't commit this file to source control.")
    writer.close()
}

properties.load(propertiesFile.newDataInputStream())
def spoofVin = properties.getProperty(spoofVinKey, defaultSpoofVin)
def spoofId = properties.getProperty(spoofIdKey, defaultSpoofId)
buildConfigField("String", "SPOOF_VIN", '"' + spoofVin + '"')
buildConfigField("String", "SPOOF_ID", '"' + spoofId + '"')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用gradle实验性android模型插件进行Ndk构建会导致java.lang.IllegalStateException:名称为[DEFAULT]的FirebaseApp不存在。 - Ndk build with gradle experimental android model plugin causes java.lang.IllegalStateException: FirebaseApp with name [DEFAULT] doesn't exist. Android如果不存在python,如何创建一个新的json文件 - Android How to create a new json file if it doesn't exist python 如何使用build.gradle创建AAR文件 - How can I create aar file using build.gradle Gradle构建-Aapt不存在 - Gradle build - aapt doesn't exist Gradle警告我的build.gradle文件中不存在配置 - Gradle warns about a configuration that doesn't exist in my build.gradle file android gradle本地路径不存在 - android gradle Local path doesn't exist Android Studio Gradle Build失败 - 要求提供不存在的sdk版本 - Android Studio Gradle Build failing - asking for a sdk version that doesn't exist 电容器(build.gradle 不存在/不允许操作)React app to Android 无法构建 - Capacitor (build.gradle does not exist/operation not permitted) React app to Android can't build Gradle Android Maven插件不会自动创建pom文件 - Gradle Android Maven Plugin doesn't create the pom file automatically Android的Gradle不使用外部库构建 - Gradle for Android doesn't build with external library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM