Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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..

like image 918
korrekorre Avatar asked Jan 09 '15 17:01

korrekorre


2 Answers

You should have three git repos for this. App1, App2 and Common.

Make common a library project using gradle.

apply plugin: 'com.android.library'

You can use the android maven plugin to build the aar locally and make it available to each child app. Add classpath 'com.github.dcendents:android-maven-plugin:1.2' AND apply plugin: 'com.github.dcendents.android-maven'

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.0.0'
        classpath 'com.github.dcendents:android-maven-plugin:1.2'
    }
}

apply plugin: 'com.github.dcendents.android-maven'

Define group and version of library app

group = 'com.foo.example'
version = '1.0.0-SNAPSHOT'

Install library app locally using ./gradlew installDebug

Child Apps

Now the parent app can be included in the child app as a dependency. Add dependency to build.gradle.

compile('com.foo.example:library:1.0.0-SNAPSHOT@aar') {transitive = true}

Each time you make a change to the library app you will have to reinstall it. Then you have to resync the gradle files in your children app. Tools -> Android -> Sync Project With Gradle Files

like image 44
Adam W Avatar answered Oct 08 '22 17:10

Adam W


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')
    :
    :
}
like image 141
amenon Avatar answered Oct 08 '22 15:10

amenon