aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/rules/android/AndroidResourcesProcessorBuilder.java
blob: 398d4397c2fa9c3df46434152b9efb643c340609 (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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
// Copyright 2015 The Bazel Authors. All rights reserved.
//
// 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.
package com.google.devtools.build.lib.rules.android;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.rules.android.AndroidConfiguration.AndroidAaptVersion;
import com.google.devtools.build.lib.rules.android.AndroidDataConverter.JoinerType;
import com.google.devtools.build.lib.rules.android.DataBinding.DataBindingContext;
import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec.VisibleForSerialization;
import java.util.Collections;
import java.util.List;

/** Builder for creating resource processing action. */
public class AndroidResourcesProcessorBuilder {

  @AutoCodec @VisibleForSerialization
  static final AndroidDataConverter<ValidatedAndroidResources> AAPT2_RESOURCE_DEP_TO_ARG_NO_PARSE =
      AndroidDataConverter.<ValidatedAndroidResources>builder(JoinerType.COLON_COMMA)
          .withRoots(ValidatedAndroidResources::getResourceRoots)
          .withEmpty()
          .withArtifact(ValidatedAndroidResources::getManifest)
          .maybeWithArtifact(ValidatedAndroidResources::getAapt2RTxt)
          .maybeWithArtifact(ValidatedAndroidResources::getCompiledSymbols)
          .build();

  private ResourceDependencies resourceDependencies = ResourceDependencies.empty();
  private AssetDependencies assetDependencies = AssetDependencies.empty();

  private Artifact proguardOut;
  private Artifact mainDexProguardOut;
  private boolean conditionalKeepRules;
  private Artifact rTxtOut;
  private Artifact sourceJarOut;
  private boolean debug = false;
  private ResourceFilterFactory resourceFilterFactory = ResourceFilterFactory.empty();
  private List<String> uncompressedExtensions = Collections.emptyList();
  private Artifact apkOut;
  private String customJavaPackage;
  private String versionCode;
  private String applicationId;
  private String versionName;
  private Artifact symbols;
  private Artifact dataBindingInfoZip;

  private Artifact manifestOut;
  private Artifact mergedResourcesOut;
  private boolean isLibrary;
  private boolean crunchPng = true;
  private Artifact featureOf;
  private Artifact featureAfter;
  private AndroidAaptVersion aaptVersion;
  private boolean throwOnResourceConflict;
  private String packageUnderTest;
  private boolean useCompiledResourcesForMerge;
  private boolean isTestWithResources = false;

  /**
   * The output zip for resource-processed data binding expressions (i.e. a zip of .xml files).
   *
   * <p>If null, data binding processing is skipped (and data binding expressions aren't allowed in
   * layout resources).
   */
  public AndroidResourcesProcessorBuilder setDataBindingInfoZip(Artifact zip) {
    this.dataBindingInfoZip = zip;
    return this;
  }

  public AndroidResourcesProcessorBuilder withResourceDependencies(
      ResourceDependencies resourceDeps) {
    this.resourceDependencies = resourceDeps;
    return this;
  }

  public AndroidResourcesProcessorBuilder withAssetDependencies(AssetDependencies assetDeps) {
    this.assetDependencies = assetDeps;
    return this;
  }

  public AndroidResourcesProcessorBuilder setUncompressedExtensions(
      List<String> uncompressedExtensions) {
    this.uncompressedExtensions = uncompressedExtensions;
    return this;
  }

  public AndroidResourcesProcessorBuilder setCrunchPng(boolean crunchPng) {
    this.crunchPng = crunchPng;
    return this;
  }

  public AndroidResourcesProcessorBuilder setResourceFilterFactory(
      ResourceFilterFactory resourceFilterFactory) {
    this.resourceFilterFactory = resourceFilterFactory;
    return this;
  }

  public AndroidResourcesProcessorBuilder setDebug(boolean debug) {
    this.debug = debug;
    return this;
  }

  public AndroidResourcesProcessorBuilder setProguardOut(Artifact proguardCfg) {
    this.proguardOut = proguardCfg;
    return this;
  }

  public AndroidResourcesProcessorBuilder conditionalKeepRules(boolean conditionalKeepRules) {
    this.conditionalKeepRules = conditionalKeepRules;
    return this;
  }

  public AndroidResourcesProcessorBuilder setMainDexProguardOut(Artifact mainDexProguardCfg) {
    this.mainDexProguardOut = mainDexProguardCfg;
    return this;
  }

  public AndroidResourcesProcessorBuilder setRTxtOut(Artifact rTxtOut) {
    this.rTxtOut = rTxtOut;
    return this;
  }

  public AndroidResourcesProcessorBuilder setSymbols(Artifact symbols) {
    this.symbols = symbols;
    return this;
  }

  public AndroidResourcesProcessorBuilder setApkOut(Artifact apkOut) {
    this.apkOut = apkOut;
    return this;
  }

  public AndroidResourcesProcessorBuilder setSourceJarOut(Artifact sourceJarOut) {
    this.sourceJarOut = sourceJarOut;
    return this;
  }

  public AndroidResourcesProcessorBuilder setManifestOut(Artifact manifestOut) {
    this.manifestOut = manifestOut;
    return this;
  }

  public AndroidResourcesProcessorBuilder setMergedResourcesOut(Artifact mergedResourcesOut) {
    this.mergedResourcesOut = mergedResourcesOut;
    return this;
  }

  public AndroidResourcesProcessorBuilder setLibrary(boolean isLibrary) {
    this.isLibrary = isLibrary;
    return this;
  }

  public AndroidResourcesProcessorBuilder setFeatureOf(Artifact featureOf) {
    this.featureOf = featureOf;
    return this;
  }

  public AndroidResourcesProcessorBuilder setFeatureAfter(Artifact featureAfter) {
    this.featureAfter = featureAfter;
    return this;
  }

  public AndroidResourcesProcessorBuilder targetAaptVersion(AndroidAaptVersion aaptVersion) {
    this.aaptVersion = aaptVersion;
    return this;
  }

  public AndroidResourcesProcessorBuilder setThrowOnResourceConflict(
      boolean throwOnResourceConflict) {
    this.throwOnResourceConflict = throwOnResourceConflict;
    return this;
  }

  /**
   * Creates and registers an action that processes only transitive data.
   *
   * <p>Local resources and assets will be completely ignored by this action.
   *
   * @return a {@link ResourceApk} containing the processed resource, asset, and manifest
   *     information.
   */
  public ResourceApk buildWithoutLocalResources(
      AndroidDataContext dataContext,
      StampedAndroidManifest manifest,
      DataBindingContext dataBindingContext) {

    build(
        dataContext, AndroidResources.empty(), AndroidAssets.empty(), manifest, dataBindingContext);

    return ResourceApk.fromTransitiveResources(
        resourceDependencies,
        assetDependencies,
        manifest.withProcessedManifest(manifestOut == null ? manifest.getManifest() : manifestOut),
        rTxtOut,
        dataBindingContext);
  }

  public ProcessedAndroidData build(
      AndroidDataContext dataContext,
      AndroidResources primaryResources,
      AndroidAssets primaryAssets,
      StampedAndroidManifest primaryManifest,
      DataBindingContext dataBindingContext) {

    if (aaptVersion == AndroidAaptVersion.AAPT2) {
      createAapt2ApkAction(dataContext, primaryResources, primaryAssets, primaryManifest);
    } else {
      createAaptAction(dataContext, primaryResources, primaryAssets, primaryManifest);
    }

    // Wrap the new manifest, if any
    ProcessedAndroidManifest processedManifest =
        new ProcessedAndroidManifest(
            manifestOut == null ? primaryManifest.getManifest() : manifestOut,
            primaryManifest.getPackage(),
            primaryManifest.isExported());

    // Wrap the parsed resources
    ParsedAndroidResources parsedResources =
        ParsedAndroidResources.of(
            primaryResources,
            symbols,
            /* compiledSymbols = */ null,
            dataContext.getLabel(),
            processedManifest,
            dataBindingContext);

    // Wrap the parsed and merged assets
    ParsedAndroidAssets parsedAssets =
        ParsedAndroidAssets.of(
            primaryAssets, symbols, /* compiledSymbols = */ null, dataContext.getLabel());
    MergedAndroidAssets mergedAssets =
        MergedAndroidAssets.of(parsedAssets, mergedResourcesOut, assetDependencies);

    return ProcessedAndroidData.of(
        parsedResources,
        mergedAssets,
        processedManifest,
        rTxtOut,
        sourceJarOut,
        apkOut,
        dataBindingInfoZip,
        resourceDependencies,
        proguardOut,
        mainDexProguardOut);
  }

  public AndroidResourcesProcessorBuilder setJavaPackage(String customJavaPackage) {
    this.customJavaPackage = customJavaPackage;
    return this;
  }

  public AndroidResourcesProcessorBuilder setVersionCode(String versionCode) {
    this.versionCode = versionCode;
    return this;
  }

  public AndroidResourcesProcessorBuilder setApplicationId(String applicationId) {
    if (applicationId != null && !applicationId.isEmpty()) {
      this.applicationId = applicationId;
    }
    return this;
  }

  public AndroidResourcesProcessorBuilder setVersionName(String versionName) {
    this.versionName = versionName;
    return this;
  }

  public AndroidResourcesProcessorBuilder setPackageUnderTest(String packageUnderTest) {
    this.packageUnderTest = packageUnderTest;
    return this;
  }

  public AndroidResourcesProcessorBuilder setUseCompiledResourcesForMerge(
      boolean useCompiledResourcesForMerge) {
    this.useCompiledResourcesForMerge = useCompiledResourcesForMerge;
    return this;
  }

  public AndroidResourcesProcessorBuilder setIsTestWithResources(boolean isTestWithResources) {
    this.isTestWithResources = isTestWithResources;
    return this;
  }

  private void createAapt2ApkAction(
      AndroidDataContext dataContext,
      AndroidResources primaryResources,
      AndroidAssets primaryAssets,
      StampedAndroidManifest primaryManifest) {
    BusyBoxActionBuilder builder =
        BusyBoxActionBuilder.create(dataContext, "AAPT2_PACKAGE").addAapt(AndroidAaptVersion.AAPT2);

    if (resourceDependencies != null) {
      builder
          .addTransitiveFlag(
              "--data",
              resourceDependencies.getTransitiveResourceContainers(),
              useCompiledResourcesForMerge
                  ? AAPT2_RESOURCE_DEP_TO_ARG_NO_PARSE
                  : AndroidDataConverter.AAPT2_RESOURCES_AND_MANIFEST_CONVERTER)
          .addTransitiveFlag(
              "--directData",
              resourceDependencies.getDirectResourceContainers(),
              useCompiledResourcesForMerge
                  ? AAPT2_RESOURCE_DEP_TO_ARG_NO_PARSE
                  : AndroidDataConverter.AAPT2_RESOURCES_AND_MANIFEST_CONVERTER)
          .addTransitiveInputValues(resourceDependencies.getTransitiveResources())
          .addTransitiveInputValues(resourceDependencies.getTransitiveManifests())
          .addTransitiveInputValues(resourceDependencies.getTransitiveAapt2RTxt())
          .addTransitiveInputValues(resourceDependencies.getTransitiveCompiledSymbols());

      if (!useCompiledResourcesForMerge) {
        builder.addTransitiveInputValues(resourceDependencies.getTransitiveSymbolsBin());
      }
    }

    if (assetDependencies != null && !assetDependencies.getTransitiveAssets().isEmpty()) {
      builder
          .addTransitiveFlag(
              "--directAssets",
              assetDependencies.getDirectParsedAssets(),
              useCompiledResourcesForMerge
                  ? AndroidDataConverter.COMPILED_ASSET_CONVERTER
                  : AndroidDataConverter.PARSED_ASSET_CONVERTER)
          .addTransitiveFlag(
              "--assets",
              assetDependencies.getTransitiveParsedAssets(),
              useCompiledResourcesForMerge
                  ? AndroidDataConverter.COMPILED_ASSET_CONVERTER
                  : AndroidDataConverter.PARSED_ASSET_CONVERTER)
          .addTransitiveInputValues(assetDependencies.getTransitiveAssets())
          .addTransitiveInputValues(
              useCompiledResourcesForMerge
                  ? assetDependencies.getTransitiveCompiledSymbols()
                  : assetDependencies.getTransitiveSymbols());
    }

    builder
        .maybeAddFlag("--useCompiledResourcesForMerge", useCompiledResourcesForMerge)
        .maybeAddFlag("--conditionalKeepRules", conditionalKeepRules);

    configureCommonFlags(dataContext, primaryResources, primaryAssets, primaryManifest, builder)
        .buildAndRegister("Processing Android resources", "AndroidAapt2");
  }

  private void createAaptAction(
      AndroidDataContext dataContext,
      AndroidResources primaryResources,
      AndroidAssets primaryAssets,
      StampedAndroidManifest primaryManifest) {
    BusyBoxActionBuilder builder = BusyBoxActionBuilder.create(dataContext, "PACKAGE");

    if (resourceDependencies != null) {
      builder
          .addTransitiveFlag(
              "--data",
              resourceDependencies.getTransitiveResourceContainers(),
              AndroidDataConverter.AAPT_RESOURCES_AND_MANIFEST_CONVERTER)
          .addTransitiveFlag(
              "--directData",
              resourceDependencies.getDirectResourceContainers(),
              AndroidDataConverter.AAPT_RESOURCES_AND_MANIFEST_CONVERTER)
          .addTransitiveInputValues(resourceDependencies.getTransitiveResources())
          .addTransitiveInputValues(resourceDependencies.getTransitiveManifests())
          .addTransitiveInputValues(resourceDependencies.getTransitiveRTxt())
          .addTransitiveInputValues(resourceDependencies.getTransitiveSymbolsBin());
    }

    if (assetDependencies != null && !assetDependencies.getTransitiveAssets().isEmpty()) {
      builder
          .addTransitiveFlag(
              "--directAssets",
              assetDependencies.getDirectParsedAssets(),
              AndroidDataConverter.PARSED_ASSET_CONVERTER)
          .addTransitiveFlag(
              "--assets",
              assetDependencies.getTransitiveParsedAssets(),
              AndroidDataConverter.PARSED_ASSET_CONVERTER)
          .addTransitiveInputValues(assetDependencies.getTransitiveAssets())
          .addTransitiveInputValues(assetDependencies.getTransitiveSymbols());
    }

    builder.addAapt(AndroidAaptVersion.AAPT);

    configureCommonFlags(dataContext, primaryResources, primaryAssets, primaryManifest, builder)
        .maybeAddVectoredFlag(
            "--prefilteredResources", resourceFilterFactory.getResourcesToIgnoreInExecution())
        .buildAndRegister("Processing Android resources", "AaptPackage");
  }

  private BusyBoxActionBuilder configureCommonFlags(
      AndroidDataContext dataContext,
      AndroidResources primaryResources,
      AndroidAssets primaryAssets,
      StampedAndroidManifest primaryManifest,
      BusyBoxActionBuilder builder) {

    return builder
        .addInput(
            "--primaryData",
            String.format(
                "%s:%s:%s",
                AndroidDataConverter.rootsToString(primaryResources.getResourceRoots()),
                AndroidDataConverter.rootsToString(primaryAssets.getAssetRoots()),
                primaryManifest.getManifest().getExecPathString()),
            Iterables.concat(
                primaryResources.getResources(),
                primaryAssets.getAssets(),
                ImmutableList.of(primaryManifest.getManifest())))
        .maybeAddFlag("--buildToolsVersion", dataContext.getSdk().getBuildToolsVersion())
        .addAndroidJar()
        .maybeAddFlag("--packageType", isLibrary)
        .maybeAddFlag("LIBRARY", isLibrary)
        .maybeAddOutput("--rOutput", rTxtOut)
        .maybeAddOutput("--symbolsOut", symbols)
        .maybeAddOutput("--srcJarOutput", sourceJarOut)
        .maybeAddOutput("--proguardOutput", proguardOut)
        .maybeAddOutput("--mainDexProguardOutput", mainDexProguardOut)
        .maybeAddOutput("--manifestOutput", manifestOut)
        .maybeAddOutput("--resourcesOutput", mergedResourcesOut)
        .maybeAddOutput("--packagePath", apkOut)

        // Always pass density and resource configuration filter strings to execution, even when
        // filtering in analysis. Filtering in analysis cannot remove resources from Filesets, and,
        // in addition, aapt needs access to resource filters to generate pseudolocalized resources
        // and because its resource filtering is somewhat stricter for locales, and resource
        // processing needs access to densities to add them to the manifest.
        .maybeAddFlag("--resourceConfigs", resourceFilterFactory.getConfigurationFilterString())
        .maybeAddFlag("--densities", resourceFilterFactory.getDensityString())
        .maybeAddVectoredFlag("--uncompressedExtensions", uncompressedExtensions)
        .maybeAddFlag("--useAaptCruncher=no", !crunchPng)
        .maybeAddFlag("--debug", debug)
        .maybeAddFlag("--versionCode", versionCode)
        .maybeAddFlag("--versionName", versionName)
        .maybeAddFlag("--applicationId", applicationId)
        .maybeAddOutput("--dataBindingInfoOut", dataBindingInfoZip)
        .maybeAddFlag("--packageForR", customJavaPackage)
        .maybeAddInput("--featureOf", featureOf)
        .maybeAddInput("--featureAfter", featureAfter)
        .maybeAddFlag("--throwOnResourceConflict", throwOnResourceConflict)
        .maybeAddFlag("--packageUnderTest", packageUnderTest)
        .maybeAddFlag("--isTestWithResources", isTestWithResources);
  }
}