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"
}
Use the command ./gradlew test to run all tests.
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:
versionCode
was updated automatically, the Manifest was changed – which triggers a full rebuild in Instant Run.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.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With