Created
December 2, 2019 08:22
-
-
Save Mojtaba-Shafaei/6549d0ac86065dd059ff6a482ea6c192 to your computer and use it in GitHub Desktop.
Android Gradle task to copy all used dependencies into a destination folder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apply plugin: 'com.android.application' | |
// this task extracts Android project’s dependencies as offline jar files. | |
// add these files to server's lib folder | |
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 | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment