1 // Copyright (c) 2017, the R8 project authors. Please see the AUTHORS file 2 // for details. All rights reserved. Use of this source code is governed by a 3 // BSD-style license that can be found in the LICENSE file. 4 package com.android.tools.r8.shaking; 5 6 import com.android.tools.r8.graph.DexItemFactory; 7 import com.android.tools.r8.naming.DictionaryReader; 8 import com.google.common.collect.ImmutableList; 9 import com.google.common.collect.ImmutableSet; 10 import java.nio.file.Path; 11 import java.util.ArrayList; 12 import java.util.HashSet; 13 import java.util.List; 14 import java.util.Set; 15 16 public class ProguardConfiguration { 17 18 public static class Builder { 19 20 private final List<Path> injars = new ArrayList<>(); 21 private final List<Path> libraryjars = new ArrayList<>(); 22 private String packagePrefix = null; 23 private boolean allowAccessModification = false; 24 private boolean ignoreWarnings = false; 25 private boolean obfuscating = true; 26 private boolean shrinking = true; 27 private boolean printUsage = false; 28 private Path printUsageFile; 29 private boolean printMapping; 30 private Path printMappingOutput; 31 private boolean verbose = false; 32 private final List<String> attributesRemovalPatterns = new ArrayList<>(); 33 private final Set<ProguardTypeMatcher> dontWarnPatterns = new HashSet<>(); 34 protected final List<ProguardConfigurationRule> rules = new ArrayList<>(); 35 private final DexItemFactory dexItemFactory; 36 private boolean printSeeds; 37 private Path seedFile; 38 private Path obfuscationDictionary; 39 private Path classObfuscationDictionary; 40 private Path packageObfuscationDictionary; 41 Builder(DexItemFactory dexItemFactory)42 private Builder(DexItemFactory dexItemFactory) { 43 this.dexItemFactory = dexItemFactory; 44 } 45 addInjars(List<Path> injars)46 public void addInjars(List<Path> injars) { 47 this.injars.addAll(injars); 48 } 49 addLibraryJars(List<Path> libraryJars)50 public void addLibraryJars(List<Path> libraryJars) { 51 this.libraryjars.addAll(libraryJars); 52 } 53 setPackagePrefix(String packagePrefix)54 public void setPackagePrefix(String packagePrefix) { 55 this.packagePrefix = packagePrefix; 56 } 57 setAllowAccessModification(boolean allowAccessModification)58 public void setAllowAccessModification(boolean allowAccessModification) { 59 this.allowAccessModification = allowAccessModification; 60 } 61 setIgnoreWarnings(boolean ignoreWarnings)62 public void setIgnoreWarnings(boolean ignoreWarnings) { 63 this.ignoreWarnings = ignoreWarnings; 64 } 65 setObfuscating(boolean obfuscate)66 public void setObfuscating(boolean obfuscate) { 67 this.obfuscating = obfuscate; 68 } 69 setShrinking(boolean shrinking)70 public void setShrinking(boolean shrinking) { 71 this.shrinking = shrinking; 72 } 73 setPrintUsage(boolean printUsage)74 public void setPrintUsage(boolean printUsage) { 75 this.printUsage = printUsage; 76 } 77 setPrintUsageFile(Path printUsageFile)78 public void setPrintUsageFile(Path printUsageFile) { 79 this.printUsageFile = printUsageFile; 80 } 81 setPrintMapping(boolean printMapping)82 public void setPrintMapping(boolean printMapping) { 83 this.printMapping = printMapping; 84 } 85 setPrintMappingOutput(Path file)86 public void setPrintMappingOutput(Path file) { 87 this.printMappingOutput = file; 88 } 89 setVerbose(boolean verbose)90 public void setVerbose(boolean verbose) { 91 this.verbose = verbose; 92 } 93 addAttributeRemovalPattern(String attributesRemovalPattern)94 public void addAttributeRemovalPattern(String attributesRemovalPattern) { 95 this.attributesRemovalPatterns.add(attributesRemovalPattern); 96 } 97 addRule(ProguardConfigurationRule rule)98 public void addRule(ProguardConfigurationRule rule) { 99 this.rules.add(rule); 100 } 101 addDontWarnPattern(ProguardTypeMatcher pattern)102 public void addDontWarnPattern(ProguardTypeMatcher pattern) { 103 dontWarnPatterns.add(pattern); 104 } 105 setSeedFile(Path seedFile)106 public void setSeedFile(Path seedFile) { 107 this.seedFile = seedFile; 108 } 109 setPrintSeed(boolean printSeeds)110 public void setPrintSeed(boolean printSeeds) { 111 this.printSeeds = printSeeds; 112 } 113 setObfuscationDictionary(Path obfuscationDictionary)114 public void setObfuscationDictionary(Path obfuscationDictionary) { 115 this.obfuscationDictionary = obfuscationDictionary; 116 } 117 setClassObfuscationDictionary(Path classObfuscationDictionary)118 public void setClassObfuscationDictionary(Path classObfuscationDictionary) { 119 this.classObfuscationDictionary = classObfuscationDictionary; 120 } 121 setPackageObfuscationDictionary(Path packageObfuscationDictionary)122 public void setPackageObfuscationDictionary(Path packageObfuscationDictionary) { 123 this.packageObfuscationDictionary = packageObfuscationDictionary; 124 } 125 build()126 public ProguardConfiguration build() { 127 return new ProguardConfiguration( 128 dexItemFactory, 129 injars, 130 libraryjars, 131 packagePrefix, 132 allowAccessModification, 133 ignoreWarnings, 134 obfuscating, 135 shrinking, 136 printUsage, 137 printUsageFile, 138 printMapping, 139 printMappingOutput, 140 verbose, 141 attributesRemovalPatterns, 142 dontWarnPatterns, 143 rules, 144 printSeeds, 145 seedFile, 146 DictionaryReader.readAllNames(obfuscationDictionary), 147 DictionaryReader.readAllNames(classObfuscationDictionary), 148 DictionaryReader.readAllNames(packageObfuscationDictionary)); 149 } 150 } 151 152 private final DexItemFactory dexItemFactory; 153 private final List<Path> injars; 154 private final List<Path> libraryjars; 155 private final String packagePrefix; 156 private final boolean allowAccessModification; 157 private final boolean ignoreWarnings; 158 private final boolean obfuscating; 159 private final boolean shrinking; 160 private final boolean printUsage; 161 private final Path printUsageFile; 162 private final boolean printMapping; 163 private final Path printMappingOutput; 164 private final boolean verbose; 165 private final List<String> attributesRemovalPatterns; 166 private final ImmutableSet<ProguardTypeMatcher> dontWarnPatterns; 167 protected final ImmutableList<ProguardConfigurationRule> rules; 168 private final boolean printSeeds; 169 private final Path seedFile; 170 private final List<String> obfuscationDictionary; 171 private final List<String> classObfuscationDictionary; 172 private final List<String> packageObfuscationDictionary; 173 ProguardConfiguration( DexItemFactory factory, List<Path> injars, List<Path> libraryjars, String packagePrefix, boolean allowAccessModification, boolean ignoreWarnings, boolean obfuscating, boolean shrinking, boolean printUsage, Path printUsageFile, boolean printMapping, Path printMappingOutput, boolean verbose, List<String> attributesRemovalPatterns, Set<ProguardTypeMatcher> dontWarnPatterns, List<ProguardConfigurationRule> rules, boolean printSeeds, Path seedFile, List<String> obfuscationDictionary, List<String> classObfuscationDictionary, List<String> packageObfuscationDictionary)174 private ProguardConfiguration( 175 DexItemFactory factory, 176 List<Path> injars, 177 List<Path> libraryjars, 178 String packagePrefix, 179 boolean allowAccessModification, 180 boolean ignoreWarnings, 181 boolean obfuscating, 182 boolean shrinking, 183 boolean printUsage, 184 Path printUsageFile, 185 boolean printMapping, 186 Path printMappingOutput, 187 boolean verbose, 188 List<String> attributesRemovalPatterns, 189 Set<ProguardTypeMatcher> dontWarnPatterns, 190 List<ProguardConfigurationRule> rules, 191 boolean printSeeds, 192 Path seedFile, 193 List<String> obfuscationDictionary, 194 List<String> classObfuscationDictionary, 195 List<String> packageObfuscationDictionary) { 196 this.dexItemFactory = factory; 197 this.injars = ImmutableList.copyOf(injars); 198 this.libraryjars = ImmutableList.copyOf(libraryjars); 199 this.packagePrefix = packagePrefix; 200 this.allowAccessModification = allowAccessModification; 201 this.ignoreWarnings = ignoreWarnings; 202 this.obfuscating = obfuscating; 203 this.shrinking = shrinking; 204 this.printUsage = printUsage; 205 this.printUsageFile = printUsageFile; 206 this.printMapping = printMapping; 207 this.printMappingOutput = printMappingOutput; 208 this.verbose = verbose; 209 this.attributesRemovalPatterns = ImmutableList.copyOf(attributesRemovalPatterns); 210 this.dontWarnPatterns = ImmutableSet.copyOf(dontWarnPatterns); 211 this.rules = ImmutableList.copyOf(rules); 212 this.printSeeds = printSeeds; 213 this.seedFile = seedFile; 214 this.obfuscationDictionary = obfuscationDictionary; 215 this.classObfuscationDictionary = classObfuscationDictionary; 216 this.packageObfuscationDictionary = packageObfuscationDictionary; 217 } 218 219 /** 220 * Create a new empty builder. 221 */ builder(DexItemFactory dexItemFactory)222 public static Builder builder(DexItemFactory dexItemFactory) { 223 return new Builder(dexItemFactory); 224 } 225 getDexItemFactory()226 public DexItemFactory getDexItemFactory() { 227 return dexItemFactory; 228 } 229 isDefaultConfiguration()230 public boolean isDefaultConfiguration() { 231 return false; 232 } 233 getInjars()234 public List<Path> getInjars() { 235 return injars; 236 } 237 getLibraryjars()238 public List<Path> getLibraryjars() { 239 return libraryjars; 240 } 241 getPackagePrefix()242 public String getPackagePrefix() { 243 return packagePrefix; 244 } 245 getAllowAccessModification()246 public boolean getAllowAccessModification() { 247 return allowAccessModification; 248 } 249 isPrintingMapping()250 public boolean isPrintingMapping() { 251 return printMapping; 252 } 253 getPrintMappingOutput()254 public Path getPrintMappingOutput() { 255 return printMappingOutput; 256 } 257 isIgnoreWarnings()258 public boolean isIgnoreWarnings() { 259 return ignoreWarnings; 260 } 261 isObfuscating()262 public boolean isObfuscating() { 263 return obfuscating; 264 } 265 isShrinking()266 public boolean isShrinking() { 267 return shrinking; 268 } 269 isPrintUsage()270 public boolean isPrintUsage() { 271 return printUsage; 272 } 273 getPrintUsageFile()274 public Path getPrintUsageFile() { 275 return printUsageFile; 276 } 277 isVerbose()278 public boolean isVerbose() { 279 return verbose; 280 } 281 getAttributesRemovalPatterns()282 public List<String> getAttributesRemovalPatterns() { 283 return attributesRemovalPatterns; 284 } 285 getDontWarnPatterns()286 public ImmutableSet<ProguardTypeMatcher> getDontWarnPatterns() { 287 return dontWarnPatterns; 288 } 289 getRules()290 public ImmutableList<ProguardConfigurationRule> getRules() { 291 return rules; 292 } 293 getObfuscationDictionary()294 public List<String> getObfuscationDictionary() { 295 return obfuscationDictionary; 296 } 297 getPackageObfuscationDictionary()298 public List<String> getPackageObfuscationDictionary() { 299 return packageObfuscationDictionary; 300 } 301 getClassObfuscationDictionary()302 public List<String> getClassObfuscationDictionary() { 303 return classObfuscationDictionary; 304 } 305 defaultConfiguration(DexItemFactory dexItemFactory)306 public static ProguardConfiguration defaultConfiguration(DexItemFactory dexItemFactory) { 307 ProguardConfiguration config = new DefaultProguardConfiguration(dexItemFactory); 308 return config; 309 } 310 311 public static class DefaultProguardConfiguration extends ProguardConfiguration { 312 DefaultProguardConfiguration(DexItemFactory factory)313 public DefaultProguardConfiguration(DexItemFactory factory) { 314 super(factory, 315 ImmutableList.of() /* injars */, 316 ImmutableList.of() /* libraryjars */, 317 "" /* package prefix */, 318 false /* allowAccessModification */, 319 false /* ignoreWarnings */, 320 false /* obfuscating */, 321 false /* shrinking */, 322 false /* printUsage */, 323 null /* printUsageFile */, 324 false /* printMapping */, 325 null /* outputMapping */, 326 false /* verbose */, 327 ImmutableList.of() /* attributesRemovalPatterns */, 328 ImmutableSet.of() /* dontWarnPatterns */, 329 ImmutableList.of(ProguardKeepRule.defaultKeepAllRule()), 330 false /* printSeeds */, 331 null /* seedFile */, 332 ImmutableList.of() /* obfuscationDictionary */, 333 ImmutableList.of() /* classObfuscationDictionary */, 334 ImmutableList.of() /* packageObfucationDictionary */); 335 } 336 337 @Override isDefaultConfiguration()338 public boolean isDefaultConfiguration() { 339 return true; 340 } 341 } 342 getPrintSeeds()343 public boolean getPrintSeeds() { 344 return printSeeds; 345 } 346 getSeedFile()347 public Path getSeedFile() { 348 return seedFile; 349 } 350 } 351