Android studio 添加外部项目到 build.gradle [英] Android studio add external project to build.gradle

查看:46
本文介绍了Android studio 添加外部项目到 build.gradle的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个示例项目,设置如下:

I have a sample project, with the following setup:

/root
  + Pure Java Lib
  + Android Test Lib
  + Android Test Project

其中测试项目"依赖于测试库",最后一个依赖于纯 Java 库"编译项目并启动此设置工作正常.

Where the 'Test Project' is dependent on the 'Test Lib', and the last depends on the 'Pure Java Lib' Compiling the project and launching this setup works just fine.

我现在正在考虑导入我以前的 Eclipse 工作区并使用 Android Studio,问题是项目设置不同,我想保持这种方式.

I'm now thinking about importing my previous Eclipse workspace and work with Android studio, the problem is the project setup is different, and I would like to keep it this way.

例如如果使用前面的例子:

for example if to use the previous example:

/root
  + Android Test Lib
  + Android Test Project

/Some Other folder (another repository for example)
  + Pure Java Lib

我尝试了很多配置,但我没有找到引用父文件夹范围之外的项目的方法(在示例中为root").

I've tried many configurations, but I didn't find a way to reference a project outside the scope of the parent folder ('root' in the example case).

在许多平台/模块中,您可以使用.."在文件夹中向上移动,但这对我不起作用,也许我用错了.

In many platforms/modules you can use the '..' to move up in the folders but this didn't work for me, perhaps I've used it wrong.

有谁知道如何使用 Gradle 实现这一目标?

Does anyone know how this can be achieved with Gradle?

更新

我会尽量更通用:

/C:/

  /Project A
    + Module 1 - Pure Java
    + Module 2 - Android Test Lib
    + Module 3 - Android Test Project

  /Project B
    + Module 1 - Pure Java
    + Module 2 - Pure Java
    + Module 3 - Pure Java

我想使用项目 B 的模块 1,项目 A.

I would like to use Module 1 of project B, in project A.

更新:09-03-19

我现在看到了这个,我必须更新......在将近 6 年之后,今天我更聪明了,我可以肯定地说问题是我误解了真相之源"的概念.

I saw this now and I must update... after almost 6 years, today I am wiser, and I can definitely say the problem was me misunderstanding the concept of "Source of truth".

虽然有一个库的引用是一个很好的概念......并且可能看起来像一个真相来源",真正的真相来源"将是每个项目使用的代码版本库,因为库本身有版本.. 很多版本和事实来源"是相对于使用库的项目.

While having one ref to a library is a nice to have concept.. and may seem like the a "Source of truth", the REAL "Source of truth" would be the version of the code each project is using of that library, cause the library by itself has versions.. many versions an the "Source of truth" is relative to the project which is using the library.

正确的方法是使用大多数开发人员不喜欢的东西,那就是 git 子模块,是的,在每个项目中复制源代码会导致每个项目使用不同版本的代码的可能性最大.

The correct way would be to use what most developers do not like, and that is git submodules, and yes duplicate the sources in each project cause most chances each project uses a different version of the code.

然而,您需要让您的所有项目都使用所有库的最新和最好的版本.这本身就是一个挑战

You would need however to aim for all of your projects to use the latest and greatest version of all your libraries.. which is a challenge by itself

这是使用库源开发项目的正确方法的原因是它可以扩展……您可以拥有数百个项目,每个项目都有自己的库配置.

The reason this is the right way to develop a project with library sources is that this scales... you can have hundreds of project each with its own library configuration.

推荐答案

假设 Some Other Folder 是一个 gradle 项目,您可以在 settings.gradle 文件中添加如下内容:

Assuming that Some Other Folder is a gradle project you could add something like the following to your settings.gradle file:

include ':module1'
project(':module1').projectDir = new File(settingsDir, '../Project B/Module 1')

这篇关于Android studio 添加外部项目到 build.gradle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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