gpt4 book ai didi

java - CI 中的 Android 配置值

转载 作者:行者123 更新时间:2023-12-02 13:28:21 25 4
gpt4 key购买 nike

我是 Android 新手,但已经在考虑是否有办法在持续集成中包含应用程序的一些重要配置。我们使用 Visual Studio Online 进行构建和发布。例子 。目前我们有 Constants.java 文件,它存储一些 imp 配置值,例如服务器 URL,这些值对于部署在开发、测试 uat 生产等应用程序上的每个环境都会有所不同,因此目前需要在代码进入 vso 之前手动检查该代码用于构建和发布。 。是否可以以某种方式在环境的持续集成中配置它并从那里进行选择,以便不需要手动检查..

最佳答案

看这个:

看看这个函数readApiKey(),它从环境或.properties 文件读取 key 。然后看这一行:

buildConfigField("String", "API_PUBLIC_KEY", readApiKey(KeyType.PUBLIC_KEY)) 它使用值加载字符串资源。

如果您有具有不同值的不同构建,这就是 flavor 的用武之地。您可以阅读 here.

apply plugin: 'com.android.application'
apply from: '../codequality/quality.gradle'

/*
* Read API key from CI server or from keys.properties file
*/
enum KeyType {
PUBLIC_KEY,
PRIVATE_KEY
}

def readApiKey(KeyType type) {
String apiKey = ""
if (System.getenv("CIENV")) {
if (type == KeyType.PUBLIC_KEY) {
apiKey = System.getenv("PB_KEY")
} else apiKey = System.getenv("PR_KEY")
} else {
Properties props = new Properties()
try {
props.load(new FileInputStream(new File(getRootDir().getAbsolutePath() + "/keys.properties")))
if (type == KeyType.PUBLIC_KEY) {
apiKey = props.getProperty("api_pb_key")
} else apiKey = props.getProperty("api_pr_key")
} catch (FileNotFoundException fnfe) {
println("Please create a keys.properties file in the root directory of the project")
}
}
return "\"$apiKey\"";
}

println 'PUBLIC KEY Log' + readApiKey(KeyType.PUBLIC_KEY)
println 'Private Key Log' + readApiKey(KeyType.PRIVATE_KEY)

android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion

dataBinding {
enabled true
}

testOptions {
unitTests.returnDefaultValues = true
}

defaultConfig {
applicationId "co.tonespy.dummarvel"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

// Setting up picked up data to String resource
buildConfigField("String", "API_PUBLIC_KEY", readApiKey(KeyType.PUBLIC_KEY))
buildConfigField("String", "API_PRIVATE_KEY", readApiKey(KeyType.PRIVATE_KEY))
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])

// support dependencies
....

// test dependencies
testCompile "junit:junit:$jUnitVersion"
androidTestCompile("com.android.support.test.espresso:espresso-core:$espressoCoreVersion", {
exclude group: 'com.android.support', module: 'support-annotations'
})
testCompile "org.mockito:mockito-core:$mockitoVersion"
compile "com.android.support:support-v4:$supportLibraryVersion"
}

关于java - CI 中的 Android 配置值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43291940/

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