aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/tools/android/java/com/google/devtools/build/android/aapt2/StaticLibrary.java
blob: a8b27e9e8c44278c04921f60ad9bf3920fd2d623 (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
// Copyright 2017 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.android.aapt2;

import static com.google.common.collect.ImmutableList.toImmutableList;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import javax.annotation.Nullable;

/** A static library generated by aapt2. */
public class StaticLibrary {

  private final Path library;
  private final Optional<Path> rTxt;
  private final Optional<List<Path>> assets;
  private final Optional<Path> sourceJar;

  private StaticLibrary(
      Path library, Optional<Path> rTxt, Optional<List<Path>> assets, Optional<Path> sourceJar) {
    this.library = library;
    this.rTxt = rTxt;
    this.assets = assets;
    this.sourceJar = sourceJar;
  }

  public static StaticLibrary from(Path library) {
    return of(library, Optional.empty(), Optional.empty(), Optional.empty());
  }

  public static StaticLibrary from(Path library, Path rTxt) {
    return of(library, Optional.of(rTxt), Optional.empty(), Optional.empty());
  }

  private static StaticLibrary of(
      Path library, Optional<Path> rTxt, Optional<List<Path>> assets, Optional<Path> sourceJar) {
    return new StaticLibrary(library, rTxt, assets, sourceJar);
  }

  public static StaticLibrary from(Path library, Path rTxt, ImmutableList<Path> assetDirs) {
    return of(library, Optional.ofNullable(rTxt), Optional.ofNullable(assetDirs), Optional.empty());
  }

  public static StaticLibrary from(
      Path library, Path rTxt, ImmutableList<Path> assetDirs, Path sourceJar) {
    return of(
        library,
        Optional.ofNullable(rTxt),
        Optional.ofNullable(assetDirs),
        Optional.ofNullable(sourceJar));
  }

  public static Collection<String> toPathStrings(List<StaticLibrary> libraries) {
    return Lists.transform(libraries, StaticLibrary::asLibraryPathString);
  }

  public StaticLibrary copyLibraryTo(Path target) throws IOException {
    return of(Files.copy(library, target), rTxt, assets, sourceJar);
  }

  public StaticLibrary copyRTxtTo(@Nullable final Path target) {
    return of(library, rTxt.map(copyTo(target)), assets, sourceJar);
  }

  public StaticLibrary copySourceJarTo(@Nullable final Path target) {
    return of(library, rTxt, assets, sourceJar.map(copyTo(target)));
  }

  private Function<Path, Path> copyTo(Path target) {
    return input -> {
      if (target == null) {
        return input;
      }
      try {
        return Files.copy(input, target);
      } catch (IOException e) {
        throw new RuntimeException(e);
      }
    };
  }

  @VisibleForTesting
  public String asLibraryPathString() {
    return fixForAapt2(library).toString();
  }

  // TODO(b/64140845): aapt2 doesn't recognize .ap_ as a valid library.
  private static Path fixForAapt2(Path library) {
    if (library.getFileName().toString().endsWith(".ap_")) {
      Path fixed = library.resolveSibling(library.getFileName().toString().replace(".ap_", ".apk"));
      // The file has already been "fixed"
      if (Files.exists(fixed)) {
        return fixed;
      }
      try {

        return Files.copy(library, fixed);
      } catch (IOException e) {
        throw new RuntimeException(e);
      }
    }
    return library;
  }

  private List<Path> asAssetPathStrings() {
    return assets.orElse(ImmutableList.of());
  }

  public static Collection<String> toAssetPaths(List<StaticLibrary> libraries) {
    return libraries
        .stream()
        .map(StaticLibrary::asAssetPathStrings)
        .filter(Predicates.isNull())
        .flatMap(List::stream)
        .map(Object::toString)
        .collect(toImmutableList());
  }
}