Answer the question
In order to leave comments, you need to log in
How to build jar with dependencies from android-library module?
There is a gradle module, which in turn depends on:
another gradle module,
aar file (google-play-services)
jar file (gson).
I need to assemble a jar from all this with all the dependencies, that is, the final jar should have class files:
my modules,
google-play services,
gson.
This is done in order to be able to connect the android library as a jar file (without the obligatory use of gradle or maven, which can connect aar)
I don’t know how to do this.
here is the gradle.build
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
classpath 'com.github.dcendents:android-maven-plugin:1.2'
}
}
apply plugin: 'com.android.library'
apply plugin: 'android-maven'
group = "com.foo"
version = "1.0"
android {
compileSdkVersion 21
buildToolsVersion '21.1.2'
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['res']
}
}
defaultConfig {
minSdkVersion 5
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
}
repositories {
jcenter()
mavenLocal()
mavenCentral()
}
dependencies {
compile project (':amlogger')
compile 'com.google.android.gms:play-services:6.5.87'
compile 'com.google.code.gson:gson:2.3'
}
Answer the question
In order to leave comments, you need to log in
Well, actually, I'll answer myself
android.libraryVariants.all { variant ->
tasks.create(name: "fatJar${variant.name.capitalize()}", type: Jar, dependsOn: assembleRelease) {
doFirst {
baseName = variant.name
destinationDir = file('output')
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
from zipTree(tasks.getByName("package${variant.name.capitalize()}Jar").outputs.files.singleFile)
def closure = { file ->
if (file.name.endsWith('.aar')) {
println file.name
List<File> listJarFiles = zipTree(file).findAll { fileInAar ->
fileInAar.name.endsWith('.jar')
}
def listClassesFilesFromJars = listJarFiles.collect {
zipTree(it)
}
listClassesFilesFromJars
} else if (file.name.endsWith('.jar')) {
return zipTree(file)
}
}
from configurations.compile.files.collect(closure)
from configurations.getByName("${variant.name}Compile").files.collect(closure)
}
}
}
android.libraryVariants.all { variant ->
tasks.create(name: "collectJars${variant.name.capitalize()}", type: Copy, dependsOn: assembleRelease) {
delete file("output/outputJars${variant.name.capitalize()}")
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
from tasks.getByName("package${variant.name.capitalize()}Jar").outputs.files.singleFile, {
rename 'classes.jar', variant.name + "-" + project.version + ".jar"
}
def closure = { file ->
if (file.name.endsWith('.aar')) {
println file.name
zipTree(file).findAll { fileInAar ->
fileInAar.name.endsWith('.jar')
}.each { f ->
if(f.name == "classes.jar") {
from f, {
println f.name
rename "classes", file.name.substring(0, file.name.lastIndexOf("."))
}
} else {
from f
}
}
} else if (file.name.endsWith('.jar')) {
from file
}
}
configurations.compile.files.collect(closure)
configurations.getByName("${variant.name}Compile").files.collect(closure)
into 'output/outputJars' + variant.name.capitalize()
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question