-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathAapt2ResourcePackagingAction.java
More file actions
463 lines (415 loc) · 19.4 KB
/
Aapt2ResourcePackagingAction.java
File metadata and controls
463 lines (415 loc) · 19.4 KB
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
// 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.android;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.Streams.concat;
import static java.util.stream.Collectors.toList;
import com.android.builder.core.VariantTypeImpl;
import com.android.utils.StdLogger;
import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.devtools.build.android.Converters.AmpersandSplitter;
import com.google.devtools.build.android.Converters.ColonSplitter;
import com.google.devtools.build.android.Converters.CompatDependencyAndroidDataConverter;
import com.google.devtools.build.android.Converters.CompatPathConverter;
import com.google.devtools.build.android.Converters.CompatSerializedAndroidDataConverter;
import com.google.devtools.build.android.Converters.CompatUnvalidatedAndroidDataConverter;
import com.google.devtools.build.android.aapt2.Aapt2ConfigOptions;
import com.google.devtools.build.android.aapt2.CompiledResources;
import com.google.devtools.build.android.aapt2.PackagedResources;
import com.google.devtools.build.android.aapt2.ProtoApk;
import com.google.devtools.build.android.aapt2.ResourceCompiler;
import com.google.devtools.build.android.aapt2.ResourceLinker;
import com.google.devtools.build.android.aapt2.StaticLibrary;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Optional;
/**
* Provides an entry point for the resource processing using the AOSP build tools.
*
* <pre>
* Example Usage:
* java/com/google/build/android/Aapt2ResourcePackagingAction\
* --sdkRoot path/to/sdk\
* --aapt path/to/sdk/aapt\
* --adb path/to/sdk/adb\
* --zipAlign path/to/sdk/zipAlign\
* --androidJar path/to/sdk/androidJar\
* --manifestOutput path/to/manifest\
* --primaryData path/to/resources:path/to/assets:path/to/manifest\
* --data p/t/res1:p/t/assets1:p/t/1/AndroidManifest.xml:p/t/1/R.txt:symbols,\
* p/t/res2:p/t/assets2:p/t/2/AndroidManifest.xml:p/t/2/R.txt:symbols\
* --packagePath path/to/write/archive.ap_\
* --srcJarOutput path/to/write/archive.srcjar
* </pre>
*/
public class Aapt2ResourcePackagingAction {
private static final StdLogger STD_LOGGER = new StdLogger(StdLogger.Level.WARNING);
/** Flag specifications for this action. */
@Parameters(separators = "= ")
public static final class Options {
@Parameter(
names = "--primaryData",
converter = CompatUnvalidatedAndroidDataConverter.class,
description =
"The directory containing the primary resource directory. The contents will override "
+ "the contents of any other resource directories during merging. The expected "
+ "format is "
+ UnvalidatedAndroidData.EXPECTED_FORMAT)
public UnvalidatedAndroidData primaryData;
@Parameter(
names = "--data",
converter = CompatDependencyAndroidDataConverter.class,
description =
"Transitive Data dependencies. These values will be used if not defined in the "
+ "primary resources. The expected format is "
+ DependencyAndroidData.EXPECTED_FORMAT
+ "[,...]")
public List<DependencyAndroidData> transitiveData = ImmutableList.of();
@Parameter(
names = "--directData",
converter = CompatDependencyAndroidDataConverter.class,
description =
"Direct Data dependencies. These values will be used if not defined in the "
+ "primary resources. The expected format is "
+ DependencyAndroidData.EXPECTED_FORMAT
+ "[,...]")
public List<DependencyAndroidData> directData = ImmutableList.of();
@Parameter(
names = "--assets",
converter = CompatSerializedAndroidDataConverter.class,
splitter = AmpersandSplitter.class,
description =
"Transitive asset dependencies. These can also be specified together with resources"
+ " using --data. Expected format: "
+ SerializedAndroidData.EXPECTED_FORMAT
+ "[,...]")
public List<SerializedAndroidData> transitiveAssets = ImmutableList.of();
@Parameter(
names = "--directAssets",
converter = CompatSerializedAndroidDataConverter.class,
splitter = AmpersandSplitter.class,
description =
"Direct asset dependencies. These can also be specified together with resources using "
+ "--directData. Expected format: "
+ SerializedAndroidData.EXPECTED_FORMAT
+ "[,...]")
public List<SerializedAndroidData> directAssets = ImmutableList.of();
@Parameter(
names = "--additionalApksToLinkAgainst",
converter = CompatPathConverter.class,
splitter = ColonSplitter.class,
description = "List of APKs used during linking.")
public List<Path> additionalApksToLinkAgainst = ImmutableList.of();
@Parameter(
names = "--packageId",
description = "Resource ID prefix; see AAPT2 documentation for --package-id.")
public int packageId = -1;
@Parameter(
names = "--rOutput",
converter = CompatPathConverter.class,
description = "Path to where the R.txt should be written.")
public Path rOutput;
@Parameter(
names = {
"--symbolsOut",
"--symbolsTxtOut" // The old name of the flag.
},
converter = CompatPathConverter.class,
description = "Path to where the symbols should be written.")
public Path symbolsOut;
@Parameter(
names = "--dataBindingInfoOut",
converter = CompatPathConverter.class,
description = "Path to where data binding's layout info output should be written.")
public Path dataBindingInfoOut;
@Parameter(
names = "--packagePath",
converter = CompatPathConverter.class,
description = "Path to the write the archive.")
public Path packagePath;
@Parameter(
names = "--resourcesOutput",
converter = CompatPathConverter.class,
description = "Path to the write merged resources archive.")
public Path resourcesOutput;
@Parameter(
names = "--proguardOutput",
converter = CompatPathConverter.class,
description = "Path for the proguard file.")
public Path proguardOutput;
@Parameter(
names = "--mainDexProguardOutput",
converter = CompatPathConverter.class,
description = "Path for the main dex proguard file.")
public Path mainDexProguardOutput;
@Parameter(
names = "--manifestOutput",
converter = CompatPathConverter.class,
description = "Path for the modified manifest.")
public Path manifestOutput;
@Parameter(
names = "--srcJarOutput",
converter = CompatPathConverter.class,
description = "Path for the generated java source jar.")
public Path srcJarOutput;
@Parameter(
names = "--packageType",
description =
"Variant configuration type for packaging the resources."
+ " Acceptable values BASE_APK, LIBRARY, ANDROID_TEST, UNIT_TEST")
public VariantTypeImpl packageType = VariantTypeImpl.BASE_APK;
@Parameter(
names = "--densities",
description = "A list of densities to filter the resource drawables by.")
public List<String> densities = ImmutableList.of();
@Parameter(
names = "--packageForR",
description = "Custom java package to generate the R symbols files.")
public String packageForR;
@Parameter(
names = "--applicationId",
description = "Custom application id (package manifest) for the packaged manifest.")
public String applicationId;
@Parameter(
names = "--versionName",
description = "Version name to stamp into the packaged manifest.")
public String versionName;
@Parameter(
names = "--versionCode",
description = "Version code to stamp into the packaged manifest.")
public int versionCode = -1;
@Parameter(
names = "--throwOnResourceConflict",
arity = 1,
description =
"If passed, resource merge conflicts will be treated as errors instead of warnings")
public boolean throwOnResourceConflict;
@Parameter(names = "--packageUnderTest", arity = 1, description = "Unused/deprecated option.")
public String packageUnderTest;
@Parameter(
names = "--isTestWithResources",
arity = 1,
description = "Unused/deprecated option.")
public boolean isTestWithResources;
@Parameter(
names = "--includeProguardLocationReferences",
arity = 1,
description = "When generating proguard configurations, include location references.")
public boolean includeProguardLocationReferences;
@Parameter(
names = "--resourceApks",
converter = CompatPathConverter.class,
splitter = ColonSplitter.class,
description = "List of reource only APK files to link against.")
public List<Path> resourceApks = ImmutableList.of();
@Option(
name = "legacyResourceOrder",
defaultValue = "false",
category = "config",
documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
effectTags = {OptionEffectTag.UNKNOWN},
help = "If passed, resource merge conflicts will give precedence to value defined last in directData/transitiveData inputs.")
public boolean legacyResourceOrder;
}
public static void main(String[] args) throws Exception {
Profiler profiler = InMemoryProfiler.createAndStart("setup");
Options options = new Options();
Aapt2ConfigOptions aaptConfigOptions = new Aapt2ConfigOptions();
ResourceProcessorCommonOptions resourceProcessorCommonOptions =
new ResourceProcessorCommonOptions();
Object[] allOptions = new Object[] {options, aaptConfigOptions, resourceProcessorCommonOptions};
JCommander jc = new JCommander(allOptions);
String[] preprocessedArgs = AndroidOptionsUtils.runArgFilePreprocessor(jc, args);
String[] normalizedArgs =
AndroidOptionsUtils.normalizeBooleanOptions(allOptions, preprocessedArgs);
jc.parse(normalizedArgs);
Preconditions.checkArgument(
options.packageId == -1 || (options.packageId >= 2 && options.packageId <= 255),
"packageId must be in the range [2,255]");
try (ScopedTemporaryDirectory scopedTmp =
new ScopedTemporaryDirectory("android_resources_tmp");
ExecutorServiceCloser executorService = ExecutorServiceCloser.createWithFixedPoolOf(15)) {
final Path tmp = scopedTmp.getPath();
final Path densityManifest = tmp.resolve("manifest-filtered/AndroidManifest.xml");
final Path processedManifest = tmp.resolve("manifest-processed/AndroidManifest.xml");
final Path symbols = tmp.resolve("symbols/symbols.bin");
final Path databindingResourcesRoot =
Files.createDirectories(tmp.resolve("android_data_binding_resources"));
final Path compiledResources = Files.createDirectories(tmp.resolve("compiled"));
final Path linkedOut = Files.createDirectories(tmp.resolve("linked"));
final AndroidCompiledDataDeserializer dataDeserializer =
AndroidCompiledDataDeserializer.create(/*includeFileContentsForValidation=*/ true);
final ResourceCompiler compiler =
ResourceCompiler.create(
executorService,
compiledResources,
aaptConfigOptions.aapt2,
aaptConfigOptions.buildToolsVersion,
aaptConfigOptions.generatePseudoLocale);
profiler.recordEndOf("setup").startTask("compile");
CompiledResources compiled =
options
.primaryData
.processDataBindings(
options.dataBindingInfoOut,
options.packageForR,
databindingResourcesRoot,
aaptConfigOptions.useDataBindingAndroidX)
.compile(compiler, compiledResources)
.processManifest(
manifest ->
AndroidManifestProcessor.with(STD_LOGGER)
.processManifest(
options.applicationId,
options.versionCode,
options.versionName,
manifest,
processedManifest,
resourceProcessorCommonOptions.logWarnings))
.processManifest(
manifest ->
new DensitySpecificManifestProcessor(options.densities, densityManifest)
.process(manifest));
profiler.recordEndOf("compile").startTask("merge");
// Checks for merge conflicts, and write the merged data out.
final Path symbolsBin =
AndroidResourceMerger.mergeDataToSymbols(
ParsedAndroidData.loadedFrom(
DependencyInfo.DependencyType.PRIMARY,
ImmutableList.of(SerializedAndroidData.from(compiled)),
executorService,
dataDeserializer),
new DensitySpecificManifestProcessor(options.densities, densityManifest)
.process(options.primaryData.getManifest()),
ImmutableList.<SerializedAndroidData>builder()
.addAll(options.directData)
.addAll(options.directAssets)
.build(),
ImmutableList.<SerializedAndroidData>builder()
.addAll(options.transitiveData)
.addAll(options.transitiveAssets)
.build(),
options.packageType,
symbols,
dataDeserializer,
options.throwOnResourceConflict,
executorService);
if (options.symbolsOut != null) {
Files.copy(symbolsBin, options.symbolsOut);
}
profiler.recordEndOf("merge").startTask("link");
// Write manifestOutput now before the dummy manifest is created.
if (options.manifestOutput != null) {
AndroidResourceOutputs.copyManifestToOutput(compiled, options.manifestOutput);
}
List<CompiledResources> compiledResourceDeps;
if (options.legacyResourceOrder) {
// Last defined dependencies will overwrite previous one, so always place direct
// after transitive.
compiledResourceDeps = concat(options.transitiveData.stream(), options.directData.stream())
.map(DependencyAndroidData::getCompiledSymbols)
.collect(toList());
} else {
// In this case, we do not want last defined resource to overwrite previous one.
// This allow, for example, resources defined in primary target to have precendence
// over resources defined in a direct dependency.
compiledResourceDeps = concat(options.directData.stream(), options.transitiveData.stream())
.map(DependencyAndroidData::getCompiledSymbols)
.collect(toList());
Collections.reverse(compiledResourceDeps);
}
// NB: "-A" options are in *decreasing* precedence, while "-R" options are in *increasing*
// precedence. While this is internally inconsistent, it matches AAPTv1's treatment of "-A".
List<Path> assetDirs =
concat(
options.primaryData.assetDirs.stream(),
concat(
options.directData.stream(),
options.directAssets.stream(),
options.transitiveData.stream(),
options.transitiveAssets.stream())
.flatMap(dep -> dep.assetDirs.stream()))
.collect(toList());
List<StaticLibrary> dependencies =
Lists.newArrayList(StaticLibrary.from(aaptConfigOptions.androidJar));
if (options.additionalApksToLinkAgainst != null) {
dependencies.addAll(
options.additionalApksToLinkAgainst.stream()
.map(StaticLibrary::from)
.collect(toList()));
}
ImmutableList<StaticLibrary> resourceApks = ImmutableList.of();
if (options.resourceApks != null) {
resourceApks =
options.resourceApks.stream().map(StaticLibrary::from).collect(toImmutableList());
}
final PackagedResources packagedResources =
ResourceLinker.create(aaptConfigOptions.aapt2, executorService, linkedOut)
.profileUsing(profiler)
.customPackage(options.packageForR)
.packageId(
options.packageId != -1 ? Optional.of(options.packageId) : Optional.empty())
.outputAsProto(aaptConfigOptions.resourceTableAsProto)
.dependencies(ImmutableList.copyOf(dependencies))
.resourceApks(resourceApks)
.include(compiledResourceDeps)
.withAssets(assetDirs)
.buildVersion(aaptConfigOptions.buildToolsVersion)
.conditionalKeepRules(aaptConfigOptions.conditionalKeepRules == TriState.YES)
.filterToDensity(options.densities)
.storeUncompressed(aaptConfigOptions.uncompressedExtensions)
.debug(aaptConfigOptions.debug)
.includeGeneratedLocales(aaptConfigOptions.generatePseudoLocale)
.includeOnlyConfigs(aaptConfigOptions.resourceConfigs)
.includeProguardLocationReferences(options.includeProguardLocationReferences)
.link(compiled);
profiler.recordEndOf("link").startTask("validate");
ValidateAndLinkResourcesAction.checkVisibilityOfResourceReferences(
ProtoApk.readFrom(packagedResources.proto()).getManifest(),
compiled,
compiledResourceDeps);
profiler.recordEndOf("validate");
if (options.packagePath != null) {
copy(packagedResources.apk(), options.packagePath);
}
if (options.proguardOutput != null) {
copy(packagedResources.proguardConfig(), options.proguardOutput);
}
if (options.mainDexProguardOutput != null) {
copy(packagedResources.mainDexProguard(), options.mainDexProguardOutput);
}
if (options.srcJarOutput != null) {
AndroidResourceOutputs.createSrcJar(
packagedResources.javaSourceDirectory(), options.srcJarOutput, /* staticIds= */ false);
}
if (options.rOutput != null) {
copy(packagedResources.rTxt(), options.rOutput);
}
if (options.resourcesOutput != null) {
packagedResources.asArchive().writeTo(options.resourcesOutput, /* compress= */ false);
}
}
}
private static void copy(Path from, Path out) throws IOException {
Files.createDirectories(out.getParent());
Files.copy(from, out);
}
}