@@ -20,8 +20,7 @@ import groovy.json.JsonSlurper
2020import java.nio.file.Files
2121import java.nio.file.Paths
2222import java.nio.file.StandardCopyOption
23- import java.util.regex.Matcher
24- import java.util.regex.Pattern
23+ import groovy.io.FileType
2524import java.security.MessageDigest
2625import javax.inject.Inject
2726apply plugin : " com.android.application"
@@ -484,7 +483,7 @@ def explodeAar(File compileDependency, File outputDir) {
484483 def targetFile = new File (outputDir, file. name)
485484 InputStream inputStream = jar. getInputStream(file)
486485 new File (targetFile. parent). mkdirs()
487- Files . copy(inputStream, targetFile. toPath(), StandardCopyOption . REPLACE_EXISTING );
486+ Files . copy(inputStream, targetFile. toPath(), StandardCopyOption . REPLACE_EXISTING )
488487 }
489488 if (file. isDirectory()) {
490489 continue
@@ -500,9 +499,9 @@ def explodeAar(File compileDependency, File outputDir) {
500499}
501500
502501def md5 (String string ) {
503- MessageDigest digest = MessageDigest . getInstance(" MD5" ) ;
504- digest. update(string. bytes);
505- return new BigInteger (1 , digest. digest()). toString(16 ). padLeft(32 , ' 0' );
502+ MessageDigest digest = MessageDigest . getInstance(" MD5" )
503+ digest. update(string. bytes)
504+ return new BigInteger (1 , digest. digest()). toString(16 ). padLeft(32 , ' 0' )
506505}
507506
508507class WorkerTask extends DefaultTask {
@@ -518,63 +517,81 @@ class EmptyRunnable implements Runnable {
518517}
519518
520519// Discover all jars and dynamically create tasks for the extraction of each of them
521- def allJars = []
520+ project . ext . allJars = []
522521afterEvaluate { project ->
523522 def buildType = project. selectedBuildType == " release" ? " Release" : " Debug"
524523 def jars = []
525- Pattern pattern = Pattern . compile(" ^(.+)${ buildType} CompileClasspath\$ " )
526524 def artifactType = Attribute . of(' artifactType' , String )
527525 configurations. all { config ->
528- Matcher matcher = pattern. matcher(config. name)
529- if (matcher. find() || config. name == " ${ buildType.toLowerCase()} CompileClasspath" ) {
526+ if (config. name == " ${ buildType.toLowerCase()} RuntimeClasspath" ) {
530527 config. incoming. artifactView {
531528 attributes {
532529 it. attribute(artifactType, ' jar' )
533530 }
534531 }. artifacts. each {
535- def jar = it. file;
536- if (! jars. contains(jar)) {
537- jars. add(jar)
538- def destDir = md5(jar. path);
539- def outputDir = new File (Paths . get(extractedDependenciesDir, destDir). normalize(). toString())
540-
541- def taskName = " extract_${ jar.name} _to_${ destDir} "
542- logger. debug(" Creating dynamic task ${ taskName} " )
543-
544- // Add discovered jars as dependencies of cleanupAllJars.
545- // This is cruicial for cloud builds because they are different
546- // on each incremental build (as each time the gradle user home
547- // directory is a randomly generated string)
548- cleanupAllJars. inputs. files jar
549-
550- task " ${ taskName} " (type : WorkerTask ) {
551- dependsOn cleanupAllJars
552- extractAllJars. dependsOn it
553-
554- // This dependency seems redundant but probably due to some Gradle issue with workers,
555- // without it `runSbg` sporadically starts before all extraction tasks have finished and
556- // fails due to missing JARs
557- runSbg. dependsOn it
558-
559- inputs. files jar
560- outputs. dir outputDir
561-
562- doLast {
563- // Runing in parallel no longer seems to bring any benefit.
564- // It mattered only when we were extracting JARs from AARs.
565- // To try it simply remove the following comments.
566- // workerExecutor.submit(EmptyRunnable.class) {
567- explodeAar(jar, outputDir)
568- // }
532+ processJar(it. file, jars)
533+ }
534+
535+ def projectDependencies = config. getAllDependencies(). withType(ProjectDependency )
536+ def dependentProjects = projectDependencies* . dependencyProject
537+ dependentProjects. findAll {
538+ // if there's a project dependency search for its result jar file in the build/intermediates/runtime_library_classes folder
539+ // this is the output folder in gradle 5.1.1, but it can be changed in the future versions of gradle
540+ def jarDir = new File (" ${ it.getBuildDir()} /intermediates/runtime_library_classes/${ buildType.toLowerCase()} " )
541+ if (jarDir. exists()) {
542+ jarDir. eachFileRecurse(FileType . FILES ) { file ->
543+ if (file. path. endsWith(" .jar" )) {
544+ processJar(file, jars)
569545 }
570546 }
571- allJars. add([file : jar, outputDir : outputDir])
547+ } else {
548+ println " WARNING: Folder ${ jarDir.path} does not exists, the dependent project's classess won't be included in the metadata"
572549 }
573550 }
574551 }
575552 }
576553}
577554
555+ def processJar (File jar , jars ) {
556+ if (! jars. contains(jar)) {
557+ jars. add(jar)
558+ def destDir = md5(jar. path)
559+ def outputDir = new File (Paths . get(extractedDependenciesDir, destDir). normalize(). toString())
560+
561+ def taskName = " extract_${ jar.name} _to_${ destDir} "
562+ logger. debug(" Creating dynamic task ${ taskName} " )
563+
564+ // Add discovered jars as dependencies of cleanupAllJars.
565+ // This is cruicial for cloud builds because they are different
566+ // on each incremental build (as each time the gradle user home
567+ // directory is a randomly generated string)
568+ cleanupAllJars. inputs. files jar
569+
570+ task " ${ taskName} " (type : WorkerTask ) {
571+ dependsOn cleanupAllJars
572+ extractAllJars. dependsOn it
573+
574+ // This dependency seems redundant but probably due to some Gradle issue with workers,
575+ // without it `runSbg` sporadically starts before all extraction tasks have finished and
576+ // fails due to missing JARs
577+ runSbg. dependsOn it
578+
579+ inputs. files jar
580+ outputs. dir outputDir
581+
582+ doLast {
583+ // Runing in parallel no longer seems to bring any benefit.
584+ // It mattered only when we were extracting JARs from AARs.
585+ // To try it simply remove the following comments.
586+ // workerExecutor.submit(EmptyRunnable.class) {
587+ explodeAar(jar, outputDir)
588+ // }
589+ }
590+ }
591+ project. ext. allJars. add([file : jar, outputDir : outputDir])
592+ }
593+ }
594+
578595task cleanupAllJars {
579596 // We depend on the list of libs directories that might contain aar or jar files
580597 // and on the list of all discovered jars
@@ -583,7 +600,7 @@ task cleanupAllJars {
583600 outputs. files cleanupAllJarsTimestamp
584601
585602 doLast {
586- def allDests = allJars* . outputDir* . name;
603+ def allDests = project . ext . allJars* . outputDir* . name
587604 def dir = new File (extractedDependenciesDir)
588605 if (dir. exists()) {
589606 dir. eachDir {
0 commit comments