Assets.Builder assetsBuilder = Assets.newBuilder();
androidManifest("com.test.app", withSplitId("moduleX"))) .addFileWithProtoContent( ZipPath.create("moduleX/assets.pb"), Assets.getDefaultInstance()) .addFileWithProtoContent( ZipPath.create("moduleX/native.pb"), NativeLibraries.getDefaultInstance())
private ImmutableList<ModuleSplit> splitAssetsDirectories(Assets assets, ModuleSplit split) { Multimap<T, TargetedAssetsDirectory> directoriesMap = Multimaps.index( assets.getDirectoryList(), targetedDirectory -> dimensionGetter.apply(targetedDirectory.getTargeting())); return directoriesMap.asMap().entrySet().stream() .map( entry -> { ModuleSplit.Builder modifiedSplit = split.toBuilder(); boolean isMasterSplit = split.isMasterSplit() && isDefaultTargeting(entry.getKey()); modifiedSplit .setEntries(findEntriesInDirectories(entry.getValue(), split)) .setApkTargeting(generateTargeting(split.getApkTargeting(), entry.getKey())) .setMasterSplit(isMasterSplit); if (!isMasterSplit) { modifiedSplit.addMasterManifestMutator(withSplitsRequired(true)); } return modifiedSplit.build(); }) .filter(moduleSplit -> !moduleSplit.getEntries().isEmpty()) .collect(toImmutableList()); }
.ignoringRepeatedFieldOrder() .isEqualTo( Assets.newBuilder() .addDirectory( TargetedAssetsDirectory.newBuilder()
.ignoringRepeatedFieldOrder() .isEqualTo( Assets.newBuilder() .addDirectory( TargetedAssetsDirectory.newBuilder()
.ignoringRepeatedFieldOrder() .isEqualTo( Assets.newBuilder() .addDirectory( TargetedAssetsDirectory.newBuilder()
.ignoringRepeatedFieldOrder() .isEqualTo( Assets.newBuilder() .addDirectory( TargetedAssetsDirectory.newBuilder()
.ignoringRepeatedFieldOrder() .isEqualTo( Assets.newBuilder() .addDirectory( TargetedAssetsDirectory.newBuilder()
XmlNode manifest = androidManifest(PKG_NAME, withHasCode(true)); Assets assetsConfig = Assets.newBuilder() .addDirectory( TargetedAssetsDirectory.newBuilder() .hasFile("base/manifest/AndroidManifest.xml") .withContent(manifest.toByteArray()); assertThat(bundle).hasFile("base/assets.pb").withContent(assetsConfig.toByteArray());
@Test public void testSplitsDontHaveBundleConfigFiles() throws Exception { BundleModule bundleModule = new BundleModuleBuilder("testModule") .addFile("assets/dict.dat") .setManifest(androidManifest("com.test.app")) .setNativeConfig(NativeLibraries.getDefaultInstance()) .setAssetsConfig(Assets.getDefaultInstance()) .build(); ImmutableList<ModuleSplit> splits = createAbiAndDensitySplitter(bundleModule).splitModule(); assertThat(splits.stream().map(ModuleSplit::getSplitType).distinct().collect(toImmutableSet())) .containsExactly(SplitType.SPLIT); assertThat( splits .stream() .map(ModuleSplit::getVariantTargeting) .distinct() .collect(toImmutableSet())) .containsExactly(lPlusVariantTargeting()); assertThat(splits).isNotEmpty(); for (ModuleSplit split : splits) { ImmutableSet<ZipPath> pathEntries = split.getEntries().stream().map(ModuleEntry::getPath).collect(toImmutableSet()); assertThat(pathEntries).doesNotContain(ZipPath.create("native.pb")); assertThat(pathEntries).doesNotContain(ZipPath.create("assets.pb")); } }
@Test public void generateTargetingForAssets_different_types_leaves_ok() throws Exception { Assets assetsConfig = new TargetingGenerator() .generateTargetingForAssets( ImmutableList.of( ZipPath.create("assets/world/texture#tcf_etc1"), ZipPath.create("assets/world/alternative/texture#opengl_3.0"))); assertThat(assetsConfig) .ignoringRepeatedFieldOrder() .isEqualTo( Assets.newBuilder() .addDirectory( TargetedAssetsDirectory.newBuilder() .setPath("assets/world/texture#tcf_etc1") .setTargeting( assetsDirectoryTargeting( textureCompressionTargeting( TextureCompressionFormatAlias.ETC1_RGB8)))) .addDirectory( TargetedAssetsDirectory.newBuilder() .setPath("assets/world/alternative/texture#opengl_3.0") .setTargeting( assetsDirectoryTargeting(graphicsApiTargeting(openGlVersionFrom(3))))) .build()); }
@Test public void differentAssetsConfig_ok() throws Exception { String filePath = "assets.pb"; byte[] fileContentA = Assets.getDefaultInstance().toByteArray(); byte[] fileContentB = Assets.newBuilder() .addDirectory(TargetedAssetsDirectory.getDefaultInstance()) .build() .toByteArray(); assertThat(fileContentA).isNotEqualTo(fileContentB); BundleModule moduleA = new BundleModuleBuilder("a") .addFile(filePath, fileContentA) .setManifest(androidManifest("com.test.app")) .build(); BundleModule moduleB = new BundleModuleBuilder("b") .addFile(filePath, fileContentB) .setManifest(androidManifest("com.test.app")) .build(); new EntryClashValidator().validateAllModules(ImmutableList.of(moduleA, moduleB)); }
@Test public void specialFiles_areNotStoredAsEntries() throws Exception { BundleModule bundleModule = BundleModule.builder() .setName(BundleModuleName.create("testModule")) .setBundleConfig(DEFAULT_BUNDLE_CONFIG) .addEntry( InMemoryModuleEntry.ofFile( "manifest/AndroidManifest.xml", androidManifest("com.test.app").toByteArray())) .addEntry( InMemoryModuleEntry.ofFile("assets.pb", Assets.getDefaultInstance().toByteArray())) .addEntry( InMemoryModuleEntry.ofFile( "native.pb", NativeLibraries.getDefaultInstance().toByteArray())) .addEntry( InMemoryModuleEntry.ofFile( "resources.pb", ResourceTable.getDefaultInstance().toByteArray())) .build(); assertThat(bundleModule.getEntries()).isEmpty(); }
@Test public void generateTargetingForAssets_nonTargetedDirectories() throws Exception { Assets assetsConfig = new TargetingGenerator() .generateTargetingForAssets( ImmutableList.of( ZipPath.create("assets/static"), ZipPath.create("assets/world/static"), ZipPath.create("assets/world/static-alt"))); assertThat(assetsConfig) .ignoringRepeatedFieldOrder() .isEqualTo( Assets.newBuilder() .addDirectory( TargetedAssetsDirectory.newBuilder() .setPath("assets/static") .setTargeting(AssetsDirectoryTargeting.getDefaultInstance())) .addDirectory( TargetedAssetsDirectory.newBuilder() .setPath("assets/world/static") .setTargeting(AssetsDirectoryTargeting.getDefaultInstance())) .addDirectory( TargetedAssetsDirectory.newBuilder() .setPath("assets/world/static-alt") .setTargeting(AssetsDirectoryTargeting.getDefaultInstance())) .build()); }
@Test public void generateTargetingForAssets_assetsAtTopLevel() throws Exception { Assets assetsConfig = new TargetingGenerator() .generateTargetingForAssets(ImmutableList.of(ZipPath.create("assets"))); assertThat(assetsConfig) .ignoringRepeatedFieldOrder() .isEqualTo( Assets.newBuilder() .addDirectory( TargetedAssetsDirectory.newBuilder() .setPath("assets") .setTargeting(AssetsDirectoryTargeting.getDefaultInstance())) .build()); } }
private void validateTargeting(BundleModule module, Assets assets) { for (TargetedAssetsDirectory targetedDirectory : assets.getDirectoryList()) { ZipPath path = ZipPath.create(targetedDirectory.getPath()); if (!path.startsWith(ASSETS_DIRECTORY)) { throw ValidationException.builder() .withMessage( "Path of targeted assets directory must start with 'assets/' but found '%s'.", path) .build(); } if (BundleValidationUtils.directoryContainsNoFiles(module, path)) { throw ValidationException.builder() .withMessage("Targeted directory '%s' is empty.", path) .build(); } } } }
public static Assets assets(TargetedAssetsDirectory... directories) { return Assets.newBuilder().addAllDirectory(Lists.newArrayList(directories)).build(); }
@Override void addToModule(BundleModule.Builder module, InputStream inputStream) throws IOException { module.setAssetsConfig(Assets.parseFrom(inputStream)); } },
@Test public void correctAssetsProtoFile_parsedAndReturned() throws Exception { Assets assetsConfig = Assets.newBuilder() .addDirectory(TargetedAssetsDirectory.newBuilder().setPath("assets/data-armv6")) .build(); BundleModule bundleModule = createMinimalModuleBuilder() .addEntry(InMemoryModuleEntry.ofFile("assets.pb", assetsConfig.toByteArray())) .build(); assertThat(bundleModule.getAssetsConfig()).hasValue(assetsConfig); }
public BundleModuleBuilder setAssetsConfig(Assets assetsConfig) { addFile("assets.pb", assetsConfig.toByteArray()); return this; }