如何分享在多个项目的单一库的源 [英] How to share a single library source across multiple projects

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

问题描述

问题作为标题。类似的问题被问<一href="http://stackoverflow.com/questions/21592647/android-studio-external-library-projects">here,而唯一的解决方法,时间将项目发布到本地仓库。

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.

时的这个问题修复机器人工作室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 +)

编辑:

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

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 /主/ build.gradle

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

结果是以下错误:

The result is the following error:

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.

推荐答案

(为版本1.3.1 +):

下面是我走上共享项目目录之外的库源的步骤。相反,普通的Java库,我的codeS被编译为模块的Andr​​oid,但该库模块没有包含在主要的项目中。这是我没意见,只要不存在code重复,我只需要维护一组库code对所有项目:

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的工作室创建命名为应用程序的项目文件夹内的模块。为prevent名称与实际应用模块碰撞,重命名模块别的东西(右键点击应用程序模块,在左面板 - > Refactor->重命名)。

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 您的库模块文件夹内,更改前行

apply plugin: 'com.android.application'

apply plugin: 'com.android.library'

然后删除在defaultConfig的的applicationID行。此外,由于这是一个库,删除 xlmns:... 命名空间和&lt;应用..&GT; 身体的Manifest.xml为好。 这是所有的库的一部分。现在,创建/修改的主要应用:

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)如果它是一个新的项目,首先要创建新的项目文件 - >新建项目 - >应用程序名称。

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)像往常一样,重构模块名称(在机器人工作室右键单击模块 - > refactor->重命名)的东西不太容易混淆,如

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)修改 settings.gradle MainProject

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

同步的项目,你就大功告成了。

Sync the project and you're done.

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

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