Skip to content

Instantly share code, notes, and snippets.

@n-belokopytov
Last active December 6, 2023 08:35
Show Gist options
  • Star 23 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save n-belokopytov/d44949590748b096c1a497008b761d04 to your computer and use it in GitHub Desktop.
Save n-belokopytov/d44949590748b096c1a497008b761d04 to your computer and use it in GitHub Desktop.
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
Copy link

JEuler 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
Copy link

Hello
How do I run this script please?

@n-belokopytov
Copy link
Author

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.

Copy link

ghost 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.)

@k4Shinigami
Copy link

Thank you for the script!

I had to change the sourcePath regex to remove .aar as it is not in the compileClassPath.

more like:

def dependencyNameRegexResult =  (sourcePath =~ /.*\/(.*)\.*\/jars\/classes\.jar/)

since the compile path looked like : <home>/.gradle/caches/transforms-2/files-2.1/475cffeb9ca1ed6d315289eef09deff5/animated-vector-drawable-28.0.0/jars/classes.jar

@n-belokopytov
Copy link
Author

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.)

Thank you for the script!

I had to change the sourcePath regex to remove .aar as it is not in the compileClassPath.

more like:

def dependencyNameRegexResult =  (sourcePath =~ /.*\/(.*)\.*\/jars\/classes\.jar/)

since the compile path looked like : <home>/.gradle/caches/transforms-2/files-2.1/475cffeb9ca1ed6d315289eef09deff5/animated-vector-drawable-28.0.0/jars/classes.jar

Thank you folks for the input! Appreciate it.

@joelbarbosa
Copy link

Why its looking in /.gradle/caches/transforms-2/files-2.1/0cc3c67e492c52aefc1403e3379aad59/jars/classes.jar
I think it should be from modules-2.
I always get: 0cc3c67e492c52aefc1403e3379aad59/jars/classes.jar -> 0cc3c67e492c52aefc1403e3379aad59.jar

@n-belokopytov
Copy link
Author

Why its looking in /.gradle/caches/transforms-2/files-2.1/0cc3c67e492c52aefc1403e3379aad59/jars/classes.jar
I think it should be from modules-2.
I always get: 0cc3c67e492c52aefc1403e3379aad59/jars/classes.jar -> 0cc3c67e492c52aefc1403e3379aad59.jar

My bet is it's because it uses getCompileClasspath() and this path is contained there. I don't know how do you get to modules-2, but what you describe seems like the correct work of this snippet - grab the folder name and replace classes with it in case of an exploded .aar. Perhaps you could modify it somehow to work in your case.

@NormanOu
Copy link

NormanOu commented May 6, 2020

may i ask, how can i get the proguard.txt file in each depended aar file. i find out that the files copied is all *.jar but not *.aar

@wujunfeng1991
Copy link

I use this code to copy dependencies. Execute gradlew copyDepsRelease or gradlew copyDepsDebug.

android.applicationVariants.all { variant ->
task "copyDeps${variant.name.capitalize()}"() {
outputs.upToDateWhen { false }
doLast {
mkdir 'depsLibs'
variant.getCompileConfiguration().getResolvedConfiguration().getResolvedArtifacts().each{ artifact ->
def id = artifact.moduleVersion.id
def targetName = "${id.group}${id.name}${id.version}.${artifact.type}"
copy{
from artifact.file
into 'depsLibs'
rename('(.*)',targetName)
}
}
}
}
}

@kibotu
Copy link

kibotu commented Dec 31, 2020

if you just want to list all dependencies with their versions for a configuration

https://gist.github.com/kibotu/a4b248e5ca8b702ef5884c3ab9693f8f

@git4pl
Copy link

git4pl commented Sep 14, 2021

thanks for your script. but i wonder how to get the dependencies in my custom AS plugin. Is there any gradle APIs for Idea Plugin developing? i have googled a lot but got nothing i want. Hope you can help me.

@n-belokopytov
Copy link
Author

@git4pl sorry, can't help you bud. Maybe you could look for a Slack community of JetBrains or Gradle - there should be people who can help you. I also couldn't find any documentation on the implemented methods of AS Gradle Plugin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment