korrekorre korrekorre - 27 days ago 23
Git Question

Two android projects sharing common module in same repository using gradle

We are creating an app (actually 2) that is split up in 2 separate projects but sharing the same GIT repository (2 separate folders in git root). It is one app for handheld and one for another platform. But they should share some code like Utils, API calls etc.

Folder structure looks like this:

-GIT Root

-- Project (Project)

--- App 1 (Android application)

--- App 2 (Android application)

--- Common (Android library)

App1 and App2 should be able to reach code from common but not the other way of course.

Tried to do like above and using Gradle but it doesn't seem to work. I know that sharing the same git repo for both apps may not be the best way to handle this scenario but I'm having no options here.

Do you think that this is possible to do somehow? (Maybe I'm just not understanding modules correctly or something like that)

I'm new to Gradle which makes this even harder..

Answer

This is not too hard to do if all three projects are in the same Git repository. Here is the skeleton of how your projects should be setup:

App1 (Android application)
    |_ build.gradle
    |_ src
App2 (Android application)
    |_ build.gradle
    |_ src
Common (Android library)
    |_ build.gradle
    |_ src
settings.gradle
build.gradle

The top-level build.gradle file can have common gradle settings that you will use for all sub-projects

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:1.0.0'
    }
}

allprojects {
    repositories {
        mavenCentral()
    }
}

Make sure the settings.gradle file includes all your projects:

include ':App1'
include ':App2'
include ':Common'

Setup Common to be a Android library project. In the build.gradle file for Common add the line:

apply plugin: 'com.android.library'

Setup the other two projects to be Android applications, and include the Common project as a dependency.

apply plugin: 'com.android.application'

dependencies {
    compile project(':Common')
    :
    :
}
Comments