How to get Android Studio to recognize my plain Gradle Java project? - android-studio

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?

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.

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.

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

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.

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

Related

What is CompositeDefinitionSource in Android Studio

Recently after upgrading Gradle Android Studio automatically added this to my .idea/gradle.xml :
<compositeConfiguration>
<compositeBuild compositeDefinitionSource="SCRIPT" />
</compositeConfiguration>
What is the purpose of this change?
First there is several points to understand:
The file .idea/gradle.xml is used by Android Studio to store the Gradle project settings.
According to the Gradle documentation:
A composite build is simply a build that includes other builds.
According to the documentation of IntelliJ IDEA on which Android Studio is based, there is two ways to define a Gradle composite build either through the IDE or through the settings.gradle file.
According to the source code of IntelliJ IDEA the value of compositeDefinitionSource can be either SCRIPT or IDE.
To answer your question, the purpose of the compositeConfiguration element in the .idea/gradle.xml file is to define as a default the Gradle composite build by script. If you set a Gradle composite build through the IDE this value will change.

Android Studio referencing classes in an AAR published to local maven repo

I have a problem in Android Studio (3.1.2) referencing classes in an AAR published to my local maven repo.
I have successfully published an AAR file to my local maven repo and included it in a project that needs to use the AAR following this article:
https://medium.com/#kuassivi/tip-work-with-third-party-projects-locally-with-gradle-961d6c9efb02
Basically I have added a reference to mavenLocal() in the top level project in the repositories sections of buildscript and all projects. I have also added a reference to the AAR in the app gradle build (i.e. implementation groupid:artifactid:version). I have not done anything more than this.
If I do a gradle rebuild within Android Studio, everything compiles fine but if I edit a file that references classes in the AAR, the IDE cannot resolve the references.
When I do find class (i.e. cmd o), the IDE correctly finds the class within the classes.jar of the AAR and decompiles it.
I am guessing that this is happening partly because it cannot pick up the source jar that I published alongside the aar file to the local maven repo.
How do I get the IDE to pick up the source files and reference the classes in the AAR correctly? I would prefer to do this via a change in the gradle scripts rather than via an IDE wizard, if that is possible.
After more digging, I have discovered that the use of the maven repo is irrelevant because it is picking up the AAR file from the gradle cache in ~/.gradle/caches/transforms-1/files-1.1 not from the maven local repo. I can also include the sources files using "implementation com.company:artifact-name:0.8:sources". There is a good information here:
https://www.alonsoruibal.com/my-gradle-tips-and-tricks/
However Android Studio still cannot recognise the imports and though compilation works, the imports are marked in red in the editor. I guess there must be some magic with gradle that I am missing.
I put together a simple example on github here:
https://github.com/jmc420/kotlin_examples
In the multiproject folder there are 2 separate projects, one called "nested" which contains an android library project called "multiprojectlibrary" which "nested" references via a grade include.
The "multipleprojectlibrary" uses the maven publish plugin to publish the aar to mavenLocal via the command gradle publishToMavenLocal.
In the "independent" project "multipleprojectlibrary" is a dependency which is read from mavenLocal.
The "nested" project works reliably.
Using mavenLocal in the "independent" project seems very unreliable. Sometimes the "independent" project correctly uses the mavenLocal "multipleprojectlibrary" but when you make an update and publish again, the "independent" project starts using a copy of "multipleprojectlibrary" in .gradle/caches/transforms-1/files-1.1.
There seem to be 2 issues. One is that the "New Project" menu option of Android Studio only allows you to make an app; you can only make an Android Library via the "New Module", which puts the library as a sub project of an app. An option to create a "New Library" would solve this.
The other issue is that it is not clear how the maven local repo and the gradle cache interact. They seem to get out of sync and sync gradle does not seem to sync the gradle cache with the mavenLocal repo.

Using Android Studio, Adding a Library to build.gradle

I'm using Android Studio, and I'm trying to add the slf4j library using
Build/Edit Libraries and Dependencies.
I clicked on the +, selected Library Dependency, and picked slf4j from the list. Studio added the library to my build.gradle file like this:
dependencies {
compile 'com.android.support:support-v4:24.1.1'
compile 'org.slf4j:slf4j-android:1.7.7'
...
But when I try to run sync, I get this error:
Error:(75, 10) Failed to resolve: org.slf4j:slf4j-android:1.7.7
Is there something else I need to do to my project to be able to use this library??
Turns out what was causing this issue for me (and likely for you, given the build file above) was the Scala plugin. There was a bug with the Scala plugin preventing Android Studio from importing Gradle projects. The fix for this has already been pushed live, so to get Android Studio working again you can either disable the Scala plugin or update it.
Open Settings/Preferences
Open the Plugins section on the left
Find the Scala plugin
Right click on the plugin and click Reload List of Plugins
Right click on the plugin again and click Update Plugin
This allowed me to do a Gradle sync and build from Android Studio again, I hope it helps for you!
It looks like this is a bug with Android Studio. Hopefully it'll be fixed soon.
In the meantime, you can debug your application without launching it from Android Studio by attaching the debugger to a running process of the application. You can do this two ways.
Via the menus: Run -> Attach Debugger to Android Process
Pressing the button that is two to the right of the play/build button. It looks like a grey phone/rectangle with a little green bug in the bottom right
After you do this, a dialog will show up listing available applications to debug. Select your application and press OK. Your breakpoints should now work.
If you need to debug something that happens on app startup, you can tell Android to wait for a debugger when launching certain applications with the following steps:
On the phone open the Android Settings
Open the Developer Options
Make sure Wait for debugger is checked
Press Select debug app and choose your application
Then when you start your application, it'll wait for you to attach a debugger via one of the two methods mentioned above.
Hope this helps!
In order to get the maven repository working with my Android Studio project (which I didn't know is what I was trying to do), I needed to add the following to build.gradle:
allprojects {
repositories {
mavenCentral()
}
}
Apparently, that allows the software to work out the compile commands.

Trying to convert projects that werent built using Gradle

I have tried to clone the following repository on GitHub using Android Studio, https://github.com/AlexKang/favr.git, but I have the error:
Before this I selected build project using Gradle and then accepted anything it wanted to install. I am having similar problems with, https://github.com/mb16/RemindEm.git, where I get that
Each time I selected "create project from existing sources" and then accepted everything it suggested.
What am I doing wrong?
The projects have eclipse .classpath files in the folder. I'm guessing you should be able to import the project into eclipse.
As an alternative to using Eclipse you might want to try using a feature of IntelliJ IDEA (Android Studio's parent project). File > New > Project from Existing Sources... Then select the .project file for the import

How to create Gradle build for existing IDEA project?

I have a Groovy project in Intellij IDEA 13.1.5 that I want to build with Gradle. So I have installed Gradle itself and enabled the Gradle plugin in IDEA.
But how can I now build the project (or better: start to write the Gradle build file) from within IDEA? I have created a build.gradle file in the project's module, which is recognized by IDEA correctly as Gradle file, according to the icon next to the file name.
Beside that I don't know how to continue from there. IDEA documentation says I should open the Gradle Tools window, but I can find it in View --> Tool Windows.
Any ideas?
The best way to accomplish this while still being in the same directory is by going to File -> New -> Module From Existing Sources.. and then selecting your build.gradle.
You can create a Gradle Run configuration. To get all IntelliJ Gradle features, you need to import the project via Gradle (File->Import Project, then point it to build.gradle).

Resources