Sharing an Android library between multiple Android apps using Gradle

为君一笑 提交于 2019-12-06 20:52:30

问题


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?


回答1:


Your best bet is to go the aar route and publish it to maven http://www.vandalsoftware.com/post/52468430435/publishing-an-android-library-aar-to-a-maven




回答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




回答3:


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 {
}


来源:https://stackoverflow.com/questions/21050212/sharing-an-android-library-between-multiple-android-apps-using-gradle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!