如何判断何时从AndroidStudio运行gradle?

Edw*_*ale 3 android gradle android-gradle-plugin

我需要在gradle构建文件中构建一些技巧,以使Android Studio理解一些东西。当我从命令行目录运行构建时,不需要这些技巧。有没有办法检测何时从Android Studio中运行构建?也许通过环境变量等?

sha*_*aca 5

使用gradle -P blah=val命令行,并在您的build.gradle使用project.hasProperty("blah")project.getProperty("test")if (blah ... ) 决定是否运行黑客或没有。

更新:

好的,我找到了直接的方法:)

def env = System.getProperties()
if (env['com.android.studio.gradle.project.path'] != null) {
    // build from Android Studio, do magic here
}
Run Code Online (Sandbox Code Playgroud)


小智 5

AndroidStudio 2.1.1中,您可以使用idea.platform.prefix属性:

def sysprops = System.getProperties()
if (sysprops['idea.platform.prefix'] != null) {
    // Built from AndroidStudio
} else {
    // Built from command line
}
Run Code Online (Sandbox Code Playgroud)