如何按构建类型和风味拆分源 [英] How to split source by buildType AND flavor
问题描述
对于 buildTypes 调试/发布以及两个 productFlavors,我有一个具有不同包名称的应用程序.
I have an app with different package name for the buildTypes debug/release as well as for two productFlavors.
我的 build.gradle 中有趣的部分如下所示:
The interesting part of my build.gradle looks like this:
android {
buildTypes {
debug {
packageNameSuffix ".debug"
}
release {
signingConfig signingConfigs.release
}
}
productFlavors {
flavor1 {
packageName "com.example.app.flavor1"
}
flavor2 {
packageName "com.example.app.flavor2"
}
}
所以有包名:
com.example.app.flavor1com.example.app.flavor1.debugcom.example.app.flavor2com.example.app.flavor2.debug
com.example.app.flavor1 com.example.app.flavor1.debug com.example.app.flavor2 com.example.app.flavor2.debug
代码一切正常.但是因为在合并清单时修改的包名称搞砸了,我需要为每个有效的包手动设置一些诸如 GCM 权限和内容提供者权限之类的东西.
Everything is fine with the code. But because the modified package name get's messed up while merging the manifest I need to set some things like GCM permissions and content providers authority by hand for each valid package.
但是它是怎么做到的呢?无论我将 AndroidManifest.xml
放入 src/falvor{1,2}
还是 src/{debug,release}
,我最终只有两种配置.我尝试了诸如 flavor1Debug
之类的东西,但没有运气.
But how do it do it?
No matter if I put the AndroidManifest.xml
into src/falvor{1,2}
or in src/{debug,release}
, I end up with only two configurations.
I tried things like flavor1Debug
without luck.
推荐答案
没有官方的方法来设置'flavorBuildType'(如flavor1Debug)组合的资源文件,所以你必须在这里做一些小技巧.
There's no official way to set resource files of the 'flavorBuildType' (like flavor1Debug) combination, so you have to do a little hack here.
首先定义AndroidManifest.xml的新路径:
First define the new path of AndroidManifest.xml:
project.ext.flavor1 = [
debugManifest: 'src/flavor1Debug/AndroidManifest.xml',
releaseManifest: 'src/flavor1Release/AndroidManifest.xml'
]
project.ext.flavor2 = [
debugManifest: 'src/flavor2Debug/AndroidManifest.xml',
releaseManifest: 'src/flavor2Release/AndroidManifest.xml'
]
第二,告诉 gradle 在 processManifest 任务中使用新的 AndroidManifest.xml.现在的问题是:sourceSet.manifest.srcFile 是只读的,我们不能直接替换它.由于我们为每个风味使用单独的资源(调试和发布),我们可以将新的 AndroidManifest.xml 复制到原始风味文件夹,gradle 将构建具有正确设置的 APK 文件.
Second, tell gradle to use the new AndroidManifest.xml in processManifest task. Now the problem is: sourceSet.manifest.srcFile is read-only, we can't just replace it on-fly. Since we're using separate resource (debug & release) for each flavor, we can copy the new AndroidManifest.xml to the origin flavor folder and gradle will build the APK file with right settings.
android.applicationVariants.all { variant ->
variant.processManifest.doFirst {
if (project.ext.has(variant.productFlavors.name)) {
if (project.ext[variant.productFlavors.name].debugManifest != null &&
project.ext[variant.productFlavors.name].releaseManifest != null) {
def manifestDirectory = android.sourceSets[variant.productFlavors.name].manifest.srcFile.parentFile
if (variant.buildType.name.equals("debug")) {
copy {
from project.ext[variant.productFlavors.name].debugManifest
into manifestDirectory
}
} else if (variant.buildType.name.equals("release")) {
copy {
from project.ext[variant.productFlavors.name].releaseManifest
into manifestDirectory
}
}
}
}
}
variant.processManifest.doLast {
if (project.ext.has(variant.productFlavors.name)) {
project.delete android.sourceSets[variant.productFlavors.name].manifest.srcFile
}
}
}
对于 AndroidManifest.xml,只需将 GCM 相关设置留在那里:
And for the AndroidManifest.xml, just leave the GCM related setting there:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.example.free.debug"
android:versionCode="1"
android:versionName="1.0" >
<permission
android:name="com.android.example.free.debug.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.android.example.free.debug.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
</manifest>
您可以在此处获取示例代码:https://github.com/shakalaca/learning_gradle_android/tree/master/07_技巧
You can get sample code here : https://github.com/shakalaca/learning_gradle_android/tree/master/07_tricks
它同时支持 Google Maps API v2 &GCM 案例,但我认为在轻微更改代码后也很容易支持内容提供者.
It supports both Google Maps API v2 & GCM cases, but I think it is easy to support content providers as well after minor code change.
这篇关于如何按构建类型和风味拆分源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!