Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access environment variables in gradle java build script

How can I access environment variables (either user level or system level) in gradle java build script.
I am new to gradle and I am trying to build my project using the gradle.
Presently I have hardcoded the path of third party jars as shown in the script below

repositories {
   flatDir  {
           dirs 'D:\\lib'
   }
}

I have created an environment variable "Third_Party" in enviroment variable:

Third_Party=D:\lib

How can I use this variable i.e. "Third_Party" instead of hardcoding the library path in script.
Please tell the exact syntax

like image 439
tech developer Avatar asked Jan 13 '15 06:01

tech developer


People also ask

How do I get environment variables in build Gradle?

Enabling Environment Variables in Gradle Android developers can use environment variables using Gradle's module-level build configuration. This module-level Gradle configuration file lets you specify build settings for that application module.

How do I access the build Gradle file?

gradle file is located inside your project folder under app/build. gradle.


1 Answers

try this way System.getenv("env var name") like

home = System.getenv('Third_Party')

task env_read << {
    println "$System.env.HOME"
    //Other way of accessing the environment variable.
    println  System.getenv('Third_Party')
}
like image 60
abdotalaat Avatar answered Oct 05 '22 06:10

abdotalaat