Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access ext variables in Gradle

I am trying to get the variable defined in a 3rd party library (fabric) to do a condition based on whether Crashlytics is enabled or not.

ext.enableCrashlytics = true

http://support.crashlytics.com/knowledgebase/articles/202938-gradle The variable can be configured in buildTypes or in flavors but I can't find a way to access it elsewhere in my build.gradle

I tried several things without any luck.

allprojects.getProperties().get("enableCrashlytics")
project.enableCrashlytics
project.ext.enableCrashlytics
allProjects.ext.enableCrashlytics

Anyone tried that before? The context I'm trying to do this is to write the fabric.properties file based on if it is enabled or not.

android.applicationVariants.all { variant ->
  ...
  //create fabric.properties
  ...
}
like image 488
Eric Labelle Avatar asked Sep 17 '15 16:09

Eric Labelle


People also ask

What does ext do in Gradle?

Extra properties extensions allow new properties to be added to existing domain objects. They act like maps, allowing the storage of arbitrary key/value pairs. All ExtensionAware Gradle domain objects intrinsically have an extension named “ ext ” of this type.

How do I declare a variable in Gradle?

Local variables are declared with the def keyword. They are only visible in the scope where they have been declared. Local variables are a feature of the underlying Groovy language. Local variables are declared with the val keyword.

What is subprojects in Gradle?

The subproject producer defines a task named buildInfo that generates a properties file containing build information e.g. the project version. You can then map the task provider to its output file and Gradle will automatically establish a task dependency.

What is Buildscript in build Gradle file?

buildscript: This block is used to configure the repositories and dependencies for Gradle. dependencies: This block in buildscript is used to configure dependencies that the Gradle needs to build during the project.


1 Answers

You can define a property in your top-level build.gradle:

ext {
  myproperty = 12
}

Or an array:

ext {

    myarray = [
            name0     : "xx",
            name1     : "xx"
   ]
}

Then in each module you can use somenthig like:

rootProject.ext.myproperty
rootProject.ext.myarray.name0
like image 198
Gabriele Mariotti Avatar answered Sep 26 '22 16:09

Gabriele Mariotti