Example:
parent module:
allprojects {
ext {
isLocalDep = {
it.name != “unspecified” &&
!it.name.contains(“mybadname”) &&
it.group.startsWith(“mygroup”)
}
createMvnCommand = { String deps ->
def cl = []
if (System.getProperty(‘os.name’).toLowerCase().contains(‘windows’)) {
cl += [‘cmd.exe’, ‘/c’];
}
cl += [“mvn”, “install”, “-f”, “…”, “-DskipTests”, “-am”, “-pl”, “$deps”]
return cl
}
}
child1:
apply plugin: ‘com.android.application’
dependencies {
compile ‘mygroup:dep:1’
}
task installMavenDeps(type: Exec) {
def mvnDeps = configurations.compile.allDependencies
.findAll { isLocalDep(it) }
.collect { dep -> “:” + dep.name }
.join(",")
onlyIf {
!mvnDeps.isEmpty()
}
environment System.env
commandLine (createMvnCommand(mvnDeps))
}
configurations {
res
}
dependencies {
res(“mydata:myBinaryResourceJar:1.0”) {
transitive = false
}
}
tasks.withType(JavaCompile) {
compileTask ->
compileTask.dependsOn installMavenDeps
compileTask.dependsOn processData
}
android.applicationVariants.all { variant ->
configurations.res.each {
jar ->
copy {
includeEmptyDirs false
from zipTree(jar)
into variant.javaCompile.destinationDir
include “**/*.class”
}
}
}
task processData {
def to = tempRes + “/raw”;
file(to).mkdirs()
configurations.res.each {
jar ->
copy {
includeEmptyDirs false
from { zipTree(jar) }
into to
include “/*.jet"
eachFile { FileCopyDetails fcp ->
// remap the file to the root
String[] segments = [fcp.getName()]
println "processFile: flatten ${fcp.relativePath}"
fcp.relativePath = new RelativePath(true, segments)
}
}
copy {
includeEmptyDirs false
from { zipTree(jar) }
into tempPath
include "assets/”, “res/**”
}
}
}
child2,child3,child4(I think there will be around 6 similar apps) are mostly copy of child1. Dependencies and android settings are different. Maybe other differences.
There are several problems:
- I have a resource jars. Some of they contains custom binary format files and compiled accessors to they ( .class files ).
We need add .class to classpath and copy binary data to res/raw.
- Some of they just contains assets: icons, fonts, etc. But packaging is jar. Can I don’t use handwritten copy task?
- android.applicationVariants.all {} and processData {configurations.res.each…} invoke before installMavenDeps. So if I don’t have dependency in local maven, then build just failed. It’s BIG problem.
- If I want build several modules, then Maven build will invoke for every project. Even all maven modules are up-to-date it cost 3 to 10 seconds every time. It’s significant slowdown.
- There are lot of copy-paste. Can I write installMavenDeps, processData tasks once?