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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21170395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:04:50  来源:igfitidea点击:

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

androidandroid-studiogradleandroid-gradle-pluginbuild.gradle

提问by androidevil

I am migrating a project from Eclipse to AndroidStudio. I have a project used as a lib in this project. This lib is called PullToRefresh.

我正在将一个项目从 Eclipse 迁移到 AndroidStudio。我有一个项目用作这个项目中的库。这个库被称为 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'

回答by pyus13

Android Studio works on project-modulesconcept,All your modules should be inside a root directory(Your Project Directory). 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.

Android Studio 基于项目模块的概念,你的所有模块都应该在一个根目录(你的项目目录)中。一个模块可以依赖于其他模块/模块。您的库被视为同一项目下的不同模块,而您的主模块(在您的情况下为应用程序)取决于它们。

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. I suggest you to change your directory name from libs to library because use libs for your jar dependency not for module dependencies.

你的 build.gradle 看起来不错。我建议您将目录名称从 libs 更改为 library,因为将 libs 用于 jar 依赖项而不是模块依赖项。

and keep this in your main module's build.gradle file :

并将其保存在主模块的 build.gradle 文件中:

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

回答by AlinaBM

From the "Help" menu option search for "import module" and then a wizard will appear!

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