67

I'm getting really confused and frustrated because I just can't understand how to import my android library project as dependency for my application project.

I tried to find online and most of the answers suggested to go to project structure of my application and click on '+' and then click on 'import module'

But Android Studio 0.3.4 - 0.5.2 doesn't show any options and when I click on '+', it just shows the windows for a new module.

Am I doing something wrong or what ?

Help!!!

shaktiman_droid
  • 2,368
  • 1
  • 17
  • 32

5 Answers5

17

You'll run into this when working with Gradle-based projects, as this one is; even in 0.3.2, this Import project command was doing the wrong thing; it would seem to work, and would make syntax coloring in the editor work properly, but it would fail when you would try to build and run your app, and your changes would be lost when you re-synced the project with the Gradle build files. This is why this has been removed until it can be properly reimplemented. The bug to track it is at https://code.google.com/p/android/issues/detail?id=62122

In the meantime, to add an existing library as a dependency, you'll need to do it by hand in your Gradle build files. To do so, move the library so its directory is under the project root (at the moment, including modules outside the project root isn't supported), write a build.gradle build file that will build the module, and include the module in your settings.gradle file.

You might find it easier if you go through the File > New Module... command to create a blank module, choosing either a plain Java module or an Android module as appropriate, and copying the library files into the module directory where they need to go. That will take care of the Gradle files for you and set up the skeleton directory structure.

Scott Barta
  • 79,344
  • 24
  • 180
  • 163
  • 3
    Thank you but doing this will mess up my structure. I don't have just one app which depends on this library and moving library to individual app project root seems inappropriate. I'll wait for a proper solution and keep continuing use of my current IDE. Meanwhile I starred the bug to get the updates about it. – shaktiman_droid Nov 18 '13 at 23:09
12

For android studio 0.5.7, volley was successfully imported as a library project following these steps:

  1. Create a directory named "libraries"(whichever you want) under your project root
  2. Clone volley using git under the directory created in step 1, command is "git clone https://android.googlesource.com/platform/frameworks/volley". Now the project structure looks like:

    [Project root]
    |- [Your module]
    |- libraries
       |- volley
    
  3. Import volley through : Right click project root -> Open Module Settings -> Click "+"(New Module) in the up left corner -> Import existing project -> Select volley source directory -> Next After step 3, volley is imported, but dependency on library project volley is not setup yet.
  4. Setup module dependency : Right click project root -> Open Module Settings(Now volley should appear in the module list) -> Choose [Your module] -> Switch to tab "Dependencies" -> Click "+"(Add) in the left bottom corner -> Choose "Module dependency" -> Select ":volley" in the module list dialog
  5. Now everything works fine, you can use volley as you want
cmoaciopm
  • 2,076
  • 2
  • 22
  • 30
  • This works with the facebook and parse SDKs in AS 0.6.0. In fact, you can ignore steps 1 and 2... this should work for any module. – Blake G Jun 12 '14 at 20:42
10

I had similar problem. I opened settings.gradle file and add the library include ':Test', ':libraries:MyLibrary', then in build.gradle add the line: compile project(':libraries:MyLibrary'). If you do: Right Click at the project -> Open Module Settings -> your project -> Dependencies, you will see the same dependencies in build.gradle. By the way, after this actions I restarted Android Studio, then IDE created *.iml file.

Josh Kovach
  • 7,679
  • 3
  • 45
  • 62
Volodymyr
  • 1,037
  • 1
  • 11
  • 27
2

Finally the import module feature has arrived in Android Studio 0.5.3 which was release on March 27th, 2014.

Take a look here: http://tools.android.com/recent/androidstudio053released

I have also tested and it's there in project structure window.

In Module tab, you can now press the '+' button and either import an existing module or create a new one.

shaktiman_droid
  • 2,368
  • 1
  • 17
  • 32
  • I updated to the 0.5.3 somehow the import module is not there. I tried File->Import Module, but it opened another project window. – JR Tan Apr 02 '14 at 17:18
  • @TanJitRen What are you trying to achieve ? Import module lets you import another project into the current one so that you can have dependencies. – shaktiman_droid Apr 02 '14 at 17:44
  • I wanted to import existing module but my 0.5.3 doesn't include the feature. Look over here, he's facing the same issue too. http://stackoverflow.com/questions/22743711/project-structure-dialogue-in-android-studio-0-5-3-not-showing-correctly – JR Tan Apr 02 '14 at 17:52
  • @TanJitRen I don't think that issue is related to import project settings. It looks like related to incorrectly build gradle project. As I mentioned there, you can try using this manager-plugin by Jake Wharton. https://github.com/JakeWharton/sdk-manager-plugin – shaktiman_droid Apr 02 '14 at 18:02
  • I tried with the manager-plugin by Jake Wharton or create a new project and the import module still not available. I'm using classpath 'com.android.tools.build:gradle:0.9.+' classpath 'com.jakewharton.sdkmanager:gradle-plugin:0.9.+' – JR Tan Apr 03 '14 at 03:44
  • @JabbarJigariyo You mention something about the gradle project being build or setup incorrectly, but I don't see what could be wrong with how it is. I'm having the same issue as the link that Tan-jit-ren mentioned. Essentially Import Module performs like the old version of Import Project (opens a new Window) and the Project Structure window doesn't have an Import option, only a New option. Do you have any idea on what the gradle issue might be? I also just tried with a new project, sadly that produced the same result. – AkiAki007 Apr 03 '14 at 16:22
  • It seems like there might have been a bug in 0.5.3. I just installed 0.5.4 a minute ago and while Project Structure doesn't allow import, one can perform the import via File -> Import Module now and that works! Woo hoo! – AkiAki007 Apr 03 '14 at 16:41
  • 2
    I spoke too soon. You can indeed import a module now, but it seems to make a copy of the entire module rather than linking to a module outside of the project structure. Any changes you make are local to new copy. So, this is only slightly useful really. It is more useful than not having the ability at all, but still pretty useless if you want to have a Library that is in a sep. repo than your App, etc. – AkiAki007 Apr 03 '14 at 16:47
  • I found an answer to my problem via http://stackoverflow.com/questions/17479076/android-studio-add-external-project-to-build-gradle - That might help OP and other users that would like to actually *link* to a module that is outside of the project structure (different repo, etc). – AkiAki007 Apr 03 '14 at 17:20
  • @AkiAki007 So it wasn't a problem with 0.5.3 but how the project has been structured. That's what I was trying to say about incorrect setup. I'm glad you found the solution :) – shaktiman_droid Apr 03 '14 at 17:31
  • @JabbarJigariyo Right, but nothing changed for me between 0.5.3 and 0.5.4 where the Import functionality actually performed an import (though not the way I thought it would). At least I was able to edit the gradle file to do the import as I thought it should happen. – AkiAki007 Apr 03 '14 at 18:25
  • @AkiAki007 Yeah. It happened to me too. As they said they fixed many bugs in 0.5.4. This might be one of them. Most probably related to how Gradle sync work. – shaktiman_droid Apr 03 '14 at 19:49
  • @AkiAki007 I've found a way to re-use a module in multiple projects. I created (NTFS Junction) folder links using an application called winbolic link for Windows. When I have a module that I depend on in multiple projects, I create a folder link to the to-be-reused project inside the directory of each project in which to use it. After this I add the module dependency (in Andr. Studio). Now I can use/edit the module in each project while keeping is in sync. Not sure if it is the right way to go, but for now it seems to work quite well! This works better than a local maven repo. – Peter Apr 29 '14 at 07:57
  • @peterman A little late on the reply here, but I was away for a bit. The most recent versions of Android Studio allow module importing to work rather well and I've been able to create a lib module and add that into my application module. – AkiAki007 May 19 '14 at 19:44
0

I had the same problem when using version 0.3.6. Finally I decided to go back to 0.3.2. and now it works. I suppose AS restricts the ability to connect so to say inhomogeneous projects. I have an (Android/Gradle) project0 an a (Java/Maven) project1. When trying to import project1 as a module of project0 I failed, because the only "option" I had was to create a new module. When I tried to import the same module (project1: Java/Maven) for a Java/Gradle project, there was no problem at all.

Maybe the reason is the Android/Gradle or even just Android nature of a project, but I can't for sure verify my assumption right now.

Kumiho
  • 121
  • 4