简体   繁体   English

如何在Android Studio项目中包括库模块依赖项?

[英]How to include a library module dependency in an Android Studio project?

I am migrating a project from Eclipse to AndroidStudio. 我正在将一个项目从Eclipse迁移到AndroidStudio。 I have a project used as a lib in this project. 我在该项目中有一个用作lib的项目。 This lib is called PullToRefresh. 该库称为PullToRefresh。

I've tried many ways to import this project to AS, but anyting I try works. 我尝试了多种方法将此项目导入到AS,但是我尝试的任何方法都可行。

In my project I have this folder structure: 在我的项目中,我具有以下文件夹结构:

Project Root
+-- app
|   +-- builds
|   +-- libs
|   |   +-- PullToRefresh (my lib project)
|   +-- src
|   |   +-- main (java code and resources)

In the build.gradle, I've tried to do this: 在build.gradle中,我尝试执行以下操作:

dependencies {
    compile project(":libs:PullToRefresh")
}

But I'm getting this error message: 但我收到此错误消息:

Gradle 'my_project' project refresh failed: Project with path ':libs:PullToRefresh'
could not be found in project ':app'

Android Studio works on project-modules concept,All your modules should be inside a root directory(Your Project Directory). Android Studio在项目模块概念上工作,所有模块都应位于根目录(您的项目目录)内。 One module can be depended on other module/modules. 一个模块可以依赖于其他模块。 Your libraries are considered as different modules under same project and your main module(app in your case) depends on them. 您的库被视为同一项目下的不同模块,而您的主模块(在您的情况下为app)取决于它们。

Change your project structure a little bit : 稍微更改项目结构:

Project Root
+-- libs
    +-- PullToRefresh (my lib project)
+-- app
|   +-- builds
|   +-- src
|   |   +-- main (java code and resources)
    +-- .....
+--settings.gradle

Include this line in your settings.gradle 在您的settings.gradle包括此行

include ':libs:PullToRefresh'

Your build.gradle looks fine. 您的build.gradle看起来不错。 I suggest you to change your directory name from libs to library because use libs for your jar dependency not for module dependencies. 我建议您将目录名从libs更改为library,因为将libs用于jar依赖项而不是模块依赖项。

and keep this in your main module's build.gradle file : 并将其保存在主模块的build.gradle文件中:

dependencies {
    compile project(":libs:PullToRefresh")
}

从“帮助”菜单选项中搜索“导入模块”,然后将出现一个向导!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM