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?
Unlike JAR files, AAR files offer the following functionality for Android applications: AAR files can contain Android resources and a manifest file, which allows you to bundle in shared resources like layouts and drawables in addition to Java classes and methods.
Yes, it is possible. As the existing answers showed, it's quite straightforward to create additional application module in the same Android Studio project. So I'll try to answer underlying question why anyone might need it. It's certainly not worth it to put multiple completely independent apps in one project.
aar bundle is the binary distribution of an Android Library Project. An AAR is similar to a JAR file, but it can contain resources as well as compiled byte-code. A AAR file can be included in the build process of an Android application similar to a JAR file.
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
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://[email protected]:7999/Mobile/android-commons.git'
connection 'scm:git:ssh://[email protected]:7999/Mobile/android-commons.git'
developerConnection 'scm:git:ssh://[email protected]: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 {
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With