如何在Android Studio中添加库项目并使用?(有人问不生效) [英] How do I add a library project to the Android Studio and use it?(Some asked dont't take effect)

查看:34
本文介绍了如何在Android Studio中添加库项目并使用?(有人问不生效)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Android Studio中添加库项目并使用?(有些问题不生效)我尝试在 找到答案如何将库项目添加到 Android Studio?如何使用Android Studio(非jar)导入相关项目 .但它不起作用.它还说检索项目的父项时出错:找不到与给定名称'Theme.Sherlock.Light'匹配的资源."等

我的 sdk 工具版本是 22.

解决方案

我将使用地图示例向您展示如何执行此操作.

假设您已经在 Android Studio 中导入了地图示例,并创建了相应的 maps 模块.

1.创建一个android库模块

您需要为 GMS 创建一个额外的库模块,其中包括地图示例从 Google API 编译您的地图模块所需的所有类.

图书馆项目位于:

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

为此,请转到 File -> Import module 并选择上述位置或库项目的位置.

然后在从现有源创建模块从外部模型导入模块之间使用正确的选项.就我而言,它来自现有来源.

Android Studio 检测 src/res 目录,到目前为止一切顺利.

它还检测库目录,这将为您省去手动导入 lib/中可能存在的任何 jar(如我的示例中的 google-play-services.jar).

2.更新您的主要模块依赖项 - 库模块

现在,棘手的部分是您只创建了模块,而不是作为依赖项链接到您的项目.

这样做,去File -> Project structure -> Modules

选择您的主模块,在我的例子中是 maps,然后转到右侧的 dependencies 选项卡.点击+3.模块依赖.

选择您的主模块所依赖的模块,然后单击确定".

3.更新您的主要模块依赖项 - Jars

现在由于无法解释的原因,我的 maps 模块中的活动仍然找不到 API 类.我还必须在模块配置上手动添加 google-play-services.jar 依赖项(如在 2. 中,但选择 Library 而不是 Module 依赖项);

这一点肯定可以用 gradle 自动化,但我还没有研究过.使用新 IDE 时,使用的工具越少,跟踪配置错误就越容易.我建议一次只添加一个工具:)

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.

解决方案

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

这篇关于如何在Android Studio中添加库项目并使用?(有人问不生效)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆