Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a library project to the Android Studio and use it?(Some asked dont't take effect)

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.

like image 550
Marshall Avatar asked May 18 '13 08:05

Marshall


People also ask

How do I add a library project to Android Studio?

Navigate to File > Project Structure > Dependencies. In the Declared Dependencies tab, click and select Library Dependency in the dropdown. In the Add Library Dependency dialog, use the search box to find the library to add.

How do I add library dependencies?

To add a dependency on a library that is an annotation processor, you must add it to the annotation processor classpath using the annotationProcessor configuration. That's because using this configuration improves build performance by separating the compile classpath from the annotation processor classpath.

Where would we generally add external libraries that are used in our Android project?

Method 1: Copy external library to the libs folder Now click on the Android drop-down and change to Project files. Click on Project files. Copy the jar file to the libs folder. Here, you can see our added jar file under the libs folder.


2 Answers

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 :)

like image 167
Thibault D. Avatar answered Oct 15 '22 02:10

Thibault D.


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
}
like image 41
ChinLoong Avatar answered Oct 15 '22 03:10

ChinLoong