摇篮混合风味 [英] Gradle mixing build flavours
问题描述
是否可以在Gradle中混合"多种口味?
Is there a way of "mixing" multiple flavours in gradle?
例如,假设我有两种口味:Red
和Blue
.现在,让我说说我希望它们具有两种口味(如果您愿意,可以添加子口味):Complex
和Simple
.
For example, let's say I have two flavours: Red
and Blue
. Now let's say I have two flavours that I want these to have (sub-flavours, if you like): Complex
and Simple
.
到目前为止,我所知道的是,这将需要我创建四种风味:Red-Complex
,Red-Simple
,Blue-Complex
,Blue-Simple
.
What I know so far is that this would require me to create four flavours: Red-Complex
, Red-Simple
, Blue-Complex
, Blue-Simple
.
我不清楚在项目中应如何组织.如何定义在Red-Complex
和Red-Simple
风味之间共享的Red
代码,但又具有在Red-Complex
和Blue-Complex
之间共享的Complex
代码?我不想在风味之间复制/粘贴代码.
I'm unclear on how this should be structured within the project. How do I define Red
code that is shared between the Red-Complex
and Red-Simple
flavours, but also have the Complex
code that is shared between Red-Complex
and Blue-Complex
? I don't want to have to copy/paste code between the flavours.
在gradle中实现此目标的最佳方法是什么?
What's the best way to achieve this in gradle?
推荐答案
Here is how flavor works: https://proandroiddev.com/advanced-android-flavors-part-1-building-white-label-apps-on-android-ade16af23bcf
->在您的src文件夹中,您要创建2种口味,分别称为红色",蓝色".
-> In Your src folder you want to create 2 flavors called "red", "blue".
->在src中创建2个名为"red","blue"的文件夹(与主文件夹相同).
-> Create 2 folder named "red", "blue" in src (same level as main).
->文件夹的结构与主文件夹相同.
-> folder hasesame structure as main.
->在这里,您可以选择覆盖所有XML/资源/java文件/值等.
-> Here you have the option to override any XML/resources/java files/values etc.
->(在构建Flavour时,具有与main相同名称的资源).
-> (resources having the same name as main will override when you build a Flavour).
->使用build.gradle中的示例代码在项目中添加风味.
-> Use sample code in build.gradle to add flavor in the project.
productFlavors {
blue{
minSdkVersion 21
applicationId 'something'
targetSdkVersion 28
versionCode 1
versionName "1.0.0"
}
red
{
minSdkVersion 21
applicationId 'something'
targetSdkVersion 28
versionCode 1
versionName '1.0.0'
}
}
buildTypes {
Debug { }
Release { }
}
将提供构建版本:
blueDebug
blueRelease
redDebug
redRelease
//================向其中添加子口味===============
//================ Add sub flavors to it ================
此处: https://proandroiddev.com /advanced-android-flavors-part-2-enter-flavor-dimensions-4ad7f486f6
->具有子风味,例如简单",复杂"(即"buildTypes").
-> To have Sub-flavors like Simple, Complex ( In other words "buildTypes").
->对于子口味或说buildType,我们使用尺寸.
-> For Sub-Flavours or say buildTypes we use dimensions.
-> flavorDimensions风味",子风味"(在您的build.gradle中) //可以是任何关键字,在内部使用 //将此关键字视为组ID,该组ID将一组口味定义为一个组.
-> flavorDimensions "Flavour", "SubFlavour" (in your build.gradle) // Can be any keyword, use internally // think of this keyword as group id who define a collection of flavors as a group.
->在您的口味中使用它 ->我们可以认为关键字维度"更像是定义一组风味和亚风味
-> Use this in your flavors -> We can think Key word "dimension" is more of like defining a set of flavor and sub-flavor
flavorDimensions "Flavour", "SubFlavour"
productFlavors
{
// group one
Simple {
dimension "SubFlavour"
}
Complex {
dimension "SubFlavour"
}
//group two
// Take a note: do not use upper case in your flavours (one which match the folder names in src)
// Sub-Flavours can have Upper case
blue
{
dimension "Flavour"
minSdkVersion 21
applicationId 'something'
targetSdkVersion 28
versionCode 1
versionName "1.0.0"
}
red
{
dimension "Flavour"
minSdkVersion 21
applicationId 'something'
targetSdkVersion 28
versionCode 1
versionName '1.0.0'
}
}
->通过正确配置此选项,您将在下面看到buildVariants.
--> By configure this correctly you will see below buildVariants.
blueSimpleDebug
blueSimpleRelease
blueComplexDebug
blueComplexRelease
redSimpleDebug
redSimpleRelease
redComplexDebug
redComplexRelease
//================ build.gradle ================ //在这里,您的build.gradle可能看起来像这样.
//================= build.gradle ================ // Here your build.gradle might look like.
android {
buildTypes {
debug { }
release { }
// Any other you want to add here
// Ex, development {}
}
flavorDimensions "Flavour", "SubFlavour"
productFlavors {
Simple {
dimension "SubFlavour"
}
Complex {
dimension "SubFlavour"
}
Blue
{
dimension "Flavour"
minSdkVersion 21
applicationId 'something'
targetSdkVersion 28
versionCode 1
versionName "1.0.0"
}
Red
{
dimension "Flavour"
minSdkVersion 21
applicationId 'something'
targetSdkVersion 28
versionCode 1
versionName '1.0.0'
}
}
sourceSets {
main {
aidl.srcDirs = ['src/main/aidl', 'src/main/res/animation', 'src/main/res/animations']
res.srcDirs = [
'src/main/res/anim',
'src/main/res/layouts/xyz',
'src/main/res/layouts/abc',
'src/main/res/layouts',
'src/main/res/layout',
'src/main/res'
]
resources.srcDirs = ['src/main/resources', 'src/main/resources/xml']
manifest.srcFile 'src/main/AndroidManifest.xml'
java.srcDirs = ['src/main/java']
}
red {
java.srcDirs = ['src/red/java']
res.srcDirs = [
'src/red/res/layouts/xyz',
'src/red/res/layouts/abc',
'src/red/res/layouts',
'src/red/res'
]
assets.srcDirs = ['src/red/assets']
manifest.srcFile 'src/red/AndroidManifest.xml'
}
blue {
java.srcDirs = ['src/blue/java']
res.srcDirs = [
'src/blue/res/layouts/xyz',
'src/blue/res/layouts/abc',
'src/blue/res/layouts',
'src/blue/res'
]
assets.srcDirs = ['src/blue/assets']
manifest.srcFile 'src/blue/AndroidManifest.xml'
}
}
} //andorid
dependencies {
// yout dependencies
}
这篇关于摇篮混合风味的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!