Like many others, I am trying to generate a single AAR file from my multi-modules Android project.
According to this post, it is not currently supported by the Android team because of various limitations (res & dependencies management, manifest merging, ...).
I am trying to come up with my own solution that will fits my needs. I know that I don't have duplicated resources and external dependencies (like the Support Libraries) are provided by the people using my lib.
My structure is similar to this:
/root
|- lib1
|- lib2
|- lib3
In order to achieve my goal, I created one more library module, named distribute
, that will include all the others modules. I can then share the generated distribute.aar
file.
/root
|- lib1
|- lib2
|- lib3
|- distribute
This is the build.gradle
file of the distribute
module:
apply from: '../common.gradle' // This simply configure properties shared accross all modules
apply plugin: 'com.android.library'
def allModules = rootProject.allprojects.collect { it.name } - [rootProject.name, name]
def whiteLabelDependencies = [
whiteLabel1: allModules - ['lib2'],
whiteLabel2: allModules,
]
android {
def buildTypeNames = buildTypes.collect { it.name }
def whiteLabelTypes = productFlavors.findAll { 'type'.equals(it.dimension) }.collect { it.name }
whiteLabelDependencies.each { whiteLabel ->
def modules = whiteLabel.value
def whiteLabelName = whiteLabel.key
whiteLabelTypes.each { whiteLabelType ->
def productFlavor = whiteLabelName + whiteLabelType.capitalize()
buildTypeNames.each { buildType ->
def sourceSet = productFlavor + buildType.capitalize()
def folders = ['main', whiteLabelName, whiteLabelType, productFlavor, whiteLabelName + buildType.capitalize(), sourceSet]
android.sourceSets.maybeCreate(sourceSet)
android.sourceSets.getByName(sourceSet) {
assets.srcDirs = getPathsList(modules, folders, 'assets')
java.srcDirs = getPathsList(modules, folders, 'java')
res.srcDirs = getPathsList(modules, folders, 'res')
resources.srcDirs = getPathsList(modules, folders, 'resources')
}
}
}
}
}
afterEvaluate {
whiteLabelDependencies.each { whiteLabel ->
def assembleTask = rootProject.tasks.getByPath(":${name}:assemble${whiteLabel.key.capitalize()}")
assembleTask.dependsOn whiteLabel.value.collect { ":${it}:assemble${whiteLabel.key.capitalize()}" }
}
}
dependencies {
whiteLabelDependencies.each { whiteLabel ->
whiteLabel.value.each {
"${whiteLabel.key}Compile" project(path: ":${it}", configuration: project.whiteLabelConfigurationName)
}
}
}
def List<File> getPathsList(List<String> modules, List<String> folders, String sourceType) {
return modules.collect { module ->
folders.collect { folder ->
new File("${rootProject.rootDir}/${module}/src/${folder}/${sourceType}")
}.findAll {
it.exists()
}
}.flatten()
}
This seem to be almost working, except that at runtime, the application crash because it can not find the R
class of each libx
module, only the one of the distribute
module. Is there a way to force Gradle to include the R
class of each module.
I know that the file for each module is generated in $module/build/generated/source/r/$whiteLabel/$buildType/$package/$module/R.java
, but I don't know how to include it and prevent duplicated classes (each module can depends on an other one).