Search code examples
android-studiokotlingradleintellij-ideagradle-kts

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


I would like to include different parts of a settings.gradle.kts file depending on whether a project is compiled with IntelliJ IDEA or Android Studio.

For my purposes, I only need to know if the project is compiled with Android Studio or not, as certain mobile-specific modules of the project can only be compiled with Android Studio.

Since the modules are distinguished in the settings gradle kotlin script file, I hope to find a solution where a simple if includes those modules accordingly. Thus far I've been commenting out the relevant modules when working in IDEA.


Solution

  • There are two relevant properties that are set in Android Studio/IntelliJ IDEA:

    • idea.paths.selector, which in default instalations has a value such as "AndroidStudioPreview2021.1" or "IntelliJIdea2021.2", so you could use this if you needed to know if it was either:
    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, which seems to only be set for Android Studio, with a value of "AndroidStudio", so it works for my case:
    if(System.getProperty("idea.platform.prefix") == "AndroidStudio")
       // Do Android Studio specific things
    else // Any other IDE or none
    

    Thank you @Konstantin Annikov for notifying me about another similar question that was never fully answered but was on the right track (using properties)