11

I have Android apps A and B. I want to extract duplicate code from each into a shared library L. How can I do this using Gradle? I've seen a few permutations of this question asked before, but there were very few answers.

The closest/best already-asked question is this one:

Multiple Android apps depending on android library with gradle

The first answer suggests a parent project that encompasses both apps and the library module. This coupling is undesirable, in part because A, B, and L are in their own Git repository, but also because it would involve having a parent project and build files that would be difficult to put in source control (or would involve manual copying of the other projects). Since Android Studio likes a parent build for single-module projects by default...Well, it's just a lot of parents for what should be a simple family. It really seems like an unecessary coupling between projects that are otherwise completely unrelated.

The second answer involves releasing an AAR to a repo and referencing the remote library in each project. I would be fine with this approach as we have a local Nexus repository, but there does not seem to be a simple way to do this without manually versioning/naming and uploading files (this is unsustainable). I'm trying to use the Sonatype gradle-release plugin, but it seems to be for JARs (not AARs).

It just seems like a lot of moving parts for what amounts to the most basic form of code sharing between Android apps. Does anyone have a better solution?

Community
  • 1
  • 1
Tremelune
  • 125
  • 1
  • 9
  • If I go the second route, this seems relevant: http://www.vandalsoftware.com/post/52468430435/publishing-an-android-library-aar-to-a-maven – Tremelune Jan 10 '14 at 17:32

2 Answers2

2

According to official document of Android(https://developer.android.com/studio/projects/android-library.html): By importing module, the library module is copied to your project, so you can actually edit the library code. If you want to maintain a single version of the library code, then this is probably not what you want and you should instead import the compiled AAR file as described above.

There is another option, git submodule. You can make your app refer to a SHA of your library. So there is only one copy for the library. Each app is using only a SHA. You should pay little bit more attention for branch management of the library. Check this for detail: https://github.com/blog/2104-working-with-submodules

Ziwei Zeng
  • 691
  • 5
  • 21
1

I was able to remove a lot of the SonaType auth stuff, since we have our own Nexus repo that doesn't use it. My gradle.build file (the module file, not the root) ended up looking like this:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.7.1'
    }
}
apply plugin: 'android-library'
apply plugin: 'maven'

version = 0.3
group = "com.whatevs.android.commons"

uploadArchives {
    repositories.mavenDeployer {

        repository(url: 'http://nexus.whatevs.net:8081/nexus/content/repositories/internal-release') {
        }

        pom.project {
            packaging 'aar'

            scm {
                url 'scm:git:ssh://git@whatevs.net:7999/Mobile/android-commons.git'
                connection 'scm:git:ssh://git@whatevs.net:7999/Mobile/android-commons.git'
                developerConnection 'scm:git:ssh://git@whatevs.net:7999/Mobile/android-commons.git'
            }
        }
    }
}

repositories {
    mavenCentral()
}

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.0"

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 19
    }
    release {
        runProguard false
        proguardFile 'proguard-rules.txt'
        proguardFile getDefaultProguardFile('proguard-android.txt')
    }
}

dependencies {
}
Tremelune
  • 125
  • 1
  • 9
  • The only problem is that the archive upload is under com.whatevs.android.commons/Common/version, and I don't really want the "Common" in there. Not sure how to kill that. – Tremelune Jan 13 '14 at 18:45