如何跨多个项目共享单个库源 [英] How to share a single library source across multiple projects

查看:24
本文介绍了如何跨多个项目共享单个库源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题如标题.在此处问了一个类似的问题,当时唯一的解决方法是发布项目到本地 Maven 存储库中.

Question as in title. A similar question was asked here, and the only workaround that time was to publish the project into a local Maven repository.

这个问题是否在 Android Studio 0.5.+ 中修复(如某些人所说)?在其 发行说明 中,有一条声明说支持模块内容根目录之外的源文件夹".这是否意味着我们终于可以从项目文件夹外部导入库?

Is this problem fixed (as claimed by some) in Android Studio 0.5.+? In its release note there is a statement that says "Support for source folders outside the module content root". Does that mean we can finally import the library from outside the project folder?

我试过文件->导入项目..但它不起作用.

I tried File->Import Project.. but it doesn't work.

编辑 2:查看最新解决方案的已接受答案(从 0.8.+ 开始)

我的项目目录结构只有一个模块main,看起来像这样

My project directory structure has only one module main which looks like this

MyApp
    main
        build.gradle
        src
    build.gradle
    settings.gradle

库项目目录只有一个名为lib的模块(都是新建库项目时自动生成的)

The library project directory has only one module named like lib (they are all auto-generated when creating a new library project)

MyLibrary
    lib
        build.gradle
        src
    build.gradle
    settings.gradle

MyApp/settings.gradle中添加以下行:

include ':main', '..:MyLibrary'

MyApp/main/build.gradle中添加以下内容:

dependencies {
    compile project(':..:MyLibrary')
}

结果是如下错误:

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring project ':main'.
> Configuration with name 'default' not found.

作为旁注,库项目 MyLibrary 本身可以编译而不会出错.问题似乎是 settings.gradle 无法识别库项目结构.

As a side note, the library project MyLibrary itself can compile without error. The problem seems to be settings.gradle not being able to recognize the library project structure.

推荐答案

(从 2.1+ 版本开始):

以下是我在项目目录之外共享库源所采取的步骤.我的代码不是纯 Java 库,而是编译为 Android 模块,但该库模块不包含在主项目中.只要没有代码重复我就可以了,我只需要为所有项目维护一套库代码:

Below are the steps that I took to share library source outside of the project directory. Instead of plain Java library, my codes are compiled as Android module, but that library module is not contained inside the main project. It is fine with me as long as there are no code duplications and I only need to maintain one set of library code for all projects:

1) 文件->新建项目.为您的库项目命名(这里我使用 LibraryProject).继续剩下的步骤来创建一个普通的项目(因为这是一个库,我选择了不添加活动")

1) File->new Project. Give a name to your library project (here I use LibraryProject). Continue the remaining steps to create a normal project (since this is intended as a library, I chose "add no activity")

2) 默认情况下,Android Studio 在项目文件夹中创建名为app"的模块.为防止名称与实际应用程序模块发生冲突,请将模块重命名为其他名称(右键单击左侧面板的应用程序"模块-> 重构-> 重命名).

2) By default, Android studio creates the module named as "app" inside the project folder. To prevent names collision with the actual application module, rename the module to something else (Right click "app" module at left panel->Refactor->Rename).

3) 在 build.gradle 在你的库模块文件夹内,更改顶行

3) In the build.gradle inside your library module folder, change the top line

apply plugin: 'com.android.application'

apply plugin: 'com.android.library'

然后删除defaultConfig"下的applicationId"行.此外,由于这是一个库,因此也请从 Manifest.xml 中删除 xlmns:... 命名空间和 正文.这就是图书馆部分的全部内容.现在,创建/修改您的主应用程序:

and then remove the "applicationId" line under "defaultConfig". Also, since this is a library, remove the xlmns:... namespace and <application ..> body from Manifest.xml as well. That's all for the library part. Now, to create/modify your main application:

4) 如果是新建项目,先新建项目File->new Project->ApplicationName.

4) If it is a new project, first create new project File->new Project->ApplicationName.

5) 打开settings.gradle(每个项目中应该只有一个这样的文件)并包含以下行(注意库模块中缺少的前导分号):

5) Open settings.gradle (there should only be one such file in every project) and include the following line (note the missing leading semi-colon in library module):

include ':app', '..:LibraryProject:yourLibraryModule'

6) 然后转到文件->项目结构.

6) Then go to File->Project Structure.

7) 在依赖项"选项卡下,单击右侧的绿色+"按钮.选择模块依赖".选择您的库模块,然后单击确定".

7) Under the tab "Dependencies" click the green "+" button at right. Select "Module dependency". Choose your library module, then click OK.

您现在应该可以在您的应用程序中使用库类了.

You should now be able to use the library classes in your application.

替代方法如果由于某种原因,上述方法仍然存在问题,您可以尝试以下方法(在此处中建议):

ALTERNATIVE METHOD If, for some reason, there are still problems with the above method, you can try the following (suggested in here):

1) 重复上面的步骤 1 到 4.默认情况下,主项目和外部(库)项目如下所示:

1) Repeat steps 1 to 4 above. By default the main and external (library) project look something like this:

  /MainProject
    + build.gradle
    + settings.gradle
    + app/
      + build.gradle
      + src/

  /LibraryProject
    + build.gradle
    + settings.gradle
    + app/
      + build.gradle
      + src/

2) 像往常一样,重构模块名称(在 android studio 中右键单击 module->refactor->rename)以减少混淆,例如

2) As usual, refactor the modules name (in android studio right-click module->refactor->rename) to something less confusing, such as

  /MainProject
    + build.gradle
    + settings.gradle
    + MainModule/
      + build.gradle
      + src/

  /LibraryProject
    + build.gradle
    + settings.gradle
    + LibraryModule/
      + build.gradle
      + src/

3) 修改MainProject中的settings.gradle:

include ':LibraryModule', ':app'
project(':LibraryModule').projectDir = new File(settingsDir, '../LibraryProject/LibraryModule')

同步项目即可完成.

Proguard 的注意事项

目前你不应该在外部库项目/模块上使用 proguard.相反,您替换以下内容(原始答案此处)

Currently you should not use a proguard on external library projects/modules. Instead, you replace the following (original answer here)

buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
    }
    debug {
        minifyEnabled false
    }
}

具有以下内容(在库的 build.gradle 中):

with the following (in build.gradle of the library):

buildTypes {
    release {
        consumerProguardFiles 'proguard-project.txt'
    }
}

其中 proguard-project.txt 是包含库项目的 proguard 规则的文件.

where proguard-project.txt is the file that contains the proguard rules for your library project.

这篇关于如何跨多个项目共享单个库源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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