aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/rules/android/ApplicationManifest.java
blob: 91579b6f0e6077c786f2fc0c91dc2760be261dc6 (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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
// 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 static com.google.common.base.Strings.isNullOrEmpty;
import static com.google.devtools.build.lib.syntax.Type.STRING;

import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSortedMap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.analysis.FileProvider;
import com.google.devtools.build.lib.analysis.RuleConfiguredTarget.Mode;
import com.google.devtools.build.lib.analysis.RuleContext;
import com.google.devtools.build.lib.analysis.TransitiveInfoCollection;
import com.google.devtools.build.lib.analysis.actions.CustomCommandLine;
import com.google.devtools.build.lib.analysis.actions.FileWriteAction;
import com.google.devtools.build.lib.analysis.actions.SpawnAction;
import com.google.devtools.build.lib.analysis.config.CompilationMode;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.packages.RuleClass.ConfiguredTargetFactory.RuleErrorException;
import com.google.devtools.build.lib.rules.android.AndroidConfiguration.AndroidAaptVersion;
import com.google.devtools.build.lib.rules.android.AndroidConfiguration.AndroidManifestMerger;
import com.google.devtools.build.lib.rules.android.ResourceContainer.Builder.JavaPackageSource;
import com.google.devtools.build.lib.rules.android.ResourceContainer.ResourceType;
import com.google.devtools.build.lib.syntax.Type;
import com.google.devtools.build.lib.util.Preconditions;
import com.google.devtools.build.lib.vfs.PathFragment;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import javax.annotation.Nullable;

/** Represents a AndroidManifest, that may have been merged from dependencies. */
public final class ApplicationManifest {

  public static ApplicationManifest fromResourcesRule(RuleContext ruleContext)
      throws RuleErrorException {
    final AndroidResourcesProvider resources = AndroidCommon.getAndroidResources(ruleContext);
    if (resources == null) {
      ruleContext.attributeError("manifest", "a resources or manifest attribute is mandatory.");
      return null;
    }
    return fromExplicitManifest(
        ruleContext, Iterables.getOnlyElement(resources.getDirectAndroidResources()).getManifest());
  }

  public ApplicationManifest createSplitManifest(
      RuleContext ruleContext, String splitName, boolean hasCode) {
    // aapt insists that manifests be called AndroidManifest.xml, even though they have to be
    // explicitly designated as manifests on the command line
    Artifact result = AndroidBinary.getDxArtifact(
        ruleContext, "split_" + splitName + "/AndroidManifest.xml");
    SpawnAction.Builder builder =
        new SpawnAction.Builder()
            .setExecutable(
                ruleContext.getExecutablePrerequisite("$build_split_manifest", Mode.HOST))
            .setProgressMessage("Creating manifest for split %s", splitName)
            .setMnemonic("AndroidBuildSplitManifest")
            .addInput(manifest)
            .addOutput(result);
    CustomCommandLine.Builder commandLine =
        CustomCommandLine.builder()
            .addExecPath("--main_manifest", manifest)
            .addExecPath("--split_manifest", result)
            .add("--split", splitName);
    if (hasCode) {
      commandLine.add("--hascode");
    } else {
      commandLine.add("--nohascode");
    }

    String overridePackage = manifestValues.get("applicationId");
    if (overridePackage != null) {
      commandLine.add("--override_package", overridePackage);
    }

    builder.setCommandLine(commandLine.build());
    ruleContext.registerAction(builder.build(ruleContext));
    return new ApplicationManifest(ruleContext, result, targetAaptVersion);
  }

  public ApplicationManifest addMobileInstallStubApplication(RuleContext ruleContext)
      throws InterruptedException {

    Artifact stubManifest = ruleContext.getImplicitOutputArtifact(
            AndroidRuleClasses.MOBILE_INSTALL_STUB_APPLICATION_MANIFEST);
    Artifact stubData =
        ruleContext.getImplicitOutputArtifact(
            AndroidRuleClasses.MOBILE_INSTALL_STUB_APPLICATION_DATA);

    SpawnAction.Builder builder =
        new SpawnAction.Builder()
            .setExecutable(ruleContext.getExecutablePrerequisite("$stubify_manifest", Mode.HOST))
            .setProgressMessage("Injecting mobile install stub application")
            .setMnemonic("InjectMobileInstallStubApplication")
            .addInput(manifest)
            .addOutput(stubManifest)
            .addOutput(stubData);
    CustomCommandLine.Builder commandLine =
        CustomCommandLine.builder()
            .add("--mode=mobile_install")
            .addExecPath("--input_manifest", manifest)
            .addExecPath("--output_manifest", stubManifest)
            .addExecPath("--output_datafile", stubData);

    String overridePackage = manifestValues.get("applicationId");
    if (overridePackage != null) {
      commandLine.add("--override_package", overridePackage);
    }

    builder.setCommandLine(commandLine.build());
    ruleContext.registerAction(builder.build(ruleContext));

    return new ApplicationManifest(ruleContext, stubManifest, targetAaptVersion);
  }

  public ApplicationManifest addInstantRunStubApplication(RuleContext ruleContext)
      throws InterruptedException {

    Artifact stubManifest = ruleContext.getImplicitOutputArtifact(
        AndroidRuleClasses.INSTANT_RUN_STUB_APPLICATION_MANIFEST);

    SpawnAction.Builder builder =
        new SpawnAction.Builder()
            .setExecutable(ruleContext.getExecutablePrerequisite("$stubify_manifest", Mode.HOST))
            .setProgressMessage("Injecting instant run stub application")
            .setMnemonic("InjectInstantRunStubApplication")
            .addInput(manifest)
            .addOutput(stubManifest)
            .setCommandLine(
                CustomCommandLine.builder()
                    .add("--mode=instant_run")
                    .addExecPath("--input_manifest", manifest)
                    .addExecPath("--output_manifest", stubManifest)
                    .build());

    ruleContext.registerAction(builder.build(ruleContext));

    return new ApplicationManifest(ruleContext, stubManifest, targetAaptVersion);
  }

  public static ApplicationManifest fromRule(RuleContext ruleContext) throws RuleErrorException {
    return fromExplicitManifest(
        ruleContext, ruleContext.getPrerequisiteArtifact("manifest", Mode.TARGET));
  }

  public static ApplicationManifest fromExplicitManifest(RuleContext ruleContext, Artifact manifest)
      throws RuleErrorException {
    return new ApplicationManifest(
        ruleContext, manifest, AndroidAaptVersion.chooseTargetAaptVersion(ruleContext));
  }

  /**
   * Generates an empty manifest for a rule that does not directly specify resources.
   *
   * <p><strong>Note:</strong> This generated manifest can then be used as the primary manifest when
   * merging with dependencies.
   *
   * @return the generated ApplicationManifest
   */
  public static ApplicationManifest generatedManifest(RuleContext ruleContext)
      throws RuleErrorException {
    Artifact generatedManifest = ruleContext.getUniqueDirectoryArtifact(
        ruleContext.getRule().getName() + "_generated", PathFragment.create("AndroidManifest.xml"),
        ruleContext.getBinOrGenfilesDirectory());

    String manifestPackage = AndroidCommon.getJavaPackage(ruleContext);
    String contents = Joiner.on("\n").join(
        "<?xml version=\"1.0\" encoding=\"utf-8\"?>",
        "<manifest xmlns:android=\"http://schemas.android.com/apk/res/android\"",
        "          package=\"" + manifestPackage + "\">",
        "   <application>",
        "   </application>",
        "</manifest>");
    ruleContext
        .getAnalysisEnvironment()
        .registerAction(
            FileWriteAction.create(
                ruleContext, generatedManifest, contents, /*makeExecutable=*/ false));
    return fromExplicitManifest(ruleContext, generatedManifest);
  }

  private static ImmutableMap<String, String> getManifestValues(RuleContext context) {
    Map<String, String> manifestValues = new TreeMap<>();
    // applicationId is set from manifest_values or android_resources.rename_manifest_package
    // with descending priority.
    AndroidResourcesProvider resourcesProvider = AndroidCommon.getAndroidResources(context);
    if (resourcesProvider != null) {
      ResourceContainer resourceContainer = Iterables.getOnlyElement(
          resourcesProvider.getDirectAndroidResources());
      if (resourceContainer.getRenameManifestPackage() != null) {
        manifestValues.put("applicationId", resourceContainer.getRenameManifestPackage());
      }
    }
    if (context.attributes().isAttributeValueExplicitlySpecified("manifest_values")) {
      manifestValues.putAll(context.attributes().get("manifest_values", Type.STRING_DICT));
    }

    for (String variable : manifestValues.keySet()) {
      manifestValues.put(
          variable, context.expandMakeVariables("manifest_values", manifestValues.get(variable)));
    }
    return ImmutableMap.copyOf(manifestValues);
  }

  private final Artifact manifest;
  private final ImmutableMap<String, String> manifestValues;
  private final AndroidAaptVersion targetAaptVersion;

  private ApplicationManifest(
      RuleContext ruleContext, Artifact manifest, AndroidAaptVersion targetAaptVersion) {
    this.manifest = manifest;
    this.manifestValues = getManifestValues(ruleContext);
    this.targetAaptVersion = targetAaptVersion;
  }

  public ApplicationManifest mergeWith(RuleContext ruleContext, ResourceDependencies resourceDeps) {
    boolean legacy = useLegacyMerging(ruleContext);
    return mergeWith(ruleContext, resourceDeps, legacy);
  }

  public ApplicationManifest mergeWith(
      RuleContext ruleContext, ResourceDependencies resourceDeps, boolean legacy) {
    Map<Artifact, Label> mergeeManifests = getMergeeManifests(resourceDeps.getResources());

    if (legacy) {
      if (!mergeeManifests.isEmpty()) {
        Artifact outputManifest = ruleContext.getUniqueDirectoryArtifact(
            ruleContext.getRule().getName() + "_merged", "AndroidManifest.xml",
            ruleContext.getBinOrGenfilesDirectory());
        AndroidManifestMergeHelper.createMergeManifestAction(ruleContext, getManifest(),
            mergeeManifests.keySet(), ImmutableList.of("all"), outputManifest);
        return new ApplicationManifest(ruleContext, outputManifest, targetAaptVersion);
      }
    } else {
      if (!mergeeManifests.isEmpty() || !manifestValues.isEmpty()) {
        Artifact outputManifest = ruleContext.getUniqueDirectoryArtifact(
            ruleContext.getRule().getName() + "_merged", "AndroidManifest.xml",
            ruleContext.getBinOrGenfilesDirectory());
        Artifact mergeLog = ruleContext.getUniqueDirectoryArtifact(
            ruleContext.getRule().getName() + "_merged", "manifest_merger_log.txt",
            ruleContext.getBinOrGenfilesDirectory());
        new ManifestMergerActionBuilder(ruleContext)
            .setManifest(getManifest())
            .setMergeeManifests(mergeeManifests)
            .setLibrary(false)
            .setManifestValues(manifestValues)
            .setCustomPackage(AndroidCommon.getJavaPackage(ruleContext))
            .setManifestOutput(outputManifest)
            .setLogOut(mergeLog)
            .build(ruleContext);
        return new ApplicationManifest(ruleContext, outputManifest, targetAaptVersion);
      }
    }
    return this;
  }

  private boolean useLegacyMerging(RuleContext ruleContext) {
    boolean legacy = true;
    if (ruleContext.isLegalFragment(AndroidConfiguration.class)
        && ruleContext.getRule().isAttrDefined("manifest_merger", STRING)) {
      AndroidManifestMerger merger = AndroidManifestMerger.fromString(
          ruleContext.attributes().get("manifest_merger", STRING));
      if (merger == null) {
        merger = ruleContext.getFragment(AndroidConfiguration.class).getManifestMerger();
      }
      if (merger == AndroidManifestMerger.LEGACY) {
        ruleContext.ruleWarning(
            "manifest_merger 'legacy' is deprecated. Please update to 'android'.\n"
                + "See https://developer.android.com/studio/build/manifest-merge.html for more "
                + "information about the manifest merger.");
      }
      legacy = merger == AndroidManifestMerger.LEGACY;
    }
    return legacy;
  }

  private static Map<Artifact, Label> getMergeeManifests(
      Iterable<ResourceContainer> resourceContainers) {
    ImmutableSortedMap.Builder<Artifact, Label> builder =
        ImmutableSortedMap.orderedBy(Artifact.EXEC_PATH_COMPARATOR);
    for (ResourceContainer r : resourceContainers) {
      if (r.isManifestExported()) {
        builder.put(r.getManifest(), r.getLabel());
      }
    }
    return builder.build();
  }

  public ApplicationManifest renamePackage(RuleContext ruleContext, String customPackage) {
    if (isNullOrEmpty(customPackage)) {
      return this;
    }
    Artifact outputManifest = ruleContext.getUniqueDirectoryArtifact(
        ruleContext.getRule().getName() + "_renamed", "AndroidManifest.xml",
        ruleContext.getBinOrGenfilesDirectory());
    new ManifestMergerActionBuilder(ruleContext)
        .setManifest(getManifest())
        .setLibrary(true)
        .setCustomPackage(customPackage)
        .setManifestOutput(outputManifest)
        .build(ruleContext);
    return new ApplicationManifest(ruleContext, outputManifest, targetAaptVersion);
  }

  public ResourceApk packTestWithDataAndResources(
      RuleContext ruleContext,
      Artifact resourceApk,
      ResourceDependencies resourceDeps,
      boolean incremental,
      Artifact proguardCfg,
      @Nullable String packageUnderTest)
      throws InterruptedException, RuleErrorException {
    LocalResourceContainer data = new LocalResourceContainer.Builder(ruleContext)
        .withAssets(
            AndroidCommon.getAssetDir(ruleContext),
            ruleContext.getPrerequisitesIf(
                // TODO(bazel-team): Remove the ResourceType construct.
                ResourceType.ASSETS.getAttribute(),
                Mode.TARGET,
                FileProvider.class))
        .withResources(
            ruleContext.getPrerequisites(
                "local_resource_files",
                Mode.TARGET,
                FileProvider.class)).build();
    ResourceContainer.Builder builder =
        ResourceContainer.builderFromRule(ruleContext)
            .setAssetsAndResourcesFrom(data)
            .setManifest(getManifest())
            .setApk(resourceApk);

    if (ruleContext.hasErrors()) {
      return null;
    }

    return createApk(
        ruleContext,
        false, /* isLibrary */
        resourceDeps,
        ImmutableList.of(), /* uncompressedExtensions */
        true, /* crunchPng */
        incremental,
        builder,
        data,
        proguardCfg,
        null, /* Artifact mainDexProguardCfg */
        null /* Artifact manifestOut */,
        null, /* Artifact mergedResources */
        null, /* Artifact dataBindingInfoZip */
        null, /* featureOf */
        null /* featureAfter */,
        packageUnderTest);
  }

  /** Packages up the manifest with resource and assets from the LocalResourceContainer. */
  public ResourceApk packAarWithDataAndResources(
      RuleContext ruleContext,
      LocalResourceContainer data,
      ResourceDependencies resourceDeps,
      Artifact rTxt,
      Artifact symbols,
      Artifact manifestOut,
      Artifact mergedResources) throws InterruptedException, RuleErrorException {
    if (ruleContext.hasErrors()) {
      return null;
    }
    ResourceContainer.Builder builder =
        ResourceContainer.builderFromRule(ruleContext)
            .setRTxt(rTxt)
            .setSymbols(symbols)
            .setJavaPackageFrom(JavaPackageSource.MANIFEST)
            .setManifestExported(true);

    return createApk(
        ruleContext,
        true, /* isLibrary */
        resourceDeps,
        ImmutableList.of(), /* List<String> uncompressedExtensions */
        false, /* crunchPng */
        false, /* incremental */
        builder,
        data,
        null, /* Artifact proguardCfg */
        null, /* Artifact mainDexProguardCfg */
        manifestOut,
        mergedResources,
        null, /* Artifact dataBindingInfoZip */
        null, /* Artifact featureOf */
        null /* Artifact featureAfter */,
        null /* packageUnderTest */);
  }

  /* Creates an incremental apk from assets and data. */
  public ResourceApk packIncrementalBinaryWithDataAndResources(
      RuleContext ruleContext,
      Artifact resourceApk,
      ResourceDependencies resourceDeps,
      List<String> uncompressedExtensions,
      boolean crunchPng,
      Artifact proguardCfg)
      throws InterruptedException, RuleErrorException {
    LocalResourceContainer data =
        new LocalResourceContainer.Builder(ruleContext)
            .withAssets(
                AndroidCommon.getAssetDir(ruleContext),
                ruleContext.getPrerequisitesIf(
                    // TODO(bazel-team): Remove the ResourceType construct.
                    ResourceType.ASSETS.getAttribute(), Mode.TARGET, FileProvider.class))
            .withResources(
                ruleContext.getPrerequisites("resource_files", Mode.TARGET, FileProvider.class))
            .build();
    if (ruleContext.hasErrors()) {
      return null;
    }
    return createApk(
        ruleContext,
        false /* isLibrary */,
        resourceDeps,
        uncompressedExtensions,
        crunchPng,
        true,
        ResourceContainer.builderFromRule(ruleContext)
            .setApk(resourceApk),
        data,
        proguardCfg,
        null, /* mainDexProguardCfg */
        null, /* manifestOut */
        null, /* mergedResources */
        null, /* dataBindingInfoZip */
        null, /* featureOf */
        null /* featureAfter */,
        null /* packageUnderTest */);
  }

  /** Packages up the manifest with resource and assets from the rule and dependent resources. */
  // TODO(bazel-team): this method calls for some refactoring, 15+ params including some nullables.
  public ResourceApk packBinaryWithDataAndResources(
      RuleContext ruleContext,
      Artifact resourceApk,
      ResourceDependencies resourceDeps,
      @Nullable Artifact rTxt,
      ResourceFilter resourceFilter,
      List<String> uncompressedExtensions,
      boolean crunchPng,
      Artifact proguardCfg,
      @Nullable Artifact mainDexProguardCfg,
      Artifact manifestOut,
      Artifact mergedResources,
      @Nullable Artifact dataBindingInfoZip,
      @Nullable Artifact featureOf,
      @Nullable Artifact featureAfter)
      throws InterruptedException, RuleErrorException {
    LocalResourceContainer data = new LocalResourceContainer.Builder(ruleContext)
        .withAssets(
            AndroidCommon.getAssetDir(ruleContext),
            ruleContext.getPrerequisitesIf(
                // TODO(bazel-team): Remove the ResourceType construct.
                ResourceType.ASSETS.getAttribute(),
                Mode.TARGET,
                FileProvider.class))
        .withResources(
            ruleContext.getPrerequisites(
                "resource_files",
                Mode.TARGET,
                FileProvider.class)).build();
    ResourceContainer.Builder builder =
        ResourceContainer.builderFromRule(ruleContext)
            .setAssetsAndResourcesFrom(data)
            .setManifest(getManifest())
            .setRTxt(rTxt)
            .setApk(resourceApk);

    if (ruleContext.hasErrors()) {
      return null;
    }
    return createApk(
        ruleContext,
        false /* isLibrary */,
        resourceDeps,
        uncompressedExtensions,
        crunchPng,
        false /* incremental */,
        builder,
        data,
        proguardCfg,
        mainDexProguardCfg,
        manifestOut,
        mergedResources,
        dataBindingInfoZip,
        featureOf,
        featureAfter,
        null /* packageUnderTest */);
  }

  public ResourceApk packLibraryWithDataAndResources(
      RuleContext ruleContext,
      ResourceDependencies resourceDeps,
      Artifact rTxt,
      Artifact symbols,
      Artifact manifestOut,
      Artifact mergedResources,
      Artifact dataBindingInfoZip)
      throws InterruptedException, RuleErrorException {
    LocalResourceContainer data =
        new LocalResourceContainer.Builder(ruleContext)
            .withAssets(
                AndroidCommon.getAssetDir(ruleContext),
                ruleContext.getPrerequisitesIf(
                    // TODO(bazel-team): Remove the ResourceType construct.
                    ResourceType.ASSETS.getAttribute(), Mode.TARGET, FileProvider.class))
            .withResources(
                ruleContext.getPrerequisites("resource_files", Mode.TARGET, FileProvider.class))
            .build();
    if (ruleContext.hasErrors()) {
      return null;
    }
    ResourceContainer.Builder builder =
        ResourceContainer.builderFromRule(ruleContext)
            .setAssetsAndResourcesFrom(data)
            .setManifest(getManifest())
            .setSymbols(symbols)
            .setRTxt(rTxt)
            // Request an APK so it can be inherited when a library is used in a binary's
            // resources attr.
            // TODO(b/30307842): Remove this once it is no longer needed for resources migration.
            .setApk(ruleContext.getImplicitOutputArtifact(AndroidRuleClasses.ANDROID_LIBRARY_APK));

    if (targetAaptVersion == AndroidAaptVersion.AAPT2) {

      builder
          .setAapt2JavaSourceJar(
              ruleContext.getImplicitOutputArtifact(
                  AndroidRuleClasses.ANDROID_RESOURCES_AAPT2_SOURCE_JAR))
          .setAapt2RTxt(
              ruleContext.getImplicitOutputArtifact(
                  AndroidRuleClasses.ANDROID_RESOURCES_AAPT2_R_TXT))
          .setCompiledSymbols(
              ruleContext.getImplicitOutputArtifact(AndroidRuleClasses.ANDROID_COMPILED_SYMBOLS))
          .setStaticLibrary(
              ruleContext.getImplicitOutputArtifact(
                  AndroidRuleClasses.ANDROID_RESOURCES_AAPT2_LIBRARY_APK));
    }
    return createApk(
        ruleContext,
        true /* isLibrary */,
        resourceDeps,
        ImmutableList.of() /* uncompressedExtensions */,
        false /* crunchPng */,
        false /* incremental */,
        builder,
        data,
        null /* proguardCfg */,
        null /* mainDexProguardCfg */,
        manifestOut,
        mergedResources,
        dataBindingInfoZip,
        null /* featureOf */,
        null /* featureAfter */,
        null /* packageUnderTest */);
  }

  private ResourceApk createApk(
      RuleContext ruleContext,
      boolean isLibrary,
      ResourceDependencies resourceDeps,
      List<String> uncompressedExtensions,
      boolean crunchPng,
      boolean incremental,
      ResourceContainer.Builder maybeInlinedResourceContainerBuilder,
      LocalResourceContainer data,
      Artifact proguardCfg,
      @Nullable Artifact mainDexProguardCfg,
      Artifact manifestOut,
      Artifact mergedResources,
      Artifact dataBindingInfoZip,
      @Nullable Artifact featureOf,
      @Nullable Artifact featureAfter,
      @Nullable String packageUnderTest)
      throws InterruptedException, RuleErrorException {
    // Filter the resources during analysis to prevent processing of and dependencies on unwanted
    // resources during execution.
    ResourceFilter resourceFilter = ResourceFilter.fromRuleContext(ruleContext);
    data = data.filter(ruleContext, resourceFilter);
    resourceDeps = resourceDeps.filter(ruleContext, resourceFilter);

    // Now that the LocalResourceContainer has been filtered, we can build a filtered resource
    // container from it.
    ResourceContainer resourceContainer =
        checkForInlinedResources(
            maybeInlinedResourceContainerBuilder
                .setManifest(getManifest())
                .setAssetsAndResourcesFrom(data)
                .build(),
            resourceDeps.getResources(), // TODO(bazel-team): Figure out if we really need to check
            // the ENTIRE transitive closure, or just the direct dependencies. Given that each rule
            // with resources would check for inline resources, we can rely on the previous rule to
            // have checked its dependencies.
            ruleContext);
    if (ruleContext.hasErrors()) {
      return null;
    }

    ResourceContainer processed;
    if (isLibrary) {
      // android_library should only build the APK one way (!incremental).
      Preconditions.checkArgument(!incremental);
      Artifact rJavaClassJar = ruleContext.getImplicitOutputArtifact(
          AndroidRuleClasses.ANDROID_RESOURCES_CLASS_JAR);

      if (resourceContainer.getSymbols() != null) {
        AndroidResourceParsingActionBuilder parsingBuilder =
            new AndroidResourceParsingActionBuilder(ruleContext)
                .withPrimary(resourceContainer)
                .setParse(data)
                .setOutput(resourceContainer.getSymbols())
                .setCompiledSymbolsOutput(resourceContainer.getCompiledSymbols());

        if (dataBindingInfoZip != null && resourceContainer.getCompiledSymbols() != null) {
          PathFragment unusedInfo = dataBindingInfoZip.getRootRelativePath();
          // TODO(corysmith): Centralize the data binding processing and zipping into a single
          // action. Data binding processing needs to be triggered here as well as the merger to
          // avoid aapt2 from throwing an error during compilation.
          parsingBuilder.setDataBindingInfoZip(
              ruleContext.getDerivedArtifact(
                  unusedInfo.replaceName(unusedInfo.getBaseName() + "_unused.zip"),
                  dataBindingInfoZip.getRoot()));
        }
        resourceContainer = parsingBuilder.build(ruleContext);
      }

      AndroidResourceMergingActionBuilder resourcesMergerBuilder =
          new AndroidResourceMergingActionBuilder(ruleContext)
              .setJavaPackage(resourceContainer.getJavaPackage())
              .withPrimary(resourceContainer)
              .withDependencies(resourceDeps)
              .setDataBindingInfoZip(dataBindingInfoZip)
              .setMergedResourcesOut(mergedResources)
              .setManifestOut(manifestOut)
              .setClassJarOut(rJavaClassJar)
              .setDataBindingInfoZip(dataBindingInfoZip)
              .setThrowOnResourceConflict(
                  ruleContext.getConfiguration()
                      .getFragment(AndroidConfiguration.class).throwOnResourceConflict());
      ResourceContainer merged = resourcesMergerBuilder.build(ruleContext);

      processed =
          new AndroidResourceValidatorActionBuilder(ruleContext)
              .setJavaPackage(merged.getJavaPackage())
              .setDebug(ruleContext.getConfiguration().getCompilationMode() != CompilationMode.OPT)
              .setMergedResources(mergedResources)
              .withPrimary(merged)
              .setRTxtOut(merged.getRTxt())
              .setSourceJarOut(merged.getJavaSourceJar())
              .setApkOut(resourceContainer.getApk())
              // aapt2 related artifacts. Will be generated if the targetAaptVersion is AAPT2.
              .withDependencies(resourceDeps)
              .setCompiledSymbols(merged.getCompiledSymbols())
              .setAapt2RTxtOut(merged.getAapt2RTxt())
              .setAapt2SourceJarOut(merged.getAapt2JavaSourceJar())
              .setStaticLibraryOut(merged.getStaticLibrary())
              .build(ruleContext);
    } else {
      AndroidResourcesProcessorBuilder builder =
          new AndroidResourcesProcessorBuilder(ruleContext)
              .setLibrary(isLibrary)
              .setApkOut(resourceContainer.getApk())
              .setResourceFilter(resourceFilter)
              .setUncompressedExtensions(uncompressedExtensions)
              .setCrunchPng(crunchPng)
              .setJavaPackage(resourceContainer.getJavaPackage())
              .setDebug(ruleContext.getConfiguration().getCompilationMode() != CompilationMode.OPT)
              .setManifestOut(manifestOut)
              .setMergedResourcesOut(mergedResources)
              .withPrimary(resourceContainer)
              .withDependencies(resourceDeps)
              .setProguardOut(proguardCfg)
              .setMainDexProguardOut(mainDexProguardCfg)
              .setDataBindingInfoZip(dataBindingInfoZip)
              .setApplicationId(manifestValues.get("applicationId"))
              .setVersionCode(manifestValues.get("versionCode"))
              .setVersionName(manifestValues.get("versionName"))
              .setFeatureOf(featureOf)
              .setFeatureAfter(featureAfter)
              .setThrowOnResourceConflict(
                ruleContext.getConfiguration()
                    .getFragment(AndroidConfiguration.class).throwOnResourceConflict())
              .setPackageUnderTest(packageUnderTest);
      if (!incremental) {
        builder
            .targetAaptVersion(targetAaptVersion)
            .setRTxtOut(resourceContainer.getRTxt())
            .setSymbols(resourceContainer.getSymbols())
            .setSourceJarOut(resourceContainer.getJavaSourceJar());
      }
      processed = builder.build(ruleContext);
    }

    return new ResourceApk(
        resourceContainer.getApk(), processed.getJavaSourceJar(), processed.getJavaClassJar(),
        resourceDeps, processed, processed.getManifest(),
        proguardCfg, mainDexProguardCfg, false);
  }

  private static ResourceContainer checkForInlinedResources(ResourceContainer resourceContainer,
      Iterable<ResourceContainer> resourceContainers, RuleContext ruleContext) {
    // Dealing with Android library projects
    if (Iterables.size(resourceContainers) > 1) {
      if (resourceContainer.getConstantsInlined()
          && !resourceContainer.getArtifacts(ResourceType.RESOURCES).isEmpty()) {
        ruleContext.ruleError("This android binary depends on an android "
            + "library project, so the resources '"
            + AndroidCommon.getAndroidResources(ruleContext).getLabel()
            + "' should have the attribute inline_constants set to 0");
        return null;
      }
    }
    return resourceContainer;
  }

  /** Uses the resource apk from the resources attribute, as opposed to recompiling. */
  public ResourceApk useCurrentResources(
      RuleContext ruleContext, Artifact proguardCfg, @Nullable Artifact mainDexProguardCfg) {
    ResourceContainer resourceContainer = Iterables.getOnlyElement(
        AndroidCommon.getAndroidResources(ruleContext).getDirectAndroidResources());

    new AndroidAaptActionHelper(
        ruleContext,
        resourceContainer.getManifest(),
        Lists.newArrayList(resourceContainer))
        .createGenerateProguardAction(proguardCfg, mainDexProguardCfg);

    return new ResourceApk(
        resourceContainer.getApk(),
        null /* javaSrcJar */,
        null /* javaClassJar */,
        ResourceDependencies.empty(),
        resourceContainer,
        manifest,
        proguardCfg,
        mainDexProguardCfg,
        false);
  }

  /**
   * Packages up the manifest with resources, and generates the R.java.
   *
   * @throws InterruptedException
   * @deprecated in favor of {@link ApplicationManifest#packBinaryWithDataAndResources} and {@link
   *     ApplicationManifest#packLibraryWithDataAndResources}.
   */
  @Deprecated
  public ResourceApk packWithResources(
      Artifact resourceApk,
      RuleContext ruleContext,
      ResourceDependencies resourceDeps,
      boolean createSource,
      Artifact proguardCfg,
      @Nullable Artifact mainDexProguardCfg)
      throws InterruptedException {

    TransitiveInfoCollection resourcesPrerequisite =
        ruleContext.getPrerequisite("resources", Mode.TARGET);
    ResourceContainer resourceContainer = Iterables.getOnlyElement(
        resourcesPrerequisite.getProvider(AndroidResourcesProvider.class)
        .getDirectAndroidResources());
    // It's ugly, but flattening now is more performant given the rest of the checks.
    List<ResourceContainer> resourceContainers =
        ImmutableList.<ResourceContainer>builder()
        //.add(resourceContainer)
        .addAll(resourceDeps.getResources()).build();

    // Dealing with Android library projects
    if (Iterables.size(resourceDeps.getResources()) > 1) {
      if (resourceContainer.getConstantsInlined()
          && !resourceContainer.getArtifacts(ResourceType.RESOURCES).isEmpty()) {
        ruleContext.ruleError("This android_binary depends on an android_library, so the"
            + " resources '" + AndroidCommon.getAndroidResources(ruleContext).getLabel()
            + "' should have the attribute inline_constants set to 0");
        return null;
      }
    }

    // This binary depends on a library project, so we need to regenerate the
    // resources. The resulting sources and apk will combine all the resources
    // contained in the transitive closure of the binary.
    AndroidAaptActionHelper aaptActionHelper = new AndroidAaptActionHelper(ruleContext,
        getManifest(), Lists.newArrayList(resourceContainers));

    ResourceFilter resourceFilter = ResourceFilter.fromRuleContext(ruleContext);

    List<String> uncompressedExtensions =
        ruleContext.getTokenizedStringListAttr("nocompress_extensions");

    ImmutableList.Builder<String> additionalAaptOpts = ImmutableList.builder();

    for (String extension : uncompressedExtensions) {
      additionalAaptOpts.add("-0").add(extension);
    }
    if (resourceFilter.hasConfigurationFilters() && !resourceFilter.isPrefiltering()) {
      additionalAaptOpts.add("-c").add(resourceFilter.getConfigurationFilterString());
    }

    Artifact javaSourcesJar = null;

    if (createSource) {
      javaSourcesJar =
        ruleContext.getImplicitOutputArtifact(AndroidRuleClasses.ANDROID_JAVA_SOURCE_JAR);
      aaptActionHelper.createGenerateResourceSymbolsAction(
          javaSourcesJar, null, resourceContainer.getJavaPackage(), true);
    }

    aaptActionHelper.createGenerateApkAction(
        resourceApk,
        resourceContainer.getRenameManifestPackage(),
        additionalAaptOpts.build(),
        resourceFilter.getDensities());

    ResourceContainer updatedResources = resourceContainer.toBuilder()
        .setLabel(ruleContext.getLabel())
        .setApk(resourceApk)
        .setManifest(getManifest())
        .setJavaSourceJar(javaSourcesJar)
        .setJavaClassJar(null)
        .setSymbols(null)
        .build();

    aaptActionHelper.createGenerateProguardAction(proguardCfg, mainDexProguardCfg);

    return new ResourceApk(resourceApk,
        updatedResources.getJavaSourceJar(),
        updatedResources.getJavaClassJar(),
        resourceDeps, updatedResources, manifest, proguardCfg, mainDexProguardCfg, true);
  }

  public Artifact getManifest() {
    return manifest;
  }
}