在 Android Studio (v0.8.9) 中引用外部库项目 [英] Referencing external library projects in Android Studio (v0.8.9)

查看:30
本文介绍了在 Android Studio (v0.8.9) 中引用外部库项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义库,需要在大约 10 个项目中引用.在 Eclipse 中一切正常,但我想将它们迁移到 Android Studio 以利用构建系统.我已经查过了

I have a custom library that needs to be referenced in about 10 projects. Everything works fine in Eclipse but I want to migrate them to Android Studio in order to take advantage of the build system. I've already check

Android studio 添加外部项目到 build.gradle

如何跨多个项目共享单个库源

以及 Gradleware 中的其他一些链接和文档,但我无法编译我的项目.

and some other links and docs in Gradleware but I don't get to make my projects compile.

我的文件结构是这样的:

My file structure is like:

+项目

|+图书馆项目

|+---工作区

|+------projectSrc

|+------projectSrc

|

|+project0

|+---工作区

|+------projectSrc0

|+------projectSrc0

|

|+project1

|+---工作区

|+------projectSrc1

|+------projectSrc1

..

|+projectN

|+---工作区

|+------projectSrcN

|+------projectSrcN

每个项目都包含文档、设计文件等...因此将 src 放在同一个根文件夹中并不方便.

Each project contains docs, design files, etc... and therefore is no convenient to have the src in the same root folder.

在我的project1"中,我使用过的settings.gradle 文件:

In my "project1", settings.gradle file I've used:

include ':module-custom-lib'
project(':module-custom-lib').projectDir = new File(settingsDir, '../../../libraryProject/workspace/projectSrc')

在我的 build.gradle 文件中,我添加了依赖项部分:

In my build.gradle file, dependencies section I add:

dependencies {
    compile project(':..:..:..:libraryProject:workspace:projectSrc')
    ....
}

但我收到一条错误消息,指出未找到该项目.

But I get an error saying that the project wasn't found.

我还使用了以下方法:

def customLib = file('../../../libraryProject/workspace/projectSrc')
settings.createProjectDescriptor(rootProjectDescriptor, 'module-custom-lib', customLib)
include ':module-custom-lib'

然后在 build.gradle

dependencies {
    compile project(':module-custom-lib')
    ....
}

请帮忙.感谢您抽出宝贵时间.

Please help. Thanks for your time.

我设法通过在项目目录中添加模块 app 作为后缀来使其工作.:

I've managed to get it working by adding the module app as a suffix in the project dir.:

include ':module-custom-lib'
project(':module-custom-lib').projectDir = new File(settingsDir, '../../../libraryProject/workspace/projectSrc/app')

现在库成员解析完美,但是项目的'R'资源类无法解析.所以如果我这样做:

Now the library members resolve perfectly, but the project's 'R' resource class can not be resolved. So if I do:

setContentView(R.layout.main);

它无法找到该资源.清理和重建项目并不能解决问题.

it fails to find that resource. Cleaning and rebuilding the project doesn't fixes the issue.

编辑 2: 显然,清单合并存在问题,因为一旦我调整了两个项目的 minSdk 和 targetSdk,问题就解决了.以前的解决方案工作得很好.

EDIT 2: Apparently, there was a problem with the manifest merging 'cause the problem was fixed once I adjusted the minSdk and targetSdk of both projects. The previous solution work perfectly.

推荐答案

您的第一次尝试具有更简单的语法,并且应该可以工作.您的 settings.gradle 文件看起来不错:

Your first attempt has a simpler syntax, and should work. Your settings.gradle file looks okay:

include ':module-custom-lib'
project(':module-custom-lib').projectDir = new File(settingsDir, '../../../libraryProject/workspace/projectSrc')

尽管您应该谨慎地在其中包含相对路径——Android Studio 可能不会使用与您从命令行构建的相同的工作目录,因此引用与 rootDir 相关的内容可能会有所帮助缓解问题.

though you should be cautious about including a relative path there -- Android Studio may not use the same working directory as a build you do from the command line, so referencing things relative to rootDir may help alleviate problems.

在模块构建文件的 dependencies 块中,您需要以与在 settings.gradle 中相同的方式引用模块,因此您可以使用类似这个:

In the dependencies block of a module's build file, you need to reference a module the same way you do in settings.gradle, so you'd use something like this:

dependencies {
    compile project(':module-custom-lib')
}

dependencies 块中,您使用 Gradle 的项目命名空间链接到项目,并且由于您已经明确设置了库模块的根目录,因此您无需尝试在此处的文件系统中找到它.:..:..:.. 语法无论如何都无法引用项目根目录之外的模块,就像这个一样.

In dependencies blocks, you're linking to projects using Gradle's project namespace, and since you've already explicitly set up the root directory of the library module, you don't need to try to locate it in the filesystem here. That :..:..:.. syntax wouldn't work anyway to reference a module that's outside of your project's root directory, as this one is.

这篇关于在 Android Studio (v0.8.9) 中引用外部库项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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