Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect IDE environment with Gradle

Is there anyway to detect the environment I'm running my project.

Something like this:

build.gradle

def usingIntelliJ = ...
def usingAndroidStudio = ...
if (usingIntelliJ) {
    buildConfigField "String", "IDE_ENV", "IDEA"
} else if (usingAndroidStudio) {
    buildConfigField "String", "IDE_ENV", "AndroidStudio"
}
like image 841
user3714348 Avatar asked Aug 15 '14 10:08

user3714348


People also ask

How do you run a test case using Gradle command?

Use the command ./gradlew test to run all tests.


1 Answers

To determine if your build is triggered by an IDE, the Android build chain will set a specific property:

def isIdeBuild() {
    return project.properties['android.injected.invoked.from.ide'] == 'true'
}

In our builds we use this method to set a static versionCode for our IDE builds, but keep the desired behavior to automatically increase it on our build servers:

def getNumberOfGitCommits() {
    def text = 'git rev-list --count HEAD'.execute().text.trim()
    return text == '' ? 0 : text.toInteger()
}

def calculateVersionCode() {
    return isIdeBuild() ? 123456789 : getNumberOfGitCommits()
}

android {
    defaultConfig {
        // ...
        versionCode calculateVersionCode()
    }
}

This solved two problems we had:

  1. Before, new commits effectively disabled Instant Run. Because the versionCode was updated automatically, the Manifest was changed – which triggers a full rebuild in Instant Run.
  2. Before, when we switched Git branches, the versionCode often changed to a smaller one (downgrade), so we had to re-install the app. Now we have the same versionCode for all our IDE builds.
like image 134
mreichelt Avatar answered Sep 29 '22 09:09

mreichelt