如何向 Android Studio 添加一个库项目?

How do I add a library project (such as Sherlock ABS) to Android Studio?

(Not to the old ADT Eclipse-based bundle, but to the new Android Studio.)

转载于:https://stackoverflow.com/questions/16588064/how-do-i-add-a-library-project-to-android-studio

csdnceshi64
游.程 Here is a video which I found to be really useful: youtube.com/watch?v=1MyBO9z7ojk
5 年多之前 回复
csdnceshi59
ℙℕℤℝ If you're using Gradle, see this StackOverflow answer.
接近 6 年之前 回复
csdnceshi52
妄徒之命 The YouTube video How to Add Libraries to Android Studio explains the process.
6 年多之前 回复
csdnceshi57
perhaps? yeah figuring this out was way more confusing than it should have been... not like it was any better in eclipse.
大约 7 年之前 回复

29个回答

Basically, you can include your JAR files in three different ways. The last one is remote library that is using https://bintray.com/ jcenter online repository. But, if you do it in one of the two other ways, the JAR file will be included physically in your project. Please read this link https://stackoverflow.com/a/35369267/5475941 for more information. In this post I explained how to import your JAR file in Android studio and I explained all possible ways.

In summary, if it is like this (local address), they are downloaded and these JAR files are physically in the project:

enter image description here

But, if it is an internet address like this, they are remote libraries (bintray.com jcenter part) and they will be used remotely:

enter image description here

I hope it helps.

To resolve this problem, you just need to add the abs resource path to your project build file, just like below:

sourceSets {
    main {
        res.srcDirs = ['src/main/res','../../ActionBarSherlock/actionbarsherlock/res']
    }
}

So, I again compile without any errors.

I would consider Dependencies, Android Libraries and Multi-project setup necessary reading. Please take a few minutes to do so.

Particularly, in the case of a non-jar library project, read the following snippet from above source:

Gradle projects can also depend on other gradle projects by using a multi-project setup. A multi-project setup usually works by having all the projects as sub folders of a given root project.

For instance, given to following structure:

MyProject/
 + app/
 + libraries/
    + lib1/
    + lib2/

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

:app
:libraries:lib1
:libraries:lib2

Each projects will have its own build.gradle declaring how it gets built. Additionally, there will be a file called settings.gradle at the root declaring the projects. This gives the following structure:

MyProject/
 | settings.gradle
 + app/
    | build.gradle
 + libraries/
    + lib1/
       | build.gradle
    + lib2/
       | build.gradle

The content of settings.gradle is very simple:

include ':app', ':libraries:lib1', ':libraries:lib2'

This defines which folder is actually a Gradle project.

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

dependencies {
    compile project(':libraries:lib1')
}

Kindly note that there was little or no use of Android Studio GUI to make this happen.

I am currently using git submodules to link the nested library to the actual library git repo to avoid a dependency mess.

csdnceshi65
larry*wei Thanks for the git submodules tip. I was using junction before, but have now switched to SVN externals. Works like a charm.
大约 5 年之前 回复
csdnceshi53
Lotus@ thank you so much for including the only answer that makes sense. every other answer is outdated and just doesn't work. i didn't even try in CS at Virginia Tech because the whole first two years was learning to fckn click around in eclipse. it didn't make any fckn sense, but seeing the gradle result of random clicks in an IDE does.
6 年多之前 回复
csdnceshi75
衫裤跑路 Very helpful and, more importantly, not play to the gui whims of the day.
6 年多之前 回复
csdnceshi59
ℙℕℤℝ Kindly upvote if you found this helpful, so that others can avoid the pain and move along faster.
接近 7 年之前 回复

After importing the ABS Module (from File > Project Structure) and making sure it has Android 2.2 and Support Library v4 as dependencies, I was still getting the following error as you @Alex

Error retrieving parent for item: No resource found that matches the given name 'Theme.Sherlock.Light.DarkActionBar'

I added the newly imported module as a dependency to my main app module and that fixed the problem.

csdnceshi79
python小菜 I'm migrate to IDEA 13 from 12 and have this error too. Only re-import for all my dependencies will help. Manually remove all modules, remove related *.iml files and re-import your libs
大约 7 年之前 回复
csdnceshi52
妄徒之命 Same here. Who can solved this?
大约 7 年之前 回复
csdnceshi57
perhaps? I tried all steps inclusing adding the module as a dependency but I still have Gradle: Error retrieving parent for item: No resource found that matches the given name 'Theme.Sherlock.Light'.. It seems like my main project does not see the resources of the library project (ABS). The IDE however does recognize the references to classes and resources. Screenshot
大约 7 年之前 回复

Simply import the Android library project as a module and in Build.gradle.

Apply plugin: 'com.android.library'

After that, follow these steps:

  1. Right click on Module & select open Module settings
  2. Select dependencies, click on +, select library dependencies, and add the previously imported module.

If you have Android Studio .0.4.0, you can create a new folder in your build path, YourApp/libraries. Copy the JAR file. There in, right click on it and "Add As Library". Now you have a popup. Just select your directory and press OK, and that's it.

An example of succesfully adding another library (PullToRefresh). Also works for ABS libproject.

This SO Question

This post

I also encountered the same problem then I did following things.

  1. I import the library project into my AndroidStudio IDE as a module using menu File -> Import module menus

  2. Then I went to my main module in which I want the library project as a dependent project

  3. Right click on the main module (in my case its name is app) -> open module setting -> go into dependencies tab -> click on + button (you will get it on right side of window) -> click on module dependency -> select your library project from list

Apply the changes and click the OK button.

It worked for me. I hope it will help others too.

Indeed as versions are changing, so is changing the user interface and options available on the menu. After reading most of the answers to these questions I had to guess what would work for Android Studio 1.1.0.

  1. With your mouse, select the project at the main level (this is where it shows the name of your app).

  2. Right click, and select the menu options New, Folder, Assets Folder.

  3. After creating the assets folder, paste or copy in it, whatever JAR file you need for your library.

  4. From Android Studio's main menu (top of the screen) select File -> Project Structure.

  5. Then select your project name and go to the Dependencies tab.

  6. Click on the plus sign (+) on the lower left of the dialog box and select file dependency.

  7. Finally open the recently created assets folder, select the JAR files that you copied, and then click apply and OK.

Clean and rebuild your project.

weixin_41568134
MAO-EYE Thanks for the advise @RajnishMishra. I suspected that, but what do you propose instead?
5 年多之前 回复
csdnceshi55
~Onlooker making copies of jars in assets is not a good idea it will unnecessarily increase apk size
5 年多之前 回复
共29条数据 首页 1 3
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐