aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/android/tools/r8/shaking/ProguardConfiguration.java
blob: 9a630080034f6ed8dfa6e3b97bb054648f28d0c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// Copyright (c) 2017, the R8 project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
package com.android.tools.r8.shaking;

import com.android.tools.r8.graph.DexItemFactory;
import com.android.tools.r8.naming.DictionaryReader;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class ProguardConfiguration {

  public static class Builder {

    private final List<Path> injars = new ArrayList<>();
    private final List<Path> libraryjars = new ArrayList<>();
    private String packagePrefix = null;
    private boolean allowAccessModification = false;
    private boolean ignoreWarnings = false;
    private boolean obfuscating = true;
    private boolean shrinking = true;
    private boolean printMapping;
    private Path printMappingOutput;
    private boolean verbose = false;
    private final List<String> attributesRemovalPatterns = new ArrayList<>();
    private final Set<ProguardTypeMatcher> dontWarnPatterns = new HashSet<>();
    protected final List<ProguardConfigurationRule> rules = new ArrayList<>();
    private final DexItemFactory dexItemFactory;
    private boolean printSeeds;
    private Path seedFile;
    private Path obfuscationDictionary;
    private Path classObfuscationDictionary;
    private Path packageObfuscationDictionary;

    private Builder(DexItemFactory dexItemFactory) {
      this.dexItemFactory = dexItemFactory;
    }

    public void addInjars(List<Path> injars) {
      this.injars.addAll(injars);
    }

    public void addLibraryJars(List<Path> libraryJars) {
      this.libraryjars.addAll(libraryJars);
    }

    public void setPackagePrefix(String packagePrefix) {
      this.packagePrefix = packagePrefix;
    }

    public void setAllowAccessModification(boolean allowAccessModification) {
      this.allowAccessModification = allowAccessModification;
    }

    public void setIgnoreWarnings(boolean ignoreWarnings) {
      this.ignoreWarnings = ignoreWarnings;
    }

    public void setObfuscating(boolean obfuscate) {
      this.obfuscating = obfuscate;
    }

    public void setShrinking(boolean shrinking) {
      this.shrinking = shrinking;
    }

    public void setPrintMapping(boolean printMapping) {
      this.printMapping = printMapping;
    }

    public void setPrintMappingOutput(Path file) {
      this.printMappingOutput = file;
    }

    public void setVerbose(boolean verbose) {
      this.verbose = verbose;
    }

    public void addAttributeRemovalPattern(String attributesRemovalPattern) {
      this.attributesRemovalPatterns.add(attributesRemovalPattern);
    }

    public void addRule(ProguardConfigurationRule rule) {
      this.rules.add(rule);
    }

    public void addDontWarnPattern(ProguardTypeMatcher pattern) {
      dontWarnPatterns.add(pattern);
    }

    public void setSeedFile(Path seedFile) {
      this.seedFile = seedFile;
    }

    public void setPrintSeed(boolean printSeeds) {
      this.printSeeds = printSeeds;
    }

    public void setObfuscationDictionary(Path obfuscationDictionary) {
      this.obfuscationDictionary = obfuscationDictionary;
    }

    public void setClassObfuscationDictionary(Path classObfuscationDictionary) {
      this.classObfuscationDictionary = classObfuscationDictionary;
    }

    public void setPackageObfuscationDictionary(Path packageObfuscationDictionary) {
      this.packageObfuscationDictionary = packageObfuscationDictionary;
    }

    public ProguardConfiguration build() {
      return new ProguardConfiguration(
          dexItemFactory,
          injars,
          libraryjars,
          packagePrefix,
          allowAccessModification,
          ignoreWarnings,
          obfuscating,
          shrinking,
          printMapping,
          printMappingOutput,
          verbose,
          attributesRemovalPatterns,
          dontWarnPatterns,
          rules,
          printSeeds,
          seedFile,
          DictionaryReader.readAllNames(obfuscationDictionary),
          DictionaryReader.readAllNames(classObfuscationDictionary),
          DictionaryReader.readAllNames(packageObfuscationDictionary));
    }
  }

  private final DexItemFactory dexItemFactory;
  private final List<Path> injars;
  private final List<Path> libraryjars;
  private final String packagePrefix;
  private final boolean allowAccessModification;
  private final boolean ignoreWarnings;
  private final boolean obfuscating;
  private final boolean shrinking;
  private final boolean printMapping;
  private final Path printMappingOutput;
  private final boolean verbose;
  private final List<String> attributesRemovalPatterns;
  private final ImmutableSet<ProguardTypeMatcher> dontWarnPatterns;
  protected final ImmutableList<ProguardConfigurationRule> rules;
  private final boolean printSeeds;
  private final Path seedFile;
  private final List<String> obfuscationDictionary;
  private final List<String> classObfuscationDictionary;
  private final List<String> packageObfuscationDictionary;

  private ProguardConfiguration(
      DexItemFactory factory,
      List<Path> injars,
      List<Path> libraryjars,
      String packagePrefix,
      boolean allowAccessModification,
      boolean ignoreWarnings,
      boolean obfuscating,
      boolean shrinking,
      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) {
    this.dexItemFactory = factory;
    this.injars = ImmutableList.copyOf(injars);
    this.libraryjars = ImmutableList.copyOf(libraryjars);
    this.packagePrefix = packagePrefix;
    this.allowAccessModification = allowAccessModification;
    this.ignoreWarnings = ignoreWarnings;
    this.obfuscating = obfuscating;
    this.shrinking = shrinking;
    this.printMapping = printMapping;
    this.printMappingOutput = printMappingOutput;
    this.verbose = verbose;
    this.attributesRemovalPatterns = ImmutableList.copyOf(attributesRemovalPatterns);
    this.dontWarnPatterns = ImmutableSet.copyOf(dontWarnPatterns);
    this.rules = ImmutableList.copyOf(rules);
    this.printSeeds = printSeeds;
    this.seedFile = seedFile;
    this.obfuscationDictionary = obfuscationDictionary;
    this.classObfuscationDictionary = classObfuscationDictionary;
    this.packageObfuscationDictionary = packageObfuscationDictionary;
  }

  /**
   * Create a new empty builder.
   */
  public static Builder builder(DexItemFactory dexItemFactory) {
    return new Builder(dexItemFactory);
  }

  public DexItemFactory getDexItemFactory() {
    return dexItemFactory;
  }

  public boolean isDefaultConfiguration() {
    return false;
  }

  public List<Path> getInjars() {
    return injars;
  }

  public List<Path> getLibraryjars() {
    return libraryjars;
  }

  public String getPackagePrefix() {
    return packagePrefix;
  }

  public boolean getAllowAccessModification() {
    return allowAccessModification;
  }

  public boolean isPrintingMapping() {
    return printMapping;
  }

  public Path getPrintMappingOutput() {
    return printMappingOutput;
  }

  public boolean isIgnoreWarnings() {
    return ignoreWarnings;
  }

  public boolean isObfuscating() {
    return obfuscating;
  }

  public boolean isShrinking() {
    return shrinking;
  }

  public boolean isVerbose() {
    return verbose;
  }

  public List<String> getAttributesRemovalPatterns() {
    return attributesRemovalPatterns;
  }

  public ImmutableSet<ProguardTypeMatcher> getDontWarnPatterns() {
    return dontWarnPatterns;
  }

  public ImmutableList<ProguardConfigurationRule> getRules() {
    return rules;
  }

  public List<String> getObfuscationDictionary() {
    return obfuscationDictionary;
  }

  public List<String> getPackageObfuscationDictionary() {
    return packageObfuscationDictionary;
  }

  public List<String> getClassObfuscationDictionary() {
    return classObfuscationDictionary;
  }

  public static ProguardConfiguration defaultConfiguration(DexItemFactory dexItemFactory) {
    ProguardConfiguration config = new DefaultProguardConfiguration(dexItemFactory);
    return config;
  }

  public static class DefaultProguardConfiguration extends ProguardConfiguration {

    public DefaultProguardConfiguration(DexItemFactory factory) {
      super(factory,
          ImmutableList.of()    /* injars */,
          ImmutableList.of()    /* libraryjars */,
          ""                    /* package prefix */,
          false                 /* allowAccessModification */,
          false                 /* ignoreWarnings */,
          false                 /* obfuscating */,
          false                 /* shrinking */,
          false                 /* printMapping */,
          null                  /* outputMapping */,
          false                 /* verbose */,
          ImmutableList.of()    /* attributesRemovalPatterns */,
          ImmutableSet.of()     /* dontWarnPatterns */,
          ImmutableList.of(ProguardKeepRule.defaultKeepAllRule()),
          false                 /* printSeeds */,
          null                  /* seedFile */,
          ImmutableList.of()     /* obfuscationDictionary */,
          ImmutableList.of()     /* classObfuscationDictionary */,
          ImmutableList.of()     /* packageObfucationDictionary */);
    }

    @Override
    public boolean isDefaultConfiguration() {
      return true;
    }
  }

  public boolean getPrintSeeds() {
    return printSeeds;
  }

  public Path getSeedFile() {
    return seedFile;
  }
}