如何通过buildType和风味分裂源 [英] How to split source by buildType AND flavor

查看:93
本文介绍了如何通过buildType和风味分裂源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有对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"
        }
}

因此​​,有对包名:

So there are for package names:

com.example.app.flavor1
com.example.app.flavor1.debug
com.example.app.flavor2
com.example.app.flavor2.debug

com.example.app.flavor1 com.example.app.flavor1.debug com.example.app.flavor2 com.example.app.flavor2.debug

一切都很好用code。但由于修改后的包名字来取得的搞砸,而合并清单我需要手动为每个有效的软件包来设置一些东西像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.

但不要怎么办呢?
不管我把的Andr​​oidManifest.xml 的src /花果{1,2} 的src / {调试,发布} ,我结束了只有两种配置。
我试着之类的东西 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这个使用新的Andr​​oidManifest.xml中processManifest任务。现在的问题是:sourceSet.manifest.srcFile是只读的,我们不能仅仅将其装飞。由于我们使用不同的资源(调试和放大器;释放)。每一个味道,我们可以将新的Andr​​oidManifest.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>

您可以得到样品code在这里: https://github.com/ shakalaca / learning_gradle_android /树/主/ 07_tricks

You can get sample code here : https://github.com/shakalaca/learning_gradle_android/tree/master/07_tricks

它同时支持谷歌地图API v2和放大器; GCM的情况下,但我认为这是很容易轻微code变更后,支持内容提供商以及

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.

这篇关于如何通过buildType和风味分裂源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆