问题
What I have:
Four independently working Android modules:
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)
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:
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?
Could you please match one of the following Android Studio menu items with each modules in your answer (if you use any):
File
->Import Module...
File
->New Module...
->Import Existing Project
File
->New Module...
->Android Library
回答1:
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:
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:
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:
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 Guide to see what configuration options are available.
回答2:
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"
回答3:
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')
Here i am sharing the image for your reference... i added PayUmoney and Mobilehelpsdk modules in my project its working properly..
回答4:
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.
回答5:
Adding the module in Settings.Gradle file fixed issue for me.
include ':app' include ':Other-Module-Folder'
回答6:
did you try this? 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
来源:https://stackoverflow.com/questions/26349721/android-studio-new-module-import-existing-project-vs-import-module