chikka.anddev
chikka.anddev

Reputation: 9629

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?

Upvotes: 7

Views: 346

Answers (4)

thekekc
thekekc

Reputation: 206

I don't know if it suits, you can create one common init gradle file, which you run from command line, so it is not a source code manipulation, where you print out all necessary data. But gradle output is dirty.

This is snippet of init.gradle which is 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 start this script like ./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 this is what could be done, another available option is to parse build.gradle file or manifest.xml in bash, or write own console utility that will do it with a cleaner output. I hope I helped.

Upvotes: 0

thekekc
thekekc

Reputation: 206

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}") }

Upvotes: 1

KDeogharkar
KDeogharkar

Reputation: 10959

you can use resValue for that to get value

Gradle

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

        }

your class

context.getString(R.string.versionCode);

Upvotes: 0

Bikesh M
Bikesh M

Reputation: 8383

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

Upvotes: 2

Related Questions