Search in sources :

Example 71 with ZipPath

use of com.android.tools.build.bundletool.model.ZipPath in project bundletool by google.

the class TargetingGenerator method generateTargetingForAssets.

/**
 * Processes given asset directories, generating targeting based on their names.
 *
 * @param assetDirectories Names of directories under assets/, including the "assets/" prefix.
 * @return Targeting for the given asset directories.
 */
public Assets generateTargetingForAssets(Collection<ZipPath> assetDirectories) {
    for (ZipPath directory : assetDirectories) {
        // Extra '/' to handle special case when the directory is just "assets".
        checkRootDirectoryName(ASSETS_DIR, directory + "/");
    }
    // Stores all different targeting values for a given set of sibling targeted directories.
    // Key: targeted directory base name {@link TargetedDirectory#getPathBaseName}
    // Values: {@link AssetsDirectoryTargeting} targeting expressed as value for each sibling.
    HashMultimap<String, AssetsDirectoryTargeting> targetingByBaseName = HashMultimap.create();
    for (ZipPath assetDirectory : FileUtils.toPathWalkingOrder(assetDirectories)) {
        TargetedDirectory targetedDirectory = TargetedDirectory.parse(assetDirectory);
        targetingByBaseName.put(targetedDirectory.getPathBaseName(), targetedDirectory.getLastSegment().getTargeting());
    }
    validateDimensions(targetingByBaseName);
    // Pass 2: Building the directory targeting proto using the targetingByBaseName map.
    Assets.Builder assetsBuilder = Assets.newBuilder();
    for (ZipPath assetDirectory : assetDirectories) {
        AssetsDirectoryTargeting.Builder targeting = AssetsDirectoryTargeting.newBuilder();
        TargetedDirectory targetedDirectory = TargetedDirectory.parse(assetDirectory);
        // We will calculate targeting of each path segment and merge them together.
        for (int i = 0; i < targetedDirectory.getPathSegments().size(); i++) {
            TargetedDirectorySegment segment = targetedDirectory.getPathSegments().get(i);
            // Set targeting values.
            targeting.mergeFrom(segment.getTargeting());
            // Set targeting alternatives.
            if (segment.getTargeting().hasLanguage()) {
                // identical language across resources and assets.
                continue;
            }
            targeting.mergeFrom(// Remove oneself from the alternatives and merge them together.
            Sets.difference(targetingByBaseName.get(targetedDirectory.getSubPathBaseName(i)), ImmutableSet.of(segment.getTargeting())).stream().map(TargetingProtoUtils::toAlternativeTargeting).reduce(AssetsDirectoryTargeting.newBuilder(), (builder, targetingValue) -> builder.mergeFrom(targetingValue), (builderA, builderB) -> builderA.mergeFrom(builderB.build())).build());
        }
        assetsBuilder.addDirectory(TargetedAssetsDirectory.newBuilder().setPath(assetDirectory.toString()).setTargeting(targeting));
    }
    return assetsBuilder.build();
}
Also used : AssetsDirectoryTargeting(com.android.bundle.Targeting.AssetsDirectoryTargeting) Assets(com.android.bundle.Files.Assets) ZipPath(com.android.tools.build.bundletool.model.ZipPath)

Example 72 with ZipPath

use of com.android.tools.build.bundletool.model.ZipPath in project bundletool by google.

the class SplitsXmlInjector method injectSplitsXml.

private static ModuleSplit injectSplitsXml(ModuleSplit split, XmlNode xmlNode) {
    ZipPath resourcePath = getUniqueResourcePath(split);
    ResourceInjector resourceInjector = ResourceInjector.fromModuleSplit(split);
    ResourceId resourceId = resourceInjector.addResource(XML_TYPE_NAME, createXmlEntry(resourcePath));
    return split.toBuilder().setResourceTable(resourceInjector.build()).setEntries(ImmutableList.<ModuleEntry>builder().addAll(split.getEntries()).add(ModuleEntry.builder().setPath(resourcePath).setContent(ByteSource.wrap(xmlNode.toByteArray())).build()).build()).setAndroidManifest(split.getAndroidManifest().toEditor().addMetaDataResourceId(METADATA_KEY, resourceId.getFullResourceId()).save()).build();
}
Also used : ResourceId(com.android.tools.build.bundletool.model.ResourceId) ModuleEntry(com.android.tools.build.bundletool.model.ModuleEntry) ZipPath(com.android.tools.build.bundletool.model.ZipPath) ResourceInjector(com.android.tools.build.bundletool.model.ResourceInjector)

Example 73 with ZipPath

use of com.android.tools.build.bundletool.model.ZipPath in project bundletool by google.

the class FileUtils method getFileExtension.

/**
 * Gets the extension of the path file.
 */
public static String getFileExtension(ZipPath path) {
    if (path.getNameCount() == 0) {
        return "";
    }
    ZipPath name = path.getFileName();
    // null for empty paths and root-only paths
    if (name == null) {
        return "";
    }
    String fileName = name.toString();
    int dotIndex = fileName.lastIndexOf('.');
    return dotIndex == -1 ? "" : fileName.substring(dotIndex + 1);
}
Also used : ZipPath(com.android.tools.build.bundletool.model.ZipPath)

Example 74 with ZipPath

use of com.android.tools.build.bundletool.model.ZipPath in project bundletool by google.

the class ResultUtilsTest method createInstantVariant.

private Variant createInstantVariant() {
    ZipPath apkLBase = ZipPath.create("instant/apkL-base.apk");
    ZipPath apkLFeature = ZipPath.create("instant/apkL-feature.apk");
    ZipPath apkLOther = ZipPath.create("instant/apkL-other.apk");
    return createVariant(variantSdkTargeting(sdkVersionFrom(21), ImmutableSet.of(SdkVersion.getDefaultInstance())), createInstantApkSet("base", ApkTargeting.getDefaultInstance(), apkLBase), createInstantApkSet("feature", ApkTargeting.getDefaultInstance(), apkLFeature), createInstantApkSet("other", ApkTargeting.getDefaultInstance(), apkLOther));
}
Also used : ZipPath(com.android.tools.build.bundletool.model.ZipPath)

Example 75 with ZipPath

use of com.android.tools.build.bundletool.model.ZipPath in project bundletool by google.

the class ResultUtilsTest method createSplitVariant.

private Variant createSplitVariant() {
    ZipPath apkL = ZipPath.create("splits/apkL.apk");
    ZipPath apkLx86 = ZipPath.create("splits/apkL-x86.apk");
    return createVariant(variantSdkTargeting(sdkVersionFrom(21)), createSplitApkSet("base", createMasterApkDescription(ApkTargeting.getDefaultInstance(), apkL), createApkDescription(apkAbiTargeting(AbiAlias.X86, ImmutableSet.of()), apkLx86, /* isMasterSplit= */
    false)));
}
Also used : ZipPath(com.android.tools.build.bundletool.model.ZipPath)

Aggregations

ZipPath (com.android.tools.build.bundletool.model.ZipPath)194 Test (org.junit.Test)154 BuildApksResult (com.android.bundle.Commands.BuildApksResult)106 Path (java.nio.file.Path)55 DeviceSpec (com.android.bundle.Devices.DeviceSpec)44 InvalidBundleException (com.android.tools.build.bundletool.model.exceptions.InvalidBundleException)23 ImmutableSet (com.google.common.collect.ImmutableSet)23 ModuleEntry (com.android.tools.build.bundletool.model.ModuleEntry)19 Variant (com.android.bundle.Commands.Variant)16 ApksArchiveHelpers.createVariant (com.android.tools.build.bundletool.testing.ApksArchiveHelpers.createVariant)15 BundleModule (com.android.tools.build.bundletool.model.BundleModule)13 ImmutableList (com.google.common.collect.ImmutableList)12 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)11 ApksArchiveHelpers.standaloneVariant (com.android.tools.build.bundletool.testing.ApksArchiveHelpers.standaloneVariant)10 Theory (org.junit.experimental.theories.Theory)10 AdbServer (com.android.tools.build.bundletool.device.AdbServer)9 IOException (java.io.IOException)9 ModuleSplit (com.android.tools.build.bundletool.model.ModuleSplit)8 ImmutableSet.toImmutableSet (com.google.common.collect.ImmutableSet.toImmutableSet)8 UncheckedIOException (java.io.UncheckedIOException)8