1 /* <lambda>null2 * Copyright (C) 2015 Square, Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.squareup.leakcanary.deobfuscation 17 18 import java.io.File 19 import org.gradle.api.DefaultTask 20 import org.gradle.api.GradleException 21 import org.gradle.api.provider.Property 22 import org.gradle.api.tasks.CacheableTask 23 import org.gradle.api.tasks.Input 24 import org.gradle.api.tasks.InputDirectory 25 import org.gradle.api.tasks.InputFile 26 import org.gradle.api.tasks.OutputFile 27 import org.gradle.api.tasks.PathSensitive 28 import org.gradle.api.tasks.PathSensitivity 29 import org.gradle.api.tasks.TaskAction 30 31 @CacheableTask 32 abstract class CopyObfuscationMappingFileTask : DefaultTask() { 33 34 @Input 35 var variantName: String = "" 36 37 @get:InputFile 38 @get:PathSensitive(PathSensitivity.RELATIVE) 39 abstract val mappingFile: Property<File?> 40 41 @get:InputDirectory 42 @get:PathSensitive(PathSensitivity.RELATIVE) 43 abstract val mergeAssetsDirectory: Property<File> 44 45 @get:OutputFile 46 val leakCanaryAssetsOutputFile: File 47 get() = File(mergeAssetsDirectory.orNull, "leakCanaryObfuscationMapping.txt") 48 49 init { 50 description = "Puts obfuscation mapping file in assets directory." 51 } 52 53 @TaskAction 54 fun copyObfuscationMappingFile() { 55 val mapping = validateMappingFile() 56 validateMergeAssetsDir() 57 mapping.copyTo(leakCanaryAssetsOutputFile, overwrite = true) 58 } 59 60 private fun validateMappingFile(): File { 61 val mapping = mappingFile.orNull 62 if (mapping == null || !mapping.exists()) { 63 throw GradleException( 64 """ 65 The plugin was configured to be applied to a variant which doesn't define 66 an obfuscation mapping file: make sure that isMinified is true for variant: $variantName. 67 """ 68 ) 69 } 70 return mapping 71 } 72 73 private fun validateMergeAssetsDir() { 74 mergeAssetsDirectory.orNull?.let { mergeAssetsDir -> 75 if (!mergeAssetsDir.exists()) { 76 val mergeAssetsDirCreated = mergeAssetsDir.mkdirs() 77 if (!mergeAssetsDirCreated) { 78 throw GradleException( 79 "Obfuscation mapping destination dir doesn't exist and it's impossible to create it." 80 ) 81 } 82 } 83 } ?: throw GradleException("Obfuscation mapping is null.") 84 } 85 } 86