19

How do I add a library project to the Android Studio and use it?(Some asked question dont't take effect) I try to found answer on How do I add a library project to Android Studio? or How to import relative project with the Android Studio(not jar) . But it does not work.It also says "Error retrieving parent for item: No resource found that matches the given name 'Theme.Sherlock.Light'." etc.

My sdk tools rev is 22.

Community
  • 1
  • 1
Marshall
  • 2,231
  • 4
  • 17
  • 15
  • It would help knowing what type of project you are using. Since you seem to be new to Studio/IntelliJ, I'm guessing you are not using an existing IntelliJ project with Android support. If you have an Eclipse Android project, make sure to export it to gradle and import the resulting project. DO NOT import the Eclipse project directly. Then, only modify the build.gradle file to add dependencies. The Studio UI does not do the right think yet. More Gradle info at http://tools.android.com – Xavier Ducrohet May 22 '13 at 04:13

6 Answers6

22

I'll show you how to do so with the map sample.

Say you have imported the map sample in Android Studio, and created the corresponding maps module.

1. Create an android library module

You need to create an additional library module for GMS, which includes all the classes the map sample needs from Google API to compile your map module.

The library project is located at:

$ANDROID_SDK/extras/google/google_play_services/libproject/google-play-services_lib

To do so, go to File -> Import module and select the location described above or the location of your library project.

Then use the right option between Create module from existing sources or Import module from external model. In my case it's from existing sources.

Android Studio detects the src/res directories, so far so good.

It also detects the library directories, which will save you the manual import of any jar you may have in lib/ (Like google-play-services.jar in my case).

[EDIT: It does actually save you only half the time because it creates the libraries but don't link them in you module dependencies]

2. Update your main module dependencies - Library module

Now, the tricky part is that you have only created the module, not linked to your project as a dependency.

Do do so, go to File -> Project structure -> Modules

Select your main module, maps in my case, and go to the dependencies tab on the right hand side. Click the + and 3. Module dependency.

Select the module your main module depends on and click OK.

3. Update your main module dependencies - Jars

Now for unexplained reason the activities in my maps module still cannot find the API classes. I also have to manually add the google-play-services.jar dependency on the Module configuration (as in 2., but select Library instead of Module dependency);

This point can surely be automatized with gradle but I haven't yet looked into it. When you use a new IDE, the fewer tools you use, the easier it is to track configuration errors. I'd suggest adding only one tool at a time :)

Thibault D.
  • 10,041
  • 3
  • 25
  • 56
  • Iv'e followed exactly as you said, still facing this problem: java: package com.google.android.maps does not exist....any idea why? – Ranco May 18 '13 at 13:23
  • Hum. Try this one as well : http://stackoverflow.com/questions/16596715/how-can-i-create-an-android-application-in-android-studio-that-uses-the-google-m in case I have forgotten something (Just came by this one) – Thibault D. May 18 '13 at 15:43
  • Are you building with Gradle? (new project created in Studio use Gradle). If yes, the answer above is completely wrong. Only add dependencies through build.gradle, do not use the UI in the IDE. – Xavier Ducrohet May 22 '13 at 04:15
  • Read the last paragraph, it explains why I don't use gradle yet. – Thibault D. May 23 '13 at 11:57
7

The steps below worked for me in Android Studio 1.3.1

mylib1 is in standalone project.

In my project that needs mylib1

1) Edit settings.gradle file

include ':app'
include ':mylib1' ##New line
project(':mylib1').projectDir = new File('../../android_lib_repo/mylib1/app/') ##New line

where ../../android_lib_repo/mylib1 is project directory

where ../../android_lib_repo/mylib1/app/ is module directory

2) Edit build.gradle file

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'org.bouncycastle:bcprov-jdk16:1.46'
    compile 'com.google.android.gms:play-services:6.5.87'
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile 'com.android.support:support-v4:21.0.3'
    compile project(':mylib1') ##New line
}
ChinLoong
  • 1,735
  • 24
  • 26
  • If your lib project is created by android studio guid, the directory you need to add may be "../../android_lib_repo/mylib1". And I found this link discuss more about this [issue](http://stackoverflow.com/questions/17479076/android-studio-add-external-project-to-build-gradle) And I met a problem about duplicate files when package APK after using external lib, which solved [here](http://stackoverflow.com/questions/20673625/android-gradle-plugin-0-7-0-duplicate-files-during-packaging-of-apk) – cuble Mar 21 '16 at 03:24
2

After you import the 'library' module in your project (from what i have understood so far, your workspace is the project, and the projects are the modules in intellij / Android studio),

  1. Go to File -> Project structure -> Modules or select the project and hit F4.-> Modules
  2. Select your 'library' module (in the middle column) and click dependencies tab (in the right most column)
  3. Click the checkbox (Export) before the module name. (Check it)
  4. Now, select 'your' module and click the + sign in the left pane and select "3. Module Dependencies...", select your 'library' project and click OK.
  5. Compile your modules now.

If you still see errors in your project saying cannot find classes from the 'library' project, then make sure you check the 'export' checkbox infront of the module in your 'library' module dependency. Hope that helps :)

wildnux
  • 408
  • 1
  • 7
  • 22
  • This only works if you are not using the new Gradle project. For new Gradle project, please edit build.gradle, do not edit the dependencies in the UI. – Xavier Ducrohet May 22 '13 at 04:13
  • 1
    Project Structure -> Modules is gone in newer versions of Android Studio (0.2.10 and beyond, IIRC). For now you'll have to do this stuff by hand in your build.gradle file(s). –  Oct 06 '13 at 14:14
  • What's the point of having an IDE if I have to edit build scripts by hand. This is ridiculous. – Almo Mar 18 '15 at 18:55
1

For anyone who may just be finding this. If you need access to the resources of a library project (as you do with ABS) ensure that you add the library project/module as a "Module Dependency" instead of a "Library".

drewan50
  • 294
  • 1
  • 7
1

If you are using a project created in Studio or a project exported from ADT to Gradle, you will need to edit build.gradle to change dependencies. The UI in the Project Structure dialog only affect the dependencies in the IDE, but the will uses Gradle which will not have the dependencies setup.

Editing build.gradle will make Studio reload it updating the dependencies in both places.

For more information about dependencies, and multi-module setup (in case you are also adding a module as a dependency), please read http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Dependencies-Android-Libraries-and-Multi-project-setup

Xavier Ducrohet
  • 28,383
  • 5
  • 88
  • 64
1

Editing library dependencies through GUI is not advisable as that doesn't writes those changes to your build.gradle file. So your project will not build from command-line. We should edit build.gradle file directly as follows.

For instance, given to following structure:

MyProject/

  • app/
  • libraries/
    • lib1/
    • lib2/

We can identify 3 projects. Gradle will reference them with the following name:

  1. :app
  2. :libraries:lib1
  3. :libraries:lib2

The :app project is likely to depend on the libraries, and this is done by declaring the following dependencies:

dependencies {
    compile project(':libraries:lib1')
}
Shakti Malik
  • 2,335
  • 25
  • 32