Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Gradle script that generates a task to copy all build variant's dependencies to a certain directory for use with Nexus IQ Server. It copies exploded AARs too, renaming the classes.jar file into "<aar_dependency_name>.jar".
apply plugin: 'com.android.application'
android.applicationVariants.all { variant ->
task "copyDependencies${variant.name.capitalize()}"() {
outputs.upToDateWhen { false }
doLast {
println "Executing copyDependencies${variant.name.capitalize()}"
variant.getCompileClasspath().each { fileDependency ->
def sourcePath = fileDependency.absolutePath
def destinationPath = project.projectDir.path + "/build/dependencies/${variant.name}/"
println "Copying dependency:"
println sourcePath
//The monstrous regex that gets the name of the lib from it’s exploded .aar path
def dependencyName
if (sourcePath.contains("classes.jar")) {
def dependencyNameRegexResult = (sourcePath =~ /.*\/(.*)\.aar\/.*\/jars\/classes\.jar/)
if (dependencyNameRegexResult.size() > 0) {
dependencyName = dependencyNameRegexResult[0][1]
println "Exploded AAR found : ${dependencyName}"
}
}
copy {
from sourcePath
into destinationPath
rename {String filename ->
if (filename.contains("classes.jar") && dependencyName != null) {
dependencyName = "${dependencyName}.jar"
println "Renaming dependency file to : ${dependencyName}"
return dependencyName
}
return filename
}
}
}
}
}
}
@JEuler

This comment has been minimized.

Copy link

commented Mar 12, 2019

Hi! I've tried your task and it is working fine. But what about native dependencies (.so)? Is it easy to extend this script to support this?

@James-Aidoo

This comment has been minimized.

Copy link

commented Mar 29, 2019

Hello
How do I run this script please?

@n-belokopytov

This comment has been minimized.

Copy link
Owner Author

commented May 20, 2019

Hi! I've tried your task and it is working fine. But what about native dependencies (.so)? Is it easy to extend this script to support this?

Unfortunately I don't know how to do that - never had to =\ Are native dependencies supplied through Gradle as well?

How do I run this script please?

Place the file in your project, then import it to your gradle with
apply from: "<PATH_TO_FILE>/copyDeps.gradle"
Sync your gradle files and you will see that there has been a new task generated, that starts with "copyDependencies". Then you run
.\gradlew copyDependencies<BUILD_VARIANT>
where BUILD_VARIANT is any of the variants that you have in your build.

@0xABD

This comment has been minimized.

Copy link

commented May 22, 2019

I modified the regex to fit backward slashes for Windows paths:
https://gist.github.com/0xABD/0afc246048f6ad0f9d63e86ba61a5933
(The original version on Windows will not export AARs at all.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.