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?
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).
If you want to use additional libraries in your Android Studio project:
- Copy your .jar library into libs folder in your project
- Right click on the library and choose “Add as library”
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.
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).
Multi Project Setup is what you’re looking for I think. I wouldn’t focus too much on the Android Studio stuff at first. Focus on getting the gradle files right, and then “synchronizing” the module to update Android Studio. (I’ve had to restart it at times for the “Build Variants” section to update with new flavors.)
@Xav suggested in a Google+ response the following:
The high level project is there in case you want to add new modules later on (Library project). It’s easier to create a top project with a single module the first time, than to create a flat project, and converting it later when adding modules.
Two main things to point out, in your settings.gradle file, you would list the :app and :library modules; also the ‘android’ plugin is used for the app, but the ‘android-library’ plugin should be used for library modules.
If you are able to ./gradlew assemble from the command line, then Android Studio should update with the proper settings the next time you open the project or synchronize.
At the minute you need to configure your
build.gradle file to use jars with Android Studio or you end up with the dreaded
NoClassDefException at runtime. See my answer here for some tips on getting jar libs working. You can also define Maven dependencies straight into that
build.gradle which is very handy.
For the sake of anyone hitting on this, here’s what I’m doing to “solve” this issue. It’s very far from an elegant solution, IMO, but it’s the closest to a good solution that I’ve found for handling my problem (reusing Android libraries) in Android Studio with Git.
Basic idea is to use Git submodules. Each library has it’s own Git repository.
- Create myLibrary as a module, and then commit it to a repository.
- From the project root repository, I do an
git submodule addto fetch the library.
- In settings.gradle, I add:
include ':myLibrary'and sync the project.
- Module Settings >> Dependencies >> + Module Dependency and add a dependency to the module as appropriate.
It’s a far cry from the simple elegance of working with Android libraries in Eclipse, but it works after a fashion. Things to keep in mind:
- remember to git pull frequently, to pick up changes (and then commit the main project to sync with the submodule changes).
- remember to git push from the module directory to remote, if you’ve committed changes to the library.
The built-in VCS should be able to help here, but it’s been pretty flaky for me, so I prefer to just do it myself from a terminal now.
Hope this helps.
Attach android library into android studio. If you have any idea about library then add directly for eg.
(this is the picasso library)
If you want to create our own library then follow these steps.
- Click on File menu
Choose New module and select Android Library.
Declare Library Name and package name.
Now finally your library has been created you can use easily.
- Click on File and after then click on Project Structure
Click on Dependencies Tab inside small window and click Green color plus button.
And select Module dependency you can see your library file.
select library and ok