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
Android Studio: “new module -> import existing project” vs. “import module”
提问by melvynkim
What I have:
我拥有的:
Four independently working Android modules:
四个独立工作的 Android 模块:
MyProjectMainModule
, a main container application, attached toMyProject
MyGradleModule
, a library, with all necessary components built duringgradlew
process.MyPreGradleModule
, a library, withsrc/
,res/
,AndroidManifest.xml
, andpom.xml
, without gradle wrapperMyRawModule
, a library, withsrc/
,res/
,AndroidManifest.xml
, withoutpom.xml
(commonly seen in Ant-based Eclipse projects)
MyProjectMainModule
,一个主容器应用程序,附加到MyProject
MyGradleModule
,一个库,在gradlew
过程中构建了所有必要的组件。MyPreGradleModule
, 一个库,src/
,res/
,AndroidManifest.xml
, andpom.xml
, 没有 gradle 包装器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 MyProject
as dependencies of MyProject
. The complete project structure should resemble below project structure:
将所有三个模块(即MyGradleModule
、MyPreGradleModule
、MyRawModule
)MyProject
作为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?
意识到所有三个模块(MyGradleModule
、MyPreGradleModule
和MyRawModule
)具有不同的结构,以最少的努力导入每个模块的最佳方法是什么?
Could you please match one of the following Android Studio menu items with each modules in your answer (if you use any):
您能否将以下 Android Studio 菜单项之一与您的答案中的每个模块匹配(如果您使用任何模块):
File
->Import Module...
File
->New Module...
->Import Existing Project
File
->New Module...
->Android Library
File
->Import Module...
File
->New Module...
->Import Existing Project
File
->New Module...
->Android Library
回答by Mike Laren
You can add the three modules to the same project by creating a settings.gradle
file 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.gradle
dependencies 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:
这对我有用:
- Open your project in Android Studio
- Download the library (using Git, or a zip archive to unzip)
- Go to File > Import Module and import the library as a module
- Go to File > Project Structure > Modules
- Locate your main project module, click on it. It should have a few android libraries, such as: support-v4, etc.
- Click on the more on the left green "+" button > Module dependency
- Select "your Library"
- 在 Android Studio 中打开您的项目
- 下载库(使用 Git 或 zip 存档解压缩)
- 转到文件 > 导入模块并将库作为模块导入
- 转到文件 > 项目结构 > 模块
- 找到您的主项目模块,单击它。它应该有一些android库,例如:support-v4等。
- 单击左侧绿色“+”按钮的更多 > 模块依赖项
- 选择“你的图书馆”
回答by Ambilpura Sunil Kumar
For Adding Module into Android Studio
File>>New>>Import Module>>select module
, click onfinish
Goto
settings.gradle
file and add the module name as below separated by comma i.e.include ':app', ':payUMoneysdk'
Now module is added in the project see in the app root folder.
Goto project setting and click on dependencies click on the plus (
+
) symbol and choose the module, click onok
.The following module is added in the
build.gradle
with name implementation project(':payUMoneysdk')
File>>New>>Import Module>>select module
, 点击finish
转到
settings.gradle
文件并添加如下模块名称,以逗号分隔,即include ':app', ':payUMoneysdk'
现在模块已添加到项目中,请参见应用程序根文件夹。
转到项目设置并单击依赖项 单击加号 (
+
) 符号并选择模块,单击ok
。在
build.gradle
with 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.
如果以上答案您不清楚或者您感到困惑,请按照以下简单步骤操作。
- Go to File > New > Import Module...
- Select the source directory where is your library or module present.
- Give module name or leave it as default and then click on finish and your module is added successfully.
- 转到文件 > 新建 > 导入模块...
- 选择您的库或模块所在的源目录。
- 提供模块名称或保留默认值,然后单击完成,您的模块已成功添加。