简体   繁体   中英

Android Gradle : Creating a library project and using it in a project

The gradle documentation (as well as many SO resources I see) suggest:

"A multi-project setup usually works by having all the projects as sub folders of a given root project."

In my situation, I have the following folder structure:

  AndroidDev /* this is my root dir where I keep all my projects and libraries */
    |
    |- MyProject1
    |     |- MyProject1Module
    |
    |- MyProject2
    |     |- MyProject2Module
    |
    |- MyLibrary
         |- MyLibraryModule

Now, I want to use MyLibraryModule and add it as a dependency to MyProject1 as well as MyProject2. I want to open MyProject1 and MyProject2 in separate Android Studio windows and be able to compile them with the library dependency. They have nothing in common.

How can I achieve this. I'm not able to find a good resource to help me understand this.

EDIT: To be clear, I want to use this with the new gradle build system. So any pointers on importing modules and configuring module dependencies would be helpful. Thanks.

If I understand your question correctly, then the only thing you need to do is:

In eclipse select your MyLibraryModule project -> right click -> Properties -> Android Here you will see a checkbox 'Is Library', enable that and apply.

Now in Workspace1 with MyProject1 import MyLibraryModule as existing android application. Right click MyProject1 -> Properties -> Android -> Add. In the now open dialog you should see MyLibraryModule. Add it, Apply and Ok.

The exact same process goes for Workspace2 with MyProject2...

This effectively does the same as if you added a jar to the libs folder, only now you are able to change the code in MyLibraryModule without having to export etc. And the two projects lives on undependent of each other but dependent on the MyLibraryModule.

I don't use Android Studio, and I haven't done any Android development, but this seems to be a question that only requires Gradle knowledge, so I'll give it a go.

First, take a look at this: Having difficulty setting up Gradle multiproject build for existing repository layout from when I asked a similar question a while ago. Peter Niederwieser's answer corrected a whole host of misconceptions that I had about Gradle multiproject support.

You'll need a settings.gradle in the root directory of the structure, which in your case will look rather like this:

include 'MyProject1', 'MyProject2', 'MyLibrary'

Bear in mind that Gradle uses this file to configure a Settings object. You can see what's possible by looking at the DSL for this here: http://www.gradle.org/docs/current/dsl/org.gradle.api.initialization.Settings.html

Now you can declare a dependency on MyLibrary in each of the other projects like this:

dependencies {
    compile(
        ':MyLibrary'
    )
}

I hope this answers the question you were asking - feel free to post again if I've misunderstood.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM