Skip to content

Commit

Permalink
Generate the classpath much faster
Browse files Browse the repository at this point in the history
  • Loading branch information
DonnieWest committed Jul 28, 2018
1 parent 3ccfbfb commit 35dda26
Showing 1 changed file with 42 additions and 92 deletions.
134 changes: 42 additions & 92 deletions classpath.gradle
@@ -1,109 +1,59 @@
boolean isResolvable(Configuration conf) {
// isCanBeResolved was added in Gradle 3.3. Previously, all configurations were resolvable
if (Configuration.class.declaredMethods.any { it.name == 'isCanBeResolved' }) {
return conf.canBeResolved
}
return true
}


File getBuildCacheForDependency(File dependency) {
String name = dependency.getName()
String home = System.getProperty("user.home")
String gradleCache = home + File.separator + '.gradle' + File.separator + 'caches' + File.separator
if (file(gradleCache).exists()) {
String include = 'transforms*' + File.separator + '**' + File.separator + name + File.separator + '**' + File.separator + 'classes.jar'
return fileTree(dir: gradleCache, include: include).files.find { it.isFile() }
} else {
return zipTree(dependency).files.find { it.isFile() && it.name.endsWith('jar') }
}
}

task classpath {
doLast {
HashSet<String> classpathFiles = new HashSet<String>()
HashSet<String> paths = new HashSet<String>()
for (proj in allprojects) {
for (conf in proj.configurations) {
if (isResolvable(conf)) {
for (dependency in conf) {
classpathFiles += dependency
}
for (project in allprojects) {
if (project.hasProperty('android')) {
project.android.getBootClasspath().each {
classpathFiles += it
}
}
if (project.android.hasProperty('applicationVariants')) {
project.android.applicationVariants.all { variant ->

def rjava = proj.getBuildDir().absolutePath + File.separator + "intermediates" + File.separator + "classes" + File.separator + "debug"
def rFiles = new File(rjava)
if (rFiles.exists()) {
classpathFiles += rFiles
}
def variantBase = variant.baseName.replaceAll("-", File.separator)

if (proj.hasProperty("android")) {
classpathFiles += proj.android.getBootClasspath()
if (proj.android.hasProperty("applicationVariants")) {
proj.android.applicationVariants.all { v ->
if (v.hasProperty("javaCompile")) {
classpathFiles += v.javaCompile.classpath
}
if (v.hasProperty("compileConfiguration")) {
v.compileConfiguration.each { dependency ->
classpathFiles += dependency
}
}
if (v.hasProperty("runtimeConfiguration")) {
v.runtimeConfiguration.each { dependency ->
classpathFiles += dependency
}
}
if (v.hasProperty("getApkLibraries")) {
println v.getApkLibraries()
classpathFiles += v.getApkLibraries()
}
if (v.hasProperty("getCompileLibraries")) {
classpathFiles += v.getCompileLibraries()
}
for (srcSet in v.getSourceSets()) {
for (dir in srcSet.java.srcDirs) {
paths += dir.absolutePath
}
}
}
}
def buildClasses = project.getBuildDir().absolutePath +
File.separator + "intermediates" +
File.separator + "classes" +
File.separator + variantBase

classpathFiles += buildClasses

def userClasses = project.getBuildDir().absolutePath +
File.separator + "intermediates" +
File.separator + "javac" +
File.separator + variant.baseName.replaceAll("-", File.separator) +
File.separator + "compile" + variantBase.capitalize() + "JavaWithJavac" + File.separator + "classes"

if (proj.android.hasProperty("libraryVariants")) {
proj.android.libraryVariants.all { v ->
classpathFiles += v.javaCompile.classpath.files
for (srcSet in v.getSourceSets()) {
for (dir in srcSet.java.srcDirs) {
paths += dir.absolutePath
}
classpathFiles += userClasses

variant.getCompileClasspath().each {
classpathFiles += it
}
}
}
}

if (proj.hasProperty("sourceSets")) {
for (srcSet in proj.sourceSets) {
for (dir in srcSet.java.srcDirs) {
paths += dir.absolutePath
} else {
// Print the list of all dependencies jar files.
project.configurations.findAll {
it.metaClass.respondsTo(it, "isCanBeResolved") ? it.isCanBeResolved() : false
}.each {
it.resolve().each {
if (it.inspect().endsWith("jar")) {
classpathFiles += it
} else if (it.inspect().endsWith("aar")) {
// If the dependency is an AAR file we try to determine the location
// of the classes.jar file in the exploded aar folder.
def splitted = it.inspect().split("/")
def namespace = splitted[-5]
def name = splitted[-4]
def version = splitted[-3]
def explodedPath = "$project.buildDir/intermediates/exploded-aar/$namespace/$name/$version/jars/classes.jar"
classpathFiles += explodedPath
}
}
}
}
}

HashSet<String> computedPaths = new HashSet<String>()
for (dependency in classpathFiles) {
if (dependency.name.endsWith("jar")) {
computedPaths += dependency
} else {
computedPaths += getBuildCacheForDependency(dependency)
}
}


computedPaths += paths

def classpath = computedPaths.join(File.pathSeparator)
def classpath = classpathFiles.join(File.pathSeparator)
println "CLASSPATH:" + classpath
println "END CLASSPATH GENERATION"
}
Expand Down

0 comments on commit 35dda26

Please sign in to comment.