4

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.

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
Siva Kumar
  • 893
  • 4
  • 14
  • 28
  • 1
    According to the Amazon docs, the point behind `com.amazon.device.tools.build:gradle` was to fix [a bug that was fixed a year ago](https://code.google.com/p/android/issues/detail?id=75407). Have you tried just using the regular Gradle for Android plugin, perhaps a newer version than the one that you have listed above? (e.g., `com.android.tools.build:gradle:1.3.0`)? I don't know if you can override `compileSdkVersion` or `buildToolsVersion` on a per-flavor basis. – CommonsWare Aug 19 '15 at 18:40
  • I just tried that but this xml namespace can not be retrieved "http://schemas.android.com/apk/res/amazon" – Siva Kumar Aug 19 '15 at 18:51

1 Answers1

4

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"            
    }
  //...
}
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • Thanks Gabriele. But how do I get the condition based on the product flavor as buildscript and android get executed before that (correct me if I am wrong here)? – Siva Kumar Aug 19 '15 at 20:07
  • 1
    @SivaKumar True. You can't get the condition based on the flavor. Hovewer (it is ugly... I know) you can use a property in `gradle.properties` or a variable in top-level file. Something like `type = google|amazon` and then `if (project.type == "google") {` – Gabriele Mariotti Aug 19 '15 at 21:02
  • @GabrieleMariotti but how do I select that specific type in the build.gradle file after I have specified the types in gradle.properties? – FabioR Apr 12 '22 at 04:19