Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access project related data from Gradle command

I need project related data like project name,app version and its main module from gradle based android project. I have tried various tasks like project,properties but none of it giving me specific information i need.

Is there a way to find version code,app name and main android module using gradle in command line?

like image 489
chikka.anddev Avatar asked Mar 25 '16 12:03

chikka.anddev


People also ask

How do I run a Gradle project from the command line?

To run a Gradle command, open a command window on the project folder and enter the Gradle command. Gradle commands look like this: On Windows: gradlew <task1> <task2> … ​ e.g. gradlew clean allTests.

What is gradlew command?

gradlew is a wrapper(w - character) that uses gradle . Under the hood gradlew performs three main things: Download and install the correct gradle version. Parse the arguments. Call a gradle task.

What is the use of Buildscript in Gradle?

Gradle builds a script file for handling two things; one is projects and other is tasks. Every Gradle build represents one or more projects. A project represents a library JAR or a web application or it might represent a ZIP that is assembled from the JARs produced by other projects.

What is buildSrc in Gradle?

buildSrc is a separate build whose purpose is to build any tasks, plugins, or other classes which are intended to be used in build scripts of the main build, but don't have to be shared across builds.


2 Answers

Using "BuildConfig" global variable you will get

boolean DEBUG

String APPLICATION_ID

String BUILD_TYPE

String FLAVOR

int VERSION_CODE

String VERSION_NAME

eg :- 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 get this details also

BuildConfig.BASE_URL

like image 166
Bikesh M Avatar answered Oct 02 '22 23:10

Bikesh M


You can probably write your own custom gradle task for doing that. Add this code snippet in your app build.gradle, where you define your android plugin and run it from console. You can format output like you need it and use other data from build script.

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

like image 37
thekekc Avatar answered Oct 02 '22 22:10

thekekc