3

I have a android gradle project where i have two buildTypes: QA and prod. I want to change a property in .properties file depending on the build type. I also want to change one of the value in settings.gradle file based on the buildType. I'm new to android and gradle so excuse me if this is a very basic question.

This is my .properties file:

build_number=3.1-SNAPSHOT
version_number=3.1.0
environment_name=prod //this needs to changed to environment_name=qa in qa environment

This is my settings.gradle file:

rootProject.name = 'tom-android-prod' //this needs to changed to rootProject.name = 'tom-android-qa' in qa environment

This is my build.gradle file:

apply plugin: 'com.android.application'

buildscript {
    repositories {
        jcenter()

    }

    dependencies {

    classpath 'com.android.tools.build:gradle:1.3.1'

    }
}


android {
    compileSdkVersion 21
    buildToolsVersion "21.1.0"

    defaultConfig {
        multiDexEnabled true
    }

    dexOptions {
        javaMaxHeapSize "4g"
    }

    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/ASL2.0'
    }

    configurations {
        all*.exclude group: 'com.android.support', module: 'support-v4'
    }

}

dependencies {
....
}
eureka19
  • 2,731
  • 5
  • 26
  • 34
  • I don't think that you can do it. What do you want to achieve? – Gabriele Mariotti Nov 22 '15 at 09:36
  • @GabrieleMariotti I want different properties to load based on the buildType. For eg. if the buildType is QA, environment_name=qa and if buildType is prod, environment_name=prod in .properties file. – eureka19 Nov 22 '15 at 09:45
  • Define a variable in your buildType or simple use different properties (not values) in your. properties file. – Gabriele Mariotti Nov 22 '15 at 09:54
  • @GabrieleMariotti i found an answer [link](http://stackoverflow.com/questions/9035356/how-can-i-transform-a-properties-file-during-a-gradle-build). But my build.gradle file cannot resolve ant.propertyfile. Any idea what can i am missing ? – eureka19 Nov 22 '15 at 10:18

1 Answers1

1

I'm not real sure what your goal is with the environment_name as its not used, but imagine you need to set a variable only for a specific build then you could do something like this:

ext {
    environment_name = null
}
android {
    // other stuff...
    buildTypes {
        qa {
            project.ext.environment_name = "qa"
        }
        prod {
            project.ext.environment_name = "prod"
        }
    }
}

// later you can use the variable like this
println "Hello variable: $project.ext.environment_name"

Using buildTypes should remove the need to modify the rootProject.name = 'tom-android'. Or you could use the productFlavors closure in the same way... depends on what all you want to specify explicitly for each build configuration.

JBirdVegas
  • 10,855
  • 2
  • 44
  • 50