3

I have a simple multi-module Gradle project with Java code. When I import it in IntelliJ Community, it asks me to select the build tool from among Maven and Gradle, and will properly recognize the Gradle modules and show me a Gradle tool window.

When I do the same in Android Studio, I don't get the build tool question, and it won't recognize the projects or show me the Gradle tool window.

How do I get this to work in Android Studio?

Jorn
  • 20,612
  • 18
  • 79
  • 126
  • 1
    I've used A/S for several years, over multiple A/S different versions. AFAIK, it should "just work". But I never liked A/S as much as I liked Eclipse for Android development ... precisely because A/S tries too hard to be "helpful" by doing too much "black magic" behind your back. So I'm unsurprised at what you're seeing. For my part, I've fled A/S, I refuse to deal with "Kotlin-first" ... and I'm happily using VSCode with Dart/Flutter instead ;) – paulsm4 Oct 03 '21 at 03:32

5 Answers5

1

Simply Try This,

Go to File -> Project Structure -> Sdk Location

This You will get a option menu like this (the given image)

there you can update your gradle settings.

enter image description here

  • OK, it's getting weird now. For the gradle/Java-only project, I don't see the SDK Location in this window. I checked with an actual Android project, and there I *can* configure this. I'm starting to think Android Studio is only loading half its own plugins for this project, or something? – Jorn Sep 27 '21 at 09:22
  • https://stackoverflow.com/q/16626810/13304913 hope this would help you! – Emon Hossain Munna Sep 27 '21 at 09:26
  • It helps a little, in that I can indeed make a new java library in an existing android project. But unfortunately it doesn't help me making Android Studio recognize existing Gradle projects. I've even tried making my own `.iml` files based on the one for the new Java library project, but even that doesn't help. – Jorn Sep 28 '21 at 08:45
0

The answer, as far as I've been able to find out, is: Don't even try. Use IntelliJ instead. Installing it is most certainly quicker than trying to get Android Studio to do this.

Jorn
  • 20,612
  • 18
  • 79
  • 126
0

How do you open the project in Intellij? Do you open the folder? Or do you open the build.gradle?

Just like a maven project (where you should open the pom.xml, not the folder) you should open the project using the build.gradle, not the folder.

Intellij will then do what you expect and import your gradle project

lance-java
  • 25,497
  • 4
  • 59
  • 101
  • I just tried opening the project using the top level `build.gradle`, but Android Studio just says "The project file specified already exists". And yes, I deleted the existing `.idea` folder and `*.iml` files before trying. So thanks for the hint, but this doesn't work either. – Jorn Oct 04 '21 at 07:16
0

The least required to make it recognize the project is to add AGP, the Android Gradle Plugin:

buildscript {
    repositories {
        google()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:7.0.2"
    }
}

Then one can define Android modules with:

  • apply plugin: "com.android.application"
  • apply plugin: "com.android.library"
  • apply plugin: "com.android.dynamic-feature"

Then it will recognize the android {} configuration block (without this Gradle DSL configuration block it would not know what or how to build for Android - as it isn't a sane Android project). Just see the documentation linked, this has little to do with the default Java tooling.

Gradle 7.2 is currently required to build. Even if one cannot mix the plugins per module, one can have different plugins per module - or use different IDE, depending on the tooling of the module. How compatible this really is, is being determined by the dependencies used in the Java module. The fact that one can only use either tooling per single module dictates the layout to be applied.

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
-1

I suspect that you will have to manually move some code from the successful build files in intellij to the android build files.

Ray Tayek
  • 9,841
  • 8
  • 50
  • 90