使用Gradle检测IDE环境 [英] Detect IDE environment with Gradle

查看:119
本文介绍了使用Gradle检测IDE环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论如何,有没有检测到我正在运行项目的环境.

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

类似这样的东西:

build.gradle

build.gradle

def usingIntelliJ = ...
def usingAndroidStudio = ...
if (usingIntelliJ) {
    buildConfigField "String", "IDE_ENV", "IDEA"
} else if (usingAndroidStudio) {
    buildConfigField "String", "IDE_ENV", "AndroidStudio"
}

推荐答案

要确定您的构建是否由IDE触发,Android构建链将设置特定属性:

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'
}

在构建中,我们使用此方法为IDE构建设置静态versionCode,但保留所需的行为以在构建服务器上自动增加它:

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. 之前,新提交实际上已禁用即时运行.由于versionCode是自动更新的,因此清单已更改-触发了即时运行中的完全重建.
  2. 之前,当我们切换Git分支时,versionCode通常更改为较小的(降级),因此我们必须重新安装该应用程序.现在,我们所有的IDE版本都具有相同的versionCode.
  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.

这篇关于使用Gradle检测IDE环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆