有没有办法以编程方式区分 IntelliJ IDEA 和 Android Studio

Is there a way to programmatically distinguish between IntelliJ IDEA and Android Studio

我想包含 settings.gradle.kts 文件的不同部分,具体取决于项目是使用 IntelliJ IDEA 还是 Android Studio 编译的。

出于我的目的,我只需要知道项目是否使用 Android Studio 编译,因为项目的某些特定于移动设备的模块只能使用 Android Studio 编译。

由于在设置 gradle kotlin 脚本文件中区分模块,我希望找到一个简单的 if 相应地包含这些模块的解决方案。到目前为止,我一直在使用 IDEA 时注释掉相关模块。

在Android Studio/IntelliJ IDEA 中设置了两个相关属性:

  • idea.paths.selector,在默认安装中具有诸如“AndroidStudioPreview2021.1”或“IntelliJIdea2021.2”之类的值,因此如果您需要知道它是否是或者:
val pathSelector: String? = System.getProperty("idea.paths.selector")
if(pathSelector == null) // Some other IDE or none
else if(pathSelector.startsWith("AndroidStudio"))
// Do Android Studio specific things
else if(pathSelector.startsWith("IntelliJIdea"))
// Do IntelliJ IDEA specific things
else // Some other Jetbrains IDE
  • idea.platform.prefix,似乎只为 Android Studio 设置,值为“AndroidStudio”,所以它适用于我的情况:
if(System.getProperty("idea.platform.prefix") == "AndroidStudio")
   // Do Android Studio specific things
else // Any other IDE or none

感谢您 @Konstantin Annikov 通知我另一个类似的问题,该问题从未完全回答但在正确的轨道上(使用属性)