AndroidGradle。如何将 Flavors 与 buildTypes 结合起来
Android Gradle. How to combine Flavors with buildTypes
我正在开发一个白牌应用程序。
我们为每个客户端创建了不同的风格,每个客户端都有 Debug
和 Production
API,因此我尝试在 Gradle.[=20= 中设置它们]
我应该怎么做?
这是我试过的方法:
buildTypes {
debug {
// some configurations
}
release {
// some configurations
}
}
flavorDimensions "client"
productFlavors {
company1{
dimension "client"
buildConfigField("String", "BASE_URL", "\"https://app.company1/devApi/\"")
}
company2 {
dimension "client"
buildConfigField("String", "BASE_URL", "\"https://app.company2/devApi/\"")
}
}
编辑:
我希望能够为每个 Flavor 和 Buildtype 定义不同的 BASE_URL
。
flavor company1, BuildType debug
https://app.company1.com/devApi/
风味公司1,BuildType发布
https://app.company1.com/prodApi/
flavor company2, BuildType debug
https://dev.company2.com/api/
风味公司2,BuildType发布
https://prod.company2.com/api/
您可以使用 flavors 为您的应用程序添加基本配置,范围从 app url
、API keys
、master password
等
flavorDimensions "Mobile"
productFlavors {
Production {
dimension "Mobile" // dimension can be mobile, kiosks, tv, miniKiosks etc
resValue "string", "API_KEY", "Just to give the idea"
resValue "string", "SERVICE_IP", "Your service IP"
resValue "string", "SERVICE_BASE_URL", ""
resValue "string", "APK_BASE_URL", "base url"
resValue "string", "MASTER_PASSWORD", ""
}
Demo {
dimension "Mobile"
resValue "string", "API_KEY", "Just to give the idea"
resValue "string", "SERVICE_IP", "Your service IP"
resValue "string", "SERVICE_BASE_URL", "services/v1/"
resValue "string", "APK_BASE_URL", "base url"
resValue "string", "MASTER_PASSWORD", ""
}
Local {
dimension "Mobile"
resValue "string", "API_KEY", ""
// resValue "string", "app_name", ""
resValue "string", "SERVICE_IP", ""
// resValue "string", "SERVICE_IP", ""
resValue "string", "SERVICE_BASE_URL", ""
resValue "string", "APK_BASE_URL", ""
resValue "string", "MASTER_PASSWORD", "a"
}
}
现在,如果您查看 build varients
,您将得到如下信息:
这是工作示例
// 指定一个风味维度。
flavorDimensions "version"
productFlavors {
dev {
dimension "version"
applicationId "com.example.lk"
versionCode 1
versionName "1.0.1"
buildConfigField("String", "BASE_URL_PATH", "\"https://app.dev.com/api/v1/\"")
}
prod {
dimension "version"
applicationId "com.example.in"
versionCode 1
versionName "1.0.1"
buildConfigField("String", "BASE_URL_PATH", "\"https://app.prod.com/api/v1/\"")
}
staging {
dimension "version"
applicationId "com.example.other"
versionCode 1
versionName "1.0.1"
buildConfigField("String", "BASE_URL_PATH", "\"https://app.staging.com/api/v1/\"")
}}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
你可以这样访问它
String baseURL =BuildConfig.BASE_URL_PATH;
你的主要问题是你没有正确地放置你的 buildTypes for flavors 并且在每个公司内部没有正确的参数而且我建议你阅读更多关于 Gradle setups(developer.android).
buildTypes {
debug {
// some configurations
}
release {
// some configurations
}
}
flavorDimensions "version", "brand"
productFlavors {
dev {
versionName += "dev"
dimension "version"
buildConfigField "String", "BASE_API_URL", "\...\""
}
prod {
dimension "version"
buildConfigField "String", "BASE_API_URL", "\...\""
}
company1{
dimension "brand"
versionName "1.0.0"
buildConfigField("int", "CLONE_ID", "1")
**here you can set some params, for current clone id: examlpe ->** buildConfigField("boolean", "SHOW_CREDIT_BUY_IN_PROFILE", "true")
}
company2 {
dimension "brand"
versionName "1.0.0"
buildConfigField("int", "CLONE_ID", "2")
**here you can set some params, for current clone id: examlpe ->** buildConfigField("boolean", "SHOW_CREDIT_BUY_IN_PROFILE", "false")
}
尝试这样的事情:
buildTypes {
debug {
buildConfigField("String", "BASE_URL_PATH", "\"devApi/\"")
}
release {
buildConfigField("String", "BASE_URL_PATH", "\"prodApi/\"")
}
}
flavorDimensions "client"
productFlavors {
company1{
dimension "client"
buildConfigField("String", "BASE_URL_DOMAIN", "\"https://app.company1/\"")
}
company2 {
dimension "client"
buildConfigField("String", "BASE_URL_DOMAIN", "\"https://app.company2/\"")
}
}
并像这样使用它:
String BASE_URL = BuildConfig.BASE_URL_DOMAIN +
BuildConfig.BASE_URL_PATH
对于我的具体问题,URL 之间的差异很大,我无法使其与 Flavors 和 BuildTypes 一起使用。
我能够通过为每个构建变体(这是风格和构建类型的每个组合)使用特定的 strings.xml
来定义 debug/production URL:
这些是这样做的文件夹结构:
src/flavour1Debug/res/values/strings.xml
src/flavour1Release/res/values/strings.xml
和
src/flavour2Debug/res/values/strings.xml
src/flavour2Release/res/values/strings.xml
额外:
这也可用于托管不同的 google-services.json
文件
我正在开发一个白牌应用程序。
我们为每个客户端创建了不同的风格,每个客户端都有 Debug
和 Production
API,因此我尝试在 Gradle.[=20= 中设置它们]
我应该怎么做?
这是我试过的方法:
buildTypes {
debug {
// some configurations
}
release {
// some configurations
}
}
flavorDimensions "client"
productFlavors {
company1{
dimension "client"
buildConfigField("String", "BASE_URL", "\"https://app.company1/devApi/\"")
}
company2 {
dimension "client"
buildConfigField("String", "BASE_URL", "\"https://app.company2/devApi/\"")
}
}
编辑:
我希望能够为每个 Flavor 和 Buildtype 定义不同的 BASE_URL
。
flavor company1, BuildType debug
https://app.company1.com/devApi/
风味公司1,BuildType发布
https://app.company1.com/prodApi/
flavor company2, BuildType debug
https://dev.company2.com/api/
风味公司2,BuildType发布
https://prod.company2.com/api/
您可以使用 flavors 为您的应用程序添加基本配置,范围从 app url
、API keys
、master password
等
flavorDimensions "Mobile"
productFlavors {
Production {
dimension "Mobile" // dimension can be mobile, kiosks, tv, miniKiosks etc
resValue "string", "API_KEY", "Just to give the idea"
resValue "string", "SERVICE_IP", "Your service IP"
resValue "string", "SERVICE_BASE_URL", ""
resValue "string", "APK_BASE_URL", "base url"
resValue "string", "MASTER_PASSWORD", ""
}
Demo {
dimension "Mobile"
resValue "string", "API_KEY", "Just to give the idea"
resValue "string", "SERVICE_IP", "Your service IP"
resValue "string", "SERVICE_BASE_URL", "services/v1/"
resValue "string", "APK_BASE_URL", "base url"
resValue "string", "MASTER_PASSWORD", ""
}
Local {
dimension "Mobile"
resValue "string", "API_KEY", ""
// resValue "string", "app_name", ""
resValue "string", "SERVICE_IP", ""
// resValue "string", "SERVICE_IP", ""
resValue "string", "SERVICE_BASE_URL", ""
resValue "string", "APK_BASE_URL", ""
resValue "string", "MASTER_PASSWORD", "a"
}
}
现在,如果您查看 build varients
,您将得到如下信息:
这是工作示例
// 指定一个风味维度。
flavorDimensions "version"
productFlavors {
dev {
dimension "version"
applicationId "com.example.lk"
versionCode 1
versionName "1.0.1"
buildConfigField("String", "BASE_URL_PATH", "\"https://app.dev.com/api/v1/\"")
}
prod {
dimension "version"
applicationId "com.example.in"
versionCode 1
versionName "1.0.1"
buildConfigField("String", "BASE_URL_PATH", "\"https://app.prod.com/api/v1/\"")
}
staging {
dimension "version"
applicationId "com.example.other"
versionCode 1
versionName "1.0.1"
buildConfigField("String", "BASE_URL_PATH", "\"https://app.staging.com/api/v1/\"")
}}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
你可以这样访问它
String baseURL =BuildConfig.BASE_URL_PATH;
你的主要问题是你没有正确地放置你的 buildTypes for flavors 并且在每个公司内部没有正确的参数而且我建议你阅读更多关于 Gradle setups(developer.android).
buildTypes {
debug {
// some configurations
}
release {
// some configurations
}
}
flavorDimensions "version", "brand"
productFlavors {
dev {
versionName += "dev"
dimension "version"
buildConfigField "String", "BASE_API_URL", "\...\""
}
prod {
dimension "version"
buildConfigField "String", "BASE_API_URL", "\...\""
}
company1{
dimension "brand"
versionName "1.0.0"
buildConfigField("int", "CLONE_ID", "1")
**here you can set some params, for current clone id: examlpe ->** buildConfigField("boolean", "SHOW_CREDIT_BUY_IN_PROFILE", "true")
}
company2 {
dimension "brand"
versionName "1.0.0"
buildConfigField("int", "CLONE_ID", "2")
**here you can set some params, for current clone id: examlpe ->** buildConfigField("boolean", "SHOW_CREDIT_BUY_IN_PROFILE", "false")
}
尝试这样的事情:
buildTypes {
debug {
buildConfigField("String", "BASE_URL_PATH", "\"devApi/\"")
}
release {
buildConfigField("String", "BASE_URL_PATH", "\"prodApi/\"")
}
}
flavorDimensions "client"
productFlavors {
company1{
dimension "client"
buildConfigField("String", "BASE_URL_DOMAIN", "\"https://app.company1/\"")
}
company2 {
dimension "client"
buildConfigField("String", "BASE_URL_DOMAIN", "\"https://app.company2/\"")
}
}
并像这样使用它:
String BASE_URL = BuildConfig.BASE_URL_DOMAIN + BuildConfig.BASE_URL_PATH
对于我的具体问题,URL 之间的差异很大,我无法使其与 Flavors 和 BuildTypes 一起使用。
我能够通过为每个构建变体(这是风格和构建类型的每个组合)使用特定的 strings.xml
来定义 debug/production URL:
这些是这样做的文件夹结构:
src/flavour1Debug/res/values/strings.xml
src/flavour1Release/res/values/strings.xml
和
src/flavour2Debug/res/values/strings.xml
src/flavour2Release/res/values/strings.xml
额外:
这也可用于托管不同的 google-services.json
文件