Android Studio:“新模块 -> 导入现有项目”与“导入模块”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26349721/
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 11:16:57  来源:igfitidea点击:

Android Studio: “new module -> import existing project” vs. “import module”

androidmoduleandroid-studioandroid-gradle-pluginproject-structure

提问by melvynkim

What I have:

我拥有的:

Four independently working Android modules:

四个独立工作的 Android 模块:

  1. MyProjectMainModule, a main container application, attached to MyProject

  2. MyGradleModule, a library, with all necessary components built during gradlewprocess.

  3. MyPreGradleModule, a library, with src/, res/, AndroidManifest.xml, and pom.xml, without gradle wrapper

  4. MyRawModule, a library, with src/, res/, AndroidManifest.xml, without pom.xml(commonly seen in Ant-based Eclipse projects)

  1. MyProjectMainModule,一个主容器应用程序,附加到 MyProject

  2. MyGradleModule,一个库,在gradlew过程中构建了所有必要的组件。

  3. MyPreGradleModule, 一个库, src/, res/, AndroidManifest.xml, and pom.xml, 没有 gradle 包装器

  4. MyRawModule, 一个库,带src/, res/, AndroidManifest.xml, 不带pom.xml(常见于基于 Ant 的 Eclipse 项目)

What I'd like to achieve:

我想达到的目标:

To import all three modules (i.e. MyGradleModule, MyPreGradleModule, MyRawModule) into MyProjectas dependencies of MyProject. The complete project structure should resemble below project structure:

将所有三个模块(即MyGradleModuleMyPreGradleModuleMyRawModuleMyProject作为MyProject. 完整的项目结构应类似于以下项目结构:

MyProject
|--MyProjectMainModule
|--MyGradleModule
|--MyPreGradleModule
|--MyRawModule

Question:

题:

Realizing all three modules (MyGradleModule, MyPreGradleModule, and MyRawModule) have different structures, what are the most optimal ways to import each modules with minimal efforts?

意识到所有三个模块(MyGradleModuleMyPreGradleModuleMyRawModule)具有不同的结构,以最少的努力导入每个模块的最佳方法是什么?

Could you please match one of the following Android Studio menu items with each modules in your answer (if you use any):

您能否将以下 Android Studio 菜单项之一与您的答案中的每个模块匹配(如果您使用任何模块):

  1. File-> Import Module...
  2. File-> New Module...-> Import Existing Project
  3. File-> New Module...-> Android Library
  1. File-> Import Module...
  2. File-> New Module...->Import Existing Project
  3. File-> New Module...->Android Library

回答by Mike Laren

You can add the three modules to the same project by creating a settings.gradlefile in the MyProject/ folder and adding the modules to it:

您可以通过settings.gradle在 MyProject/ 文件夹中创建一个文件并将模块添加到其中来将三个模块添加到同一个项目中:

include ':MyGradleModule'
include ':MyPreGradleModule'
include ':MyRawModule'

Then for each module, configure the build.gradledependencies to reference the other modules as needed. For example, add this to the MyProjectMainModule to make it use the output produced by MyGradleModule:

然后对于每个模块,配置build.gradle依赖项以根据需要引用其他模块。例如,将此添加到 MyProjectMainModule 以使其使用 MyGradleModule 生成的输出:

dependencies {
  compile project(':MyGradleModule')
}

Finally, if your project has heterogeneous submodules then you can configure their structure using the 'sourceSets' closure. For example, your raw module would have a configuration similar to this:

最后,如果您的项目具有异构子模块,那么您可以使用“sourceSets”闭包配置它们的结构。例如,您的原始模块将具有与此类似的配置:

android {
    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }
        androidTest.setRoot('tests')
    }
}

Check out this section of the Gradle Plugin Guideto see what configuration options are available.

查看Gradle 插件指南的这一部分,了解可用的配置选项。

回答by Prem kumar adepu

This worked out for me:

这对我有用:

  1. Open your project in Android Studio
  2. Download the library (using Git, or a zip archive to unzip)
  3. Go to File > Import Module and import the library as a module
  4. Go to File > Project Structure > Modules
  5. Locate your main project module, click on it. It should have a few android libraries, such as: support-v4, etc.
  6. Click on the more on the left green "+" button > Module dependency
  7. Select "your Library"
  1. 在 Android Studio 中打开您的项目
  2. 下载库(使用 Git 或 zip 存档解压缩)
  3. 转到文件 > 导入模块并将库作为模块导入
  4. 转到文件 > 项目结构 > 模块
  5. 找到您的主项目模块,单击它。它应该有一些android库,例如:support-v4等。
  6. 单击左侧绿色“+”按钮的更多 > 模块依赖项
  7. 选择“你的图书馆”

回答by Ambilpura Sunil Kumar

enter image description hereFor Adding Module into Android Studio

在此处输入图片说明用于将模块添加到 Android Studio

  1. File>>New>>Import Module>>select module, click on finish

  2. Goto settings.gradlefile and add the module name as below separated by comma i.e. include ':app', ':payUMoneysdk'

  3. Now module is added in the project see in the app root folder.

  4. Goto project setting and click on dependencies click on the plus (+) symbol and choose the module, click on ok.

  5. The following module is added in the build.gradlewith name implementation project(':payUMoneysdk')

  1. File>>New>>Import Module>>select module, 点击 finish

  2. 转到settings.gradle文件并添加如下模块名称,以逗号分隔,即include ':app', ':payUMoneysdk'

  3. 现在模块已添加到项目中,请参见应用程序根文件夹。

  4. 转到项目设置并单击依赖项 单击加号 ( +) 符号并选择模块,单击ok

  5. build.gradlewith name 实现项目(':payUMoneysdk')中添加如下模块

Here i am sharing the image for your reference... i added PayUmoney and Mobilehelpsdk modules in my project its working properly..

我在这里分享图像供您参考......我在我的项目中添加了 PayUmoney 和 Mobilehelpsdk 模块,它可以正常工作......

回答by PravyNandas

Adding the module in Settings.Gradle file fixed issue for me.

在 Settings.Gradle 文件中添加模块为我解决了问题。

include ':app' include ':Other-Module-Folder'

包括“:应用程序”包括“:其他模块文件夹”

回答by kolawole

did you try this? https://developer.android.com/studio/projects/android-library.

你试过这个吗? https://developer.android.com/studio/projects/android-library

You need to create a Create a library module and Convert an app module to a library module and then Add your library as a dependency. Check ANT Android project to Android Studio

您需要创建一个创建库模块并将应用程序模块转换为库模块,然后将您的库添加为依赖项。将ANT Android 项目检查到 Android Studio

回答by anoopbryan2

If the above answers not clear to you or you are confused So please follow these simple steps.

如果以上答案您不清楚或者您感到困惑,请按照以下简单步骤操作。

  1. Go to File > New > Import Module...
  2. Select the source directory where is your library or module present.
  3. Give module name or leave it as default and then click on finish and your module is added successfully.
  1. 转到文件 > 新建 > 导入模块...
  2. 选择您的库或模块所在的源目录。
  3. 提供模块名称或保留默认值,然后单击完成,您的模块已成功添加。