Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 461
Siva Kumar Avatar asked Oct 22 '25 01:10

Siva Kumar


1 Answers

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"            
    }
  //...
}
like image 103
Gabriele Mariotti Avatar answered Oct 23 '25 15:10

Gabriele Mariotti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!