我有一些外部库,它们是不同的版本,以匹配我所有的构建变体。在这个例子中,我有同一个库的4个不同版本。我不知道如何在我的构建中做到这一点。gradle文件。我有两种不同的风格,结合了发布和调试。在下面的示例中,注释掉的代码可以工作,而其他代码是我试图使其工作的。
android {
buildTypes {
release {
minifyEnabled true
}
debug {
minifyEnabled false
}
}
flavorDimensions "base"
productFlavors {
flavor1 {
dimension "base"
applicationIdSuffix ".flavor1"
}
flavor2 {
dimension "base"
applicationIdSuffix ".flavor2"
}
}
}
dependencies {
// this works but cannot specify the build type
flavor1Implementation files('libs/mylib-release.aar')
// this combination of buildType+Flavor is not working
// flavor1ReleaseImplementation files('libs/mylib-flavor1-release.aar')
// flavor1DebugImplementation files('libs/mylib-flavor1-debug.aar')
// this works but cannot specify the build type
flavor2Implementation files('libs/mylib-flavor2-release.aar')
// flavor2ReleaseImplementation files('libs/mylib-flavor2-release.aar')
// flavor2DebugImplementation files('libs/mylib-flavor2-debug.aar')
}
将以下代码添加到构建中。为依赖项配置初始化占位符的Gradle文件
// Initializes a placeholder for these dependency configurations
configurations {
flavor1DebugImplementation {}
flavor1ReleaseImplementation {}
flavor2DebugImplementation {}
flavor2ReleaseImplementation {}
}
build.gradle
中的完整解决方案示例android {
buildTypes {
release {
minifyEnabled true
}
debug {
minifyEnabled false
}
}
flavorDimensions "base"
productFlavors {
flavor1 {
dimension "base"
applicationIdSuffix ".flavor1"
}
flavor2 {
dimension "base"
applicationIdSuffix ".flavor2"
}
}
}
// Initializes a placeholder for these dependency configurations
configurations {
flavor1DebugImplementation {}
flavor1ReleaseImplementation {}
flavor2DebugImplementation {}
flavor2ReleaseImplementation {}
}
dependencies {
//flavor1Implementation files('libs/mylib-release.aar')
flavor1ReleaseImplementation files('libs/mylib-flavor1-release.aar')
flavor1DebugImplementation files('libs/mylib-flavor1-debug.aar')
//flavor2Implementation files('libs/mylib-flavor2-release.aar')
flavor2ReleaseImplementation files('libs/mylib-flavor2-release.aar')
flavor2DebugImplementation files('libs/mylib-flavor2-debug.aar')
}
这种方式将在多模块项目中工作
创建一个构建gradle文件(例如flavor_config.gradle)并在其中定义如下配置
android {
flavorDimensions 'resource_type'
productFlavors {
create("flavor1") {
dimension 'resource_type'
versionName "$app_version_name - flavor1"
}
create("flavor2") {
dimension 'resource_type'
versionName "$app_version_name - flavor2"
}
}
}
并将这个gradle文件应用到你想要的每个模块中,例如app模块或功能模块,像这样:
apply from: rootProject.file("flavor_config.gradle")
在同步项目之后,您可以像这样访问每种风格的具体实现:
flavor1Implementation("flavor1Library")
flavor2Implementation("flavor2Library")