Skip to content

Commit

Permalink
Prepare for release 1.15.1.
Browse files Browse the repository at this point in the history
  • Loading branch information
sopo-c committed May 23, 2023
1 parent 65ca3b5 commit f17ce94
Show file tree
Hide file tree
Showing 17 changed files with 555 additions and 56 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ https://developer.android.com/studio/command-line/bundletool

## Releases

Latest release: [1.15.0](https://github.com/google/bundletool/releases)
Latest release: [1.15.1](https://github.com/google/bundletool/releases)
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
release_version = 1.15.0
release_version = 1.15.1
Original file line number Diff line number Diff line change
Expand Up @@ -1008,13 +1008,7 @@ private ImmutableMap<String, BundleModule> getValidatedSdkModules(
getValidatedSdkBundlesByPackageName(closer, tempDir);
validateSdkBundlesMatchAppBundleDependencies(appBundle, sdkBundles);
return sdkBundles.entrySet().stream()
.collect(
toImmutableMap(
Entry::getKey,
entry ->
entry.getValue().getModule().toBuilder()
.setSdkModulesConfig(entry.getValue().getSdkModulesConfig())
.build()));
.collect(toImmutableMap(Entry::getKey, entry -> entry.getValue().getModule()));
}

private ImmutableMap<String, SdkAsar> getValidatedSdkAsarsByPackageName(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static com.android.tools.build.bundletool.model.utils.files.FilePreconditions.checkFileExistsAndReadable;
import static com.android.tools.build.bundletool.model.utils.files.FilePreconditions.checkFileHasExtension;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;

import com.android.bundle.RuntimeEnabledSdkConfigProto.SdkSplitPropertiesInheritedFromApp;
import com.android.tools.build.bundletool.androidtools.Aapt2Command;
Expand All @@ -32,6 +33,7 @@
import com.android.tools.build.bundletool.model.BundleModule;
import com.android.tools.build.bundletool.model.Password;
import com.android.tools.build.bundletool.model.SdkAsar;
import com.android.tools.build.bundletool.model.SdkBundle;
import com.android.tools.build.bundletool.model.SignerConfig;
import com.android.tools.build.bundletool.model.SigningConfiguration;
import com.android.tools.build.bundletool.model.exceptions.CommandExecutionException;
Expand All @@ -41,6 +43,7 @@
import com.android.tools.build.bundletool.model.utils.files.BufferedIo;
import com.android.tools.build.bundletool.sdkmodule.SdkModuleToAppBundleModuleConverter;
import com.android.tools.build.bundletool.validation.SdkAsarValidator;
import com.android.tools.build.bundletool.validation.SdkBundleValidator;
import com.google.auto.value.AutoValue;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
Expand All @@ -64,7 +67,10 @@ public abstract class BuildSdkApksForAppCommand {

public static final String COMMAND_NAME = "build-sdk-apks-for-app";

private static final Flag<Path> SDK_BUNDLE_LOCATION_FLAG = Flag.path("sdk-bundle");

private static final Flag<Path> SDK_ARCHIVE_LOCATION_FLAG = Flag.path("sdk-archive");

private static final Flag<Path> INHERITED_APP_PROPERTIES_LOCATION_FLAG =
Flag.path("app-properties");

Expand All @@ -81,7 +87,9 @@ public abstract class BuildSdkApksForAppCommand {
private static final SystemEnvironmentProvider DEFAULT_PROVIDER =
new DefaultSystemEnvironmentProvider();

public abstract Path getSdkArchivePath();
public abstract Optional<Path> getSdkBundlePath();

public abstract Optional<Path> getSdkArchivePath();

public abstract SdkSplitPropertiesInheritedFromApp getInheritedAppProperties();

Expand All @@ -107,6 +115,9 @@ public static BuildSdkApksForAppCommand.Builder builder() {
@AutoValue.Builder
public abstract static class Builder {

/** Sets the path to the SDK bundle file. Must have the extension ".asb". */
public abstract Builder setSdkBundlePath(Path sdkBundlePath);

/** Sets the path to the SDK archive file. Must have the extension ".asar". */
public abstract Builder setSdkArchivePath(Path sdkArchivePath);

Expand Down Expand Up @@ -160,19 +171,33 @@ public BuildSdkApksForAppCommand build() {
setExecutorServiceInternal(createInternalExecutorService(DEFAULT_THREAD_POOL_SIZE));
setExecutorServiceCreatedByBundleTool(true);
}
return autoBuild();
BuildSdkApksForAppCommand command = autoBuild();
checkState(
command.getSdkBundlePath().isPresent() ^ command.getSdkArchivePath().isPresent(),
"One and only one of SdkBundlePath and SdkArchivePath should be set.");
return command;
}
}

public static CommandHelp help() {
return CommandHelp.builder()
.setCommandName(COMMAND_NAME)
.setCommandDescription(CommandDescription.builder().setShortDescription("").build())
.addFlag(
FlagDescription.builder()
.setFlagName(SDK_BUNDLE_LOCATION_FLAG.getName())
.setExampleValue("sdk.asb")
.setDescription(
"Path to SDK bundle to generate app-specific split APKs from. Can"
+ " not be used together with the `sdk-archive` flag.")
.build())
.addFlag(
FlagDescription.builder()
.setFlagName(SDK_ARCHIVE_LOCATION_FLAG.getName())
.setExampleValue("sdk.asar")
.setDescription("Path to SDK archive to generate app-specific split APKs from.")
.setDescription(
"Path to SDK archive to generate app-specific split APKs from. Can"
+ " not be used together with the `sdk-bundle` flag.")
.build())
.addFlag(
FlagDescription.builder()
Expand Down Expand Up @@ -250,11 +275,11 @@ static BuildSdkApksForAppCommand fromFlags(
ParsedFlags flags, PrintStream out, SystemEnvironmentProvider systemEnvironmentProvider) {
BuildSdkApksForAppCommand.Builder command =
BuildSdkApksForAppCommand.builder()
.setSdkArchivePath(SDK_ARCHIVE_LOCATION_FLAG.getRequiredValue(flags))
.setInheritedAppProperties(
INHERITED_APP_PROPERTIES_LOCATION_FLAG.getRequiredValue(flags))
.setOutputFile(OUTPUT_FILE_FLAG.getRequiredValue(flags));

SDK_BUNDLE_LOCATION_FLAG.getValue(flags).ifPresent(command::setSdkBundlePath);
SDK_ARCHIVE_LOCATION_FLAG.getValue(flags).ifPresent(command::setSdkArchivePath);
AAPT2_PATH_FLAG
.getValue(flags)
.ifPresent(
Expand All @@ -267,14 +292,58 @@ static BuildSdkApksForAppCommand fromFlags(

public void execute() {
validateInput();
if (getSdkBundlePath().isPresent()) {
executeForSdkBundle();
} else if (getSdkArchivePath().isPresent()) {
executeForSdkArchive();
} else {
throw new IllegalStateException("whaaat");
}
}

private void validateInput() {
if (getSdkBundlePath().isPresent()) {
checkFileExistsAndReadable(getSdkBundlePath().get());
checkFileHasExtension("ASB file", getSdkBundlePath().get(), ".asb");
}
if (getSdkArchivePath().isPresent()) {
checkFileExistsAndReadable(getSdkArchivePath().get());
checkFileHasExtension("ASAR file", getSdkArchivePath().get(), ".asar");
}
}

private void executeForSdkBundle() {
try (TempDirectory tempDir = new TempDirectory(getClass().getSimpleName());
ZipFile sdkBundleZip = new ZipFile(getSdkBundlePath().get().toFile())) {
Path modulesPath = tempDir.getPath().resolve(EXTRACTED_SDK_MODULES_FILE_NAME);
try (ZipFile modulesZip = getModulesZip(sdkBundleZip, modulesPath)) {
SdkBundleValidator sdkBundleValidator = SdkBundleValidator.create();
sdkBundleValidator.validateModulesFile(modulesZip);
// SdkBundle#getVersionCode is not used in `build-apks`. It does not matter what
// value we set here, so we are just setting 0.
SdkBundle sdkBundle =
SdkBundle.buildFromZip(sdkBundleZip, modulesZip, /* versionCode= */ 0);
sdkBundleValidator.validate(sdkBundle);
generateAppApks(sdkBundle.getModule(), tempDir);
}
} catch (ZipException e) {
throw CommandExecutionException.builder()
.withInternalMessage("ASB is not a valid zip file.")
.withCause(e)
.build();
} catch (IOException e) {
throw new UncheckedIOException("An error occurred when processing the SDK bundle.", e);
}
}

private void executeForSdkArchive() {
try (TempDirectory tempDir = new TempDirectory(getClass().getSimpleName());
ZipFile asarZip = new ZipFile(getSdkArchivePath().toFile())) {
ZipFile asarZip = new ZipFile(getSdkArchivePath().get().toFile())) {
Path modulesPath = tempDir.getPath().resolve(EXTRACTED_SDK_MODULES_FILE_NAME);
try (ZipFile modulesZip = getModulesZip(asarZip, modulesPath)) {
SdkAsarValidator.validateModulesFile(modulesZip);
SdkAsar sdkAsar = SdkAsar.buildFromZip(asarZip, modulesZip, modulesPath);
generateAppApks(sdkAsar, tempDir);
generateAppApks(sdkAsar.getModule(), tempDir);
}
} catch (ZipException e) {
throw CommandExecutionException.builder()
Expand All @@ -286,15 +355,9 @@ public void execute() {
}
}

private void validateInput() {
checkFileExistsAndReadable(getSdkArchivePath());
checkFileHasExtension("ASAR file", getSdkArchivePath(), ".asar");
}

private void generateAppApks(SdkAsar sdkAsar, TempDirectory tempDirectory) {
private void generateAppApks(BundleModule sdkModule, TempDirectory tempDirectory) {
BundleModule convertedAppModule =
new SdkModuleToAppBundleModuleConverter(sdkAsar.getModule(), getInheritedAppProperties())
.convert();
new SdkModuleToAppBundleModuleConverter(sdkModule, getInheritedAppProperties()).convert();
DaggerBuildSdkApksForAppManagerComponent.builder()
.setBuildSdkApksForAppCommand(this)
.setModule(convertedAppModule)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@
import com.android.tools.build.bundletool.flags.Flag;
import com.android.tools.build.bundletool.flags.ParsedFlags;
import com.android.tools.build.bundletool.io.ZipFlingerBundleSerializer;
import com.android.tools.build.bundletool.model.AndroidManifest;
import com.android.tools.build.bundletool.model.BundleMetadata;
import com.android.tools.build.bundletool.model.BundleModule;
import com.android.tools.build.bundletool.model.ManifestEditor;
import com.android.tools.build.bundletool.model.SdkBundle;
import com.android.tools.build.bundletool.model.ZipPath;
import com.android.tools.build.bundletool.model.exceptions.CommandExecutionException;
Expand Down Expand Up @@ -224,6 +226,7 @@ public void execute() {
.map(
moduleZipPath ->
BundleModuleParser.parseSdkBundleModule(moduleZipPath, sdkModulesConfig))
.map(BuildSdkBundleCommand::sanitizeManifest)
.collect(toImmutableList());

new SdkBundleModulesValidator().validateSdkBundleModules(modules);
Expand Down Expand Up @@ -324,6 +327,17 @@ private static <T extends Message.Builder> void populateConfigFromJson(
}
}

private static BundleModule sanitizeManifest(BundleModule bundleModule) {
return bundleModule.toBuilder()
.setAndroidManifest(sanitizeManifest(bundleModule.getAndroidManifest()))
.build();
}

private static AndroidManifest sanitizeManifest(AndroidManifest androidManifest) {
return androidManifest.applyMutators(
ImmutableList.of(ManifestEditor::removePermissions, ManifestEditor::removeComponents));
}

public static CommandHelp help() {
return CommandHelp.builder()
.setCommandName(COMMAND_NAME)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public static AppBundle buildFromZip(ZipFile bundleFile) {
bundleConfig.getType(),
Version.of(bundleConfig.getBundletool().getVersion()),
apexConfig,
/* sdkModulesConfig= */ Optional.empty(),
NON_MODULE_DIRECTORIES)),
bundleConfig,
readBundleMetadata(bundleFile));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,10 @@ public AndroidManifest getAndroidManifest() {

public abstract Optional<RuntimeEnabledSdkConfig> getRuntimeEnabledSdkConfig();

/** Only present for modules of type SDK_DEPENDENCY_MODULE. */
/**
* Only present for modules of type SDK_DEPENDENCY_MODULE in app bundles, as well as in SDK
* bundles and ASARs.
*/
public abstract Optional<SdkModulesConfig> getSdkModulesConfig();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@
import static com.android.tools.build.bundletool.model.AndroidManifest.NAME_ATTRIBUTE_NAME;
import static com.android.tools.build.bundletool.model.AndroidManifest.NAME_RESOURCE_ID;
import static com.android.tools.build.bundletool.model.AndroidManifest.NO_NAMESPACE_URI;
import static com.android.tools.build.bundletool.model.AndroidManifest.PERMISSION_ELEMENT_NAME;
import static com.android.tools.build.bundletool.model.AndroidManifest.PROPERTY_ELEMENT_NAME;
import static com.android.tools.build.bundletool.model.AndroidManifest.PROVIDER_ELEMENT_NAME;
import static com.android.tools.build.bundletool.model.AndroidManifest.RECEIVER_ELEMENT_NAME;
import static com.android.tools.build.bundletool.model.AndroidManifest.REMOVABLE_ELEMENT_NAME;
import static com.android.tools.build.bundletool.model.AndroidManifest.REQUIRED_ATTRIBUTE_NAME;
import static com.android.tools.build.bundletool.model.AndroidManifest.REQUIRED_BY_PRIVACY_SANDBOX_SDK_ATTRIBUTE_NAME;
Expand Down Expand Up @@ -741,6 +743,38 @@ private boolean hasRequiredByPrivacySandboxSdkAttr(XmlProtoElementBuilder elemen
.isPresent();
}

/** Removes custom permissions from the manifest. */
@CanIgnoreReturnValue
public ManifestEditor removePermissions() {
manifestElement.removeChildrenElementsIf(
childElement ->
childElement.isElement()
&& childElement.getElement().getName().equals(PERMISSION_ELEMENT_NAME));
return this;
}

/** Removes any components from the application element. */
@CanIgnoreReturnValue
public ManifestEditor removeComponents() {
manifestElement
.getOptionalChildElement(APPLICATION_ELEMENT_NAME)
.ifPresent(this::removeComponents);
return this;
}

private void removeComponents(XmlProtoElementBuilder element) {
ImmutableSet<String> componentNames =
ImmutableSet.of(
ACTIVITY_ELEMENT_NAME,
SERVICE_ELEMENT_NAME,
PROVIDER_ELEMENT_NAME,
RECEIVER_ELEMENT_NAME);
element.removeChildrenElementsIf(
childElement ->
childElement.isElement()
&& componentNames.contains(childElement.getElement().getName()));
}

/** Generates the modified manifest. */
@CheckReturnValue
public AndroidManifest save() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,14 @@ public static SdkAsar buildFromZip(ZipFile asar, ZipFile modulesFile, Path modul
SdkModulesConfig sdkModulesConfig = readSdkModulesConfig(modulesFile);
BundleModule sdkModule =
Iterables.getOnlyElement(
sanitize(
extractModules(
modulesFile,
BundleType.REGULAR,
Version.of(sdkModulesConfig.getBundletool().getVersion()),
/* apexConfig= */ Optional.empty(),
/* nonModuleDirectories= */ ImmutableSet.of())))
.toBuilder()
.setSdkModulesConfig(sdkModulesConfig)
.build();
sanitize(
extractModules(
modulesFile,
BundleType.REGULAR,
Version.of(sdkModulesConfig.getBundletool().getVersion()),
/* apexConfig= */ Optional.empty(),
Optional.of(sdkModulesConfig),
/* nonModuleDirectories= */ ImmutableSet.of())));
SdkAsar.Builder sdkAsarBuilder =
builder()
.setModule(sdkModule)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public static SdkBundle buildFromZip(
BundleType.REGULAR,
Version.of(sdkModulesConfig.getBundletool().getVersion()),
/* apexConfig= */ Optional.empty(),
Optional.of(sdkModulesConfig),
/* nonModuleDirectories= */ ImmutableSet.of()))
.get(0))
.setSdkModulesConfig(sdkModulesConfig)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public static ImmutableList<BundleModule> extractModules(
BundleType bundleType,
Version bundletoolVersion,
Optional<ApexConfig> apexConfig,
Optional<SdkModulesConfig> sdkModulesConfig,
ImmutableSet<ZipPath> nonModuleDirectories) {
Map<BundleModuleName, BundleModule.Builder> moduleBuilders = new HashMap<>();
Enumeration<? extends ZipEntry> entries = bundleFile.entries();
Expand All @@ -136,6 +137,7 @@ public static ImmutableList<BundleModule> extractModules(
.setBundleType(bundleType)
.setBundletoolVersion(bundletoolVersion);
apexConfig.ifPresent(bundleModuleBuilder::setBundleApexConfig);
sdkModulesConfig.ifPresent(bundleModuleBuilder::setSdkModulesConfig);
return bundleModuleBuilder;
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*/
public final class BundleToolVersion {

private static final String CURRENT_VERSION = "1.15.0";
private static final String CURRENT_VERSION = "1.15.1";

/** Returns the version of BundleTool being run. */
public static Version getCurrentVersion() {
Expand Down
Loading

0 comments on commit f17ce94

Please sign in to comment.