| /* |
| * Copyright (C) 2015 The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| android.libraryVariants.all { variant -> |
| |
| if (!jarjarDeps) |
| throw new InvalidUserDataException('Dependency set is empty, please define a set of ' + |
| 'dependencies: ext.jarjarDeps = ["depA.jar", "depB.jar"] as Set') |
| |
| if (!jarjarRulesFile) |
| logger.warning 'No rules setup for jajar dependencies. Rules can be defined like this:' + |
| 'ext.jarjarRulesFile = file(\'jarjar-rules.txt\')' |
| |
| // Get the original classes.jar output folder to place the final jarjar'd file in for the |
| // aar generation to pick up. |
| Jar classesJarTask = (Jar) project.tasks.getByName("package${variant.name.capitalize()}Jar") |
| def finalOutputJarDir = classesJarTask.destinationDir |
| variant.ext.pathToFinalOutputJarDir = finalOutputJarDir |
| |
| // Place the original classes.jar in a different folder to run jarjar on it. |
| def tmpClassesJarDir = "$project.buildDir/pre-jarjar/$variant.dirName" |
| classesJarTask.destinationDir = project.file("$tmpClassesJarDir") |
| |
| def classesJar = "$finalOutputJarDir/classes.jar" |
| def tmpClassesJar = "$tmpClassesJarDir/classes.jar" |
| |
| def jarJarTaskName = "jarJar${variant.name.capitalize()}" |
| |
| task "$jarJarTaskName" << { |
| project.ant { |
| taskdef name: "jarjar", classname: "com.tonicsystems.jarjar.JarJarTask", |
| classpath: configurations.jarjar.asPath |
| jarjar(jarfile: "$classesJar", filesetmanifest: "merge") { |
| jarjarDeps.each { |
| zipfileset(src: "$it") |
| } |
| zipfileset(src: "$tmpClassesJar") |
| jarjarRulesFile.splitEachLine('\\s+') { |
| if (it[0] == 'rule') { |
| rule pattern: it[1], result: it[2] |
| } |
| } |
| // Set the destination dir of the packageJar task to it's original location |
| classesJarTask.destinationDir = finalOutputJarDir |
| } |
| } |
| } |
| |
| // Fix the dependency chain so the output jars will be available at the right time. |
| project.tasks.getByName("$jarJarTaskName").dependsOn classesJarTask, configurations.provided |
| variant.outputs.get(0).packageLibrary.dependsOn "$jarJarTaskName" |
| } |