Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Libraries in Android Studio

Tags:

Has anyone figured/found out how Android libraries are intended to work in Android studio?

I have not been able to find any documentation on this yet (the documentation on the Android Developer Site is incredibly bare-boned), and I observe that depending on how I create the library project, I get completely different results.

If I import a library from Eclipse (following the recommendations on the website to export to Gradle first) I end up with a new project + module containing the library project. This does not seem quite right (an intellij project = eclipse workspace), and attempts to compile/make this in intellij results in many errors (the library project can't find the android support libraries).

If I create a new project from scratch with a library module, then I get an android library project with a build.gradle file.

If I create a new module from within a project (using right click on the project), then I get an Android library built using Ant. This compiles fine, but seems very odd. Surely it is not intended that we should use Ant for Android libraries in android Studio? Creating a new library module using File > New Module doesn't seem to work, incidentally. It just creates a new application instead.

Has anyone picked up any information to make sense of this? I also haven't found any location where one can specify which libraries should be used in which application modules. I understand this is a preview release, but I'm having difficulty believing that a core feature like Android libraries is so poorly supported. What am I missing?

Update 2014-04-09:

So I took a new round with Android Studio this week. And while the problems now are different than when I originally wrote this, this is - quite incredibly - still a big problem. I've still not found a good explanation of using Android libraries in multiple projects in AS (including nested libs). Some of the suggestions I've seen recommend copying code into multiple locations - which completely defeats the purpose of having a library to begin with.

I just don't get what Google are thinking with Android Studio... It's a pity, because it seems like a great tool, but the lack of an intuitive handling something so basic as code reuse is a big issue for me (never mind that coding specifically for Android is becoming less and less attractive, when compared to the ever-improving cross-platform development tools).

like image 629
Michael A. Avatar asked May 16 '13 13:05

Michael A.


People also ask

Where are libraries Android studio?

The android default libraries like appcompact, design support libraries are stored in your folder where you have installed the SDK, precisely <SDK FOLDER>/extras/android/m2repository/com/android . The 3rd party libraries are stored in . gradle/caches/modules-2/files-2.1 folder.

What is the Android support library?

The Android Support Library package is a set of code libraries that provide backward-compatible versions of Android framework APIs as well as features that are only available through the library APIs. Each Support Library is backward-compatible to a specific Android API level.

How do I create a library for Android?

Creating The Android Library Open Android Studio and create a new Project. Name your application as ToasterExample and your project name as Toaster. The ToasterExample will be our sample application to show an example use of our application. Click on Finish and your project will be ready.


2 Answers

If you want to use additional libraries in your Android Studio project:

  1. Copy your .jar library into libs folder in your project
  2. Right click on the library and choose "Add as library"
  3. In build.gradle replace

    compile files('libs/android-support-v4.jar')

with

compile fileTree(dir: 'libs', include: '*.jar')

If it still doesn't work, navigate to the root folder of your project with our terminal and run a gradlew clean command.

like image 104
JustMeh Avatar answered Sep 19 '22 18:09

JustMeh


Intellij is different to Eclipse, in that your 'workspace' is only for one 'project'. Each project is made up of multiple modules. And these modules can themselves be there own 'project / app / library'.

So a module is kind of like the equivalent of an Eclipse project.

Go to : File->Project Structure->Modules

And add the Android Library as a module, you can declare that it is in Android Library and make it a dependency of your 'project' (app module).

like image 33
Blundell Avatar answered Sep 21 '22 18:09

Blundell