如何在 Android Studio 项目中包含库模块依赖项? [英] How to include a library module dependency in an Android Studio project?

查看:52
本文介绍了如何在 Android Studio 项目中包含库模块依赖项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一个项目从 Eclipse 迁移到 AndroidStudio.我有一个项目用作这个项目中的库.这个库叫做 PullToRefresh.

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.

我尝试了很多方法来将这个项目导入到 AS,但我尝试的任何方法都有效.

I've tried many ways to import this project to AS, but anyting I try works.

在我的项目中,我有这个文件夹结构:

In my project I have this folder structure:

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

在 build.gradle 中,我尝试这样做:

In the build.gradle, I've tried to do this:

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

但我收到此错误消息:

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

推荐答案

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

Android Studio works on project-modules concept,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.

稍微改变你的项目结构:

Change your project structure a little bit :

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

在您的 settings.gradle

include ':libs:PullToRefresh'

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

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 文件中:

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

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

这篇关于如何在 Android Studio 项目中包含库模块依赖项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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