Question

This question has been discussed a lot, but the problem is that Android Studio has changed a lot in past few months and for example the Project Structure dialog window is now completely different. So many solutions don't work anymore, at least for me.

If I understand it correctly, there are two separate issues to be solved when importing external JARs (or generally using external libraries).

  1. I have to configure the jar as a Gradle dependency so it would compile with the rest of my code.
  2. I have to let Android Studio know that I'm using this jar as a library so I would get autocomplete and other IDE stuff working.

I managed to solve the first problem. I put the jar file into build.gradle to dependencies { ... } and when I use some class contained in the jar file, it builds and runs with no errors (and when I remove it from dependencies it crashes so I would say it works as expected).

But I have a problem with the second part. I added the jar file as a library using rightclick on the file in the sidebar and I can now expand the jar and see all contained classes. However, Android Studio still doesn't recognize these classes when I use them in my code (which is in separated module/folder). Any ideas how to fix it?

Was it helpful?

Solution

So the answer is probably to add the jar file as a File dependency to the main module in Project Structure dialog. Unfortunately, I wasn't able to add anything from the outside of the main module directory which was kind of the point of all of this, to keep my jars in one separate place.

Anyway, I solved my problem by switching to maven. As I'm new to all of this, I didn't know that there is such a thing and that I can use it together with Gradle so easily. I simply put the dependency I needed to build.gradle, then clicked on Clean Project and Rebuild Project and finally I closed and reopened the project in Android Studio. After that, I could both build my application and use desired classes in my code with all IDE luxury.

Apparently, the dependencies are now completely managed by Gradle and any clicking in Android Studio (which is what most solutions here on stackoverflow suggest) is wrong.

UPDATE: You don't have to do all the steps described in the second paragraph because there is a special tool for it. Just go to Tools > Android > Sync Project with Gradle Files (or click the appropriate button in the toolbar) and that should do the job.

OTHER TIPS

I think you have to right click the jar file inside Android Studio and hit "Add as library". Someone else had the same problem: Android Studio: Add jar as library?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top