DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Five Minute Android: Simplifying Gradle Build Scripts With Functions

Five Minute Android: Simplifying Gradle Build Scripts With Functions

If you've used build flavors in Gradle, you might have a lot of copy and paste code. Adding functions can make your scripts instantly more readable.

James Sugrue user avatar by
James Sugrue
CORE ·
Feb. 22, 17 · Tutorial
Like (3)
Save
Tweet
Share
4.15K Views

Join the DZone community and get the full member experience.

Join For Free

If you're like me, or tons of other developers, you'll have started out with a limited number of build flavors in your Gradle build script — probably two, in fact: production and development. However, as you add different variables to expose in your BuildConfig, and extended the number of flavors, your code might start to look like a copy and paste mess. 

Here's an example: 

production {
   buildConfigField 'String', 'REST_API', '"https://api.myserver.com/"'
   buildConfigField 'Boolean', 'DEBUG_MODE', 'false'
   buildConfigField 'Boolean', 'DEMO_MODE', 'false'
   }
development {
   buildConfigField 'String', 'REST_API', '"https://test-api.myserver.com/"'
   buildConfigField 'Boolean', 'DEBUG_MODE', 'true'
   buildConfigField 'Boolean', 'DEMO_MODE', 'false'
   }
demo {
   buildConfigField 'String', 'REST_API', '"https://demo-api.myserver.com/"'
   buildConfigField 'Boolean', 'DEBUG_MODE', 'false'
   buildConfigField 'Boolean', 'DEMO_MODE', 'true'
   }


By using a combination of environment variables and functions, we can simplify these flavors. Most continuous integration systems, like TeamCity and Jenkins, will allow you to set parameters during the build, which in turn can be set as environment variables.

In the example above, the REST API variable is an obvious candidate for this. Here's how it would look, assuming the API_SERVER environment variable is used in the build script: 

productFlavours{
  def getAPIServer = {
	def server = System.getenv("API_SERVER")
	if(server)
        return '"'+server+'"'
   	else
        return '"https://api.myserver.com"'
  }
  //other functions and the rest of the flavors 
  //....
}


As you can see the function reads from the environment variables set, but will also return a default if the variable isn't set. Typically it makes sense to have the production variables as the defaults, as you really don't want to send out a production build pointing at a development setup. 

To use this in the flavor definition itself: 

production {
   buildConfigField 'String', 'REST_API', getAPIServer()
   buildConfigField 'Boolean', 'DEBUG_MODE', 'false'
   buildConfigField 'Boolean', 'DEMO_MODE', 'false'
   }


While it might not reduce the lines of code in your script, it certainly a clearer way of expressing the variations.

Continuous Integration/Deployment Gradle Android (robot)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Uplevel Your Managers With Mini-M Support Groups
  • How Observability Is Redefining Developer Roles
  • Spring Cloud: How To Deal With Microservice Configuration (Part 1)
  • What Is Testing as a Service?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: