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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22243269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:42:08  来源:igfitidea点击:

How to share a single library source across multiple projects

androidandroid-studio

提问by Neoh

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.

问题如标题。在这里一个类似的问题,当时唯一的解决方法是将项目发布到本地 Maven 存储库中。

Is this problem fixed (as claimed by some) in Android Studio 0.5.+? In its release notethere 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?

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

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

我试过 File->Import Project.. 但它不起作用。

EDIT 2: See accepted answer for latest solution (as of 0.8.+)

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

EDIT:

编辑:

My project directory structure has only one module mainwhich looks like this

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

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

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

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

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

The following line is added into MyApp/settings.gradle:

将以下行添加到MyApp/settings.gradle

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

The following is added into MyApp/main/build.gradle:

以下内容添加到MyApp/main/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.

As a side note, the library project MyLibraryitself can compile without error. The problem seems to be settings.gradlenot being able to recognize the library project structure.

附带说明一下,库项目MyLibrary本身可以编译而不会出错。问题似乎是settings.gradle无法识别库项目结构。

回答by Neoh

(as of version 2.1+):

(从 2.1+ 版本开始):

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:

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

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")

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

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).

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

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

3)在build.gradle你的库模块文件夹里面,改变顶行

apply plugin: 'com.android.application'

to

apply plugin: 'com.android.library'

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:

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

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

4)如果是新建工程,首先新建工程File->new Project->ApplicationName。

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):

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

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

6) Then go to File->Project Structure.

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

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

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

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

您现在应该能够在您的应用程序中使用库类。



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

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

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

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

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

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

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

2)像往常一样,将模块名称(在android studio中右键单击module->refactor->rename)重构为不那么令人困惑的东西,例如

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

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

3) Modify the settings.gradlein MainProject:

3)修改settings.gradleMainProject

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

Sync the project and you're done.

同步项目,你就完成了。



Note on Proguard

关于 Proguard 的注意事项

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

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

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

with the following (in build.gradleof the library):

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

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

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

proguard-project.txt包含库项目的 proguard 规则的文件在哪里。

回答by Scott Barta

Yes, it works now in 0.5.0. There isn't a friendly UI to it (Import Project isn't what you want, as that creates an entirely new project; Import Module is still broken; see https://code.google.com/p/android/issues/detail?id=62122), but you can set up your build files to make it work.

是的,它现在可以在 0.5.0 中使用。它没有友好的用户界面(导入项目不是您想要的,因为它创建了一个全新的项目;导入模块仍然损坏;请参阅https://code.google.com/p/android/issues/ detail?id=62122),但您可以设置构建文件以使其工作。

Let's say you have a directory structure that looks like this:

假设您有一个如下所示的目录结构:

MyApp
    appmodule
        build.gradle
            src
                main
                    java
    build.gradle
    settings.gradle
MyPlainJavaLibrary
    build.gradle
    src
        java

Note that MyPlainJavaLibraryisn't underneath the MyAppdirectory in the filesystem.

请注意,MyPlainJavaLibrary不在文件系统中的MyApp目录下。

Set up your settings.gradlefile like so:

像这样设置你的settings.gradle文件:

include ':appmodule', '..:MyPlainJavaLibrary'

and include a dependency to it in build.gradlelike this (don't forget the leading colon):

并像这样在build.gradle 中包含对它的依赖(不要忘记前导冒号):

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

This works for me. In my Project viewer, I now see MyApp and MyPlainJavaLibrary as two root-level module directories in the view, and I can make java importstatements work across module boundaries and such.

这对我有用。在我的项目查看器中,我现在将 MyApp 和 MyPlainJavaLibrary 视为视图中的两个根级模块目录,并且我可以使 javaimport语句跨模块边界等工作。

Note that in this default setup, this shared module will only have a single build output folder that will be shared among all the projects using it. This may or may not be what you want. If you'd like to have a different output directory for each project using the shared module, you can probably do some magic with the output directory in the sourceSet; if you want help with this, I'd recommend trying it out on your own and posting a question with details on your work if you get stuck.

请注意,在此默认设置中,此共享模块将只有一个构建输出文件夹,该文件夹将在使用它的所有项目之间共享。这可能是也可能不是您想要的。如果您想使用共享模块为每个项目使用不同的输出目录,您可以使用 ; 中的输出目录做一些魔术sourceSet。如果您需要这方面的帮助,我建议您自己尝试一下,并在遇到问题时发布有关您工作详细信息的问题。

You can only have one settings.gradlefile per project, so this isn't going to take a whole other multimodule Gradle project and bring in all the modules in one fell swoop; you'll need to bring them in individually. However, it should satisfy the use case of using a module in multiple projects.

每个项目只能有一个settings.gradle文件,因此这不会占用一个完整的其他多模块 Gradle 项目并一举引入所有模块;你需要把它们单独带进来。但是,它应该满足在多个项目中使用一个模块的用例。

回答by Opus1217

I wanted to do something similar for my Android Wear project, where you have mobileand wearmodules. In Android Studio 1.5.1, you can do File->New->Module and pick Android Library as your module. This will take care of many of the settings described above and put your library insideyour existing Android Wear project.

我想为我的 Android Wear 项目做一些类似的事情,你有mobilewear模块。在 Android Studio 1.5.1 中,您可以执行 File->New->Module 并选择 Android Library 作为您的模块。这将处理上述许多设置,并将您的库放入现有的 Android Wear 项目中。

回答by ErstwhileIII

To make full use of Android Studio, I would recommend initially creating a normal android project. Then, within that project, add a Module "android library" type, and (optionally) a "java library" (if you have code you want to use that may be common to Android applications and to server side code).

为了充分利用Android Studio,我建议最初创建一个普通的 android 项目。然后,在该项目中,添加一个 Module " android library" 类型和(可选)一个 " java library"(如果您要使用可能对 Android 应用程序和服务器端代码通用的代码)。

Using this mechanism, you do not need to manipulate and modify gradle and manifest files.

使用此机制,您无需操作和修改 gradle 和 manifest 文件。