1 /*
2  * Copyright 2023 The Android Open Source Project
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 
17 package androidx.baselineprofile.gradle.apptarget.task
18 
19 import androidx.baselineprofile.gradle.utils.INTERMEDIATES_BASE_FOLDER
20 import androidx.baselineprofile.gradle.utils.maybeRegister
21 import com.android.build.gradle.internal.tasks.BuildAnalyzer
22 import com.android.buildanalyzer.common.TaskCategory
23 import org.gradle.api.DefaultTask
24 import org.gradle.api.Project
25 import org.gradle.api.file.RegularFileProperty
26 import org.gradle.api.tasks.OutputFile
27 import org.gradle.api.tasks.TaskAction
28 import org.gradle.api.tasks.TaskProvider
29 import org.gradle.work.DisableCachingByDefault
30 
31 /** This task generates a fixed keep rule file that disables obfuscation. */
32 @DisableCachingByDefault(because = "Not worth caching.")
33 @BuildAnalyzer(primaryTaskCategory = TaskCategory.OPTIMIZATION)
34 abstract class GenerateKeepRulesForBaselineProfilesTask : DefaultTask() {
35 
36     companion object {
37         private val KEEP_RULES =
38             """
39             # Autogenerated for baseline profiles. Changes to this file will be overwritten.
40             -dontobfuscate
41 
42         """
43                 .trimIndent()
44 
maybeRegisternull45         internal fun maybeRegister(
46             project: Project
47         ): TaskProvider<GenerateKeepRulesForBaselineProfilesTask> =
48             project.tasks.maybeRegister("generateKeepRulesForBaselineProfile") {
49                 it.keepRuleFile.set(
50                     project.layout.buildDirectory.file(
51                         "$INTERMEDIATES_BASE_FOLDER/tmp/dontobfuscate.pro"
52                     )
53                 )
54             }
55     }
56 
57     @get:OutputFile abstract val keepRuleFile: RegularFileProperty
58 
59     @TaskAction
execnull60     fun exec() {
61         keepRuleFile.get().asFile.apply {
62             writeText(KEEP_RULES)
63             logger.info(
64                 "Generated keep rule file for baseline profiles build type in $absolutePath"
65             )
66         }
67     }
68 }
69