是否有可能针对不同的产品口味有不同的build.gradles? [英] Is it possible to have different build.gradles for different product flavors?

查看:139
本文介绍了是否有可能针对不同的产品口味有不同的build.gradles?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的项目是为基于Amazon的设备和Android构建的。 95%的代码基数在这两者之间是相同的。因此,我不想将这两个项目作为不同的项目,而是考虑使用产品口味将这两者结合在一起(请让我知道是否可以有其他更好的解决方案)



但我在这里面临的问题之一是build.gradle中的buildscript和android属性,我需要为Amazon和Android使用不同的值。
例如,我需要这个for Android



依赖关系{
classpath'com.android.tools.build:gradle :1.1.3'
}



这个适用于亚马逊



依赖关系{
classpath'com.amazon.device.tools.build:gradle:1.0.0'
}



同样也适用于compileSdkVersion和buildToolsVersion。



如果我需要为每个build.gradles设置两个不同的build.gradles,我应该如何让他们根据建立的味道来挑选? (我不确定是否真的有可能)



如果没有,是否有更好的解决方案通过将所有内容放在同一个项目中来解决这个问题。

解决方案

它应该是一个评论,但它如果它不提供帮助,我将删除它。



据我所知,你不能有不同的 build.gradle 用于产品风味。

但是,您应该可以调整 buildscript compileSdkVersion



它不是很干净,但可以使用类似于:

  buildscript {
repositories {
jcenter()
mavenCentral()
}
if(condition){
dependencies {
classpath'com.android.tools.build:gradle:1.3.0+'
}
} else if(condition){
dependencies {
C lasspath'com.amazon.device.tools.build:gradle:1.1.3'
}
}
}

android {
if(condition ){
compileSdkVersion 23
} else if(condition){
compileSdkVersionAmazon.com:Amazon Fire Phone SDK Addon:XX
}
// .. 。
}


The project that I am working is built for both Amazon based devices as well as Android. Almost of 95% of the code base is common between these two. So instead of making these two as different projects, I thought of putting these two together using product flavors.(Please let me know if there can be other better solution)

But one of the problem I am facing here is with the buildscript and android properties in build.gradle, where I will need different values for Amazon and Android. For example, I need this for Android

dependencies { classpath 'com.android.tools.build:gradle:1.1.3' }

and this for Amazon

dependencies { classpath 'com.amazon.device.tools.build:gradle:1.0.0' }

Similarly for compileSdkVersion and buildToolsVersion as well.

If I need to have two different build.gradles for each, how should I let one of them get picked based on the build flavor? ( I am not sure if it is really possible)

If not, is there really a better possible solution to fix this problem by placing everything in the same project.

Thanks in advance.

解决方案

It should be a comment, but it is too long.
I will delete it if it will not provide an help.

As I know you can't have different build.gradle for product flavors.
However you should be able to condition the buildscript and the compileSdkVersion.

It is not so clean but you can use something like:

buildscript {
    repositories {
        jcenter()
        mavenCentral()
    }
    if (condition) {
        dependencies {
            classpath 'com.android.tools.build:gradle:1.3.0+'
        }
    } else if (condition) {
        dependencies {
            classpath 'com.amazon.device.tools.build:gradle:1.1.3'
        }
    }
}

android {
    if (condition) {
        compileSdkVersion 23
    } else if (condition) {
        compileSdkVersion "Amazon.com:Amazon Fire Phone SDK Addon:XX"            
    }
  //...
}

这篇关于是否有可能针对不同的产品口味有不同的build.gradles?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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