简体   繁体   English

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

[英]How to tell when gradle is being run from AndroidStudio?

I need to build some hacks into my gradle build file so that Android Studio understands some things. 我需要在我的gradle构建文件中构建一些hack,以便Android Studio了解一些内容。 I don't need these hacks when I run the build from the command line directory. 当我从命令行目录运行构建时,我不需要这些hacks。 Is there a way to detect when the build is being run from within Android Studio? 有没有办法检测从Android Studio中运行构建的时间? Maybe through environment variables, etc? 也许通过环境变量等?

With AndroidStudio 2.1.1 , you can use the idea.platform.prefix property: 使用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
}

Use gradle -P blah=val from command line and in your build.gradle use project.hasProperty("blah") or project.getProperty("test") or if (blah ... ) to decide whether run your hack or not. 在命令行和gradle -P blah=val使用gradle -P blah=val使用project.hasProperty("blah")project.getProperty("test")或者if (blah ... )来决定是否运行你的hack 。

Updated: 更新:

OK I found the direct way :) 好的,我找到了直接的方式:)

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

Jake Wharton suggests android.injected.invoked.from.ide to speed butterknife at development time by using reflection : Jake Wharton建议android.injected.invoked.from.ide 使用反射在开发时加速butterknife

dependencies {
  if (properties.containsKey('android.injected.invoked.from.ide')) {
    implementation 'com.jakewharton:butterknife-reflect:<version>'
  } else {
    implementation 'com.jakewharton:butterknife:<version>'
    kapt 'com.jakewharton:butterknife-compiler:<version>'
  }
}

From Twitter : 来自Twitter

Hey ButterKnife users: I'm working on a reflection-based implementation for use during development so the annotation processor is not needed. 嘿ButterKnife用户:我正在开发基于反射的实现,以便在开发过程中使用,因此不需要注释处理器。

A follow-up : 后续行动

What is this? 这是什么? A property from (link: http://gradle.properties ) gradle.properties? 来自(link: http ://gradle.properties)gradle.properties的属性?

The answer we want : 我们想要答案

No it's added by the IDE 不,它是由IDE添加的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM