Is it possible to have different build.gradles for different product flavors?

偶尔善良 提交于 2019-12-23 19:20:01

问题


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.


回答1:


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"            
    }
  //...
}


来源:https://stackoverflow.com/questions/32103127/is-it-possible-to-have-different-build-gradles-for-different-product-flavors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!