Access project related data from the Gradle team

I need project related data such as project name, application version and its main module from a gradle based project. I tried various tasks, such as project, properties, but none of them gave me the specific information that I need.

Is there a way to find the version code, application name and main Android module using gradle on the command line?

+6
source share
4 answers

Using the global variable BuildConfig, you will get

boolean DEBUG

String APPLICATION_ID

String BUILD_TYPE

String FLAVOR

int VERSION_CODE

String VERSION_NAME

for example: - BuildConfig.APPLICATION_ID

and if you defined any global data in gradle like

debug { buildConfigField "String", "BASE_URL", '"http://172.16.1.175:8080/api/"' debuggable true } 

You will also receive this information.

BuildConfig.BASE_URL

+2
source

You can write your own gradle task for this. Add this piece of code to your build.gradle application, where you define your android plugin and launch it from the console. You can format the output as you need and use other data from the script assembly.

task hello<<{ println("versionCode = ${android.defaultConfig.versionCode}") println("applicationId = ${android.defaultConfig.applicationId}") println("minSDK = ${android.defaultConfig.minSdkVersion}") }

+1
source

you can use resValue to get the value

Gradle

  defaultConfig { //other config resValue "String","versionCode","1" } 

your class

 context.getString(R.string.versionCode); 
0
source

I do not know if this is suitable, you can create one common init gradle file that you run from the command line, so this is not a source code manipulation where you print all the necessary data. But the gradle conclusion is dirty.

This is the init.gradle snippet found in /Users/username

 allprojects{ afterEvaluate({//listen for project evaluation println(project.name)//it is supposed to be 2 projects "ProjName" and "app" if(project.name.equalsIgnoreCase("app")){//or any other condtion to check if it is inner android project project.task("getVersion",{ println("versionCode = ${android.defaultConfig.versionCode}") }) } }); } 

you run this script as ./gradlew --I /Users/username/init.gradle This is what I have as an output

 music app versionCode = 1 :help Welcome to Gradle 2.4. To run a build, run gradlew <task> ... To see a list of available tasks, run gradlew tasks To see a list of command-line options, run gradlew --help To see more detail about a task, run gradlew help --task <task> BUILD SUCCESSFUL Total time: 6.929 secs This build could be faster, please consider using the Gradle Daemon: http://gradle.org/docs/2.4/userguide/gradle_daemon.html 

So, here's what you can do, another option available is to parse the build.gradle or manifest.xml file in bash or write your own console utility that will do this with a cleaner output. Hope I helped.

0
source

All Articles