private static Optional<Integer> getMinSdk(VariantTargeting variantTargeting) { // If the variant does not have an SDK targeting, it is suitable for all SDK values. if (variantTargeting.getSdkVersionTargeting().getValueList().isEmpty()) { return Optional.empty(); } // If there are many different minSdks values, select the maximum of those. // We ignore the alternatives. return variantTargeting .getSdkVersionTargeting() .getValueList() .stream() .map(sdkVersion -> sdkVersion.getMin().getValue()) .max(Comparator.naturalOrder()); }
@Override protected ImmutableList<SdkVersion> getValues(VariantTargeting targeting) { return ImmutableList.copyOf(targeting.getSdkVersionTargeting().getValueList()); }
/** * Given a set of potentially overlapping variant targetings generate smallest set of disjoint * variant targetings covering all of them. * * <p>Assumption: All Variants only support sdk targeting. */ public static ImmutableSet<VariantTargeting> generateAllVariantTargetings( ImmutableSet<VariantTargeting> variantTargetings) { if (variantTargetings.size() <= 1) { return variantTargetings; } ImmutableList<SdkVersionTargeting> sdkVersionTargetings = generateAllSdkTargetings( variantTargetings .stream() .map(variantTargeting -> variantTargeting.getSdkVersionTargeting()) .collect(toImmutableList())); return sdkVersionTargetings .stream() .map( sdkVersionTargeting -> VariantTargeting.newBuilder().setSdkVersionTargeting(sdkVersionTargeting).build()) .collect(toImmutableSet()); }
Map<SizeConfiguration, Long> maxSizeByConfiguration = new HashMap<>(); SdkVersionTargeting sdkVersionTargeting = variant.getTargeting().getSdkVersionTargeting(); for (AbiTargeting abiTargeting : abiTargetingOptions) { for (ScreenDensityTargeting screenDensityTargeting : screenDensityTargetingOptions) {
private ConfigurationSizes getSizeStandaloneVariant() { checkState( !getSizeRequest.getInstant(), "Standalone Variants cant be selected when instant flag is set"); // When modules are specified we ignore standalone variants. if (getSizeRequest.getModules().isPresent()) { return ConfigurationSizes.create(ImmutableMap.of(), ImmutableMap.of()); } VariantTargeting variantTargeting = variant.getTargeting(); SizeConfiguration sizeConfiguration = mergeWithDeviceSpec( getSizeConfiguration( variantTargeting.getSdkVersionTargeting(), variantTargeting.getAbiTargeting(), variantTargeting.getScreenDensityTargeting(), LanguageTargeting.getDefaultInstance()), getSizeRequest.getDeviceSpec()); // Variants of standalone APKs have only one APK each. long compressedSize = sizeByApkPaths.get( Iterables.getOnlyElement( Iterables.getOnlyElement(variant.getApkSetList()).getApkDescriptionList()) .getPath()); ImmutableMap<SizeConfiguration, Long> sizeConfigurationMap = ImmutableMap.of(sizeConfiguration, compressedSize); return ConfigurationSizes.create(sizeConfigurationMap, sizeConfigurationMap); }
@Test public void buildApksCommand_splitApks_targetLPlus() throws Exception { bundlePath = FileUtils.getRandomFilePath(tmp, "bundle-", ".aab"); AppBundle appBundle = new AppBundleBuilder() .addModule( "base", builder -> builder.addFile("dex/classes.dex").setManifest(androidManifest("com.test.app"))) .build(); bundleSerializer.writeToDisk(appBundle, bundlePath); BuildApksCommand command = BuildApksCommand.builder() .setBundlePath(bundlePath) .setOutputFile(outputFilePath) .setAapt2Command(aapt2Command) .build(); Path apkSetFilePath = execute(command); ZipFile apkSetFile = new ZipFile(apkSetFilePath.toFile()); BuildApksResult result = extractTocFromApkSetFile(apkSetFile, outputDir); assertThat(splitApkVariants(result)).hasSize(1); Variant variant = splitApkVariants(result).get(0); assertThat(variant.hasTargeting()).isTrue(); assertThat(variant.getTargeting().getSdkVersionTargeting().getValueList()) .containsExactly(sdkVersionFrom(ANDROID_L_API_VERSION)); }
@Test public void buildApksCommand_instantApks_targetLPlus() throws Exception { bundlePath = FileUtils.getRandomFilePath(tmp, "bundle-", ".aab"); AppBundle appBundle = new AppBundleBuilder() .addModule( "base", builder -> builder .addFile("dex/classes.dex") .setManifest(androidManifest("com.test.app", withInstant(true)))) .build(); bundleSerializer.writeToDisk(appBundle, bundlePath); BuildApksCommand command = BuildApksCommand.builder() .setBundlePath(bundlePath) .setOutputFile(outputFilePath) .setAapt2Command(aapt2Command) .build(); Path apkSetFilePath = execute(command); ZipFile apkSetFile = new ZipFile(apkSetFilePath.toFile()); BuildApksResult result = extractTocFromApkSetFile(apkSetFile, outputDir); assertThat(instantApkVariants(result)).hasSize(1); Variant variant = instantApkVariants(result).get(0); assertThat(variant.hasTargeting()).isTrue(); assertThat(variant.getTargeting().getSdkVersionTargeting().getValueList()) .containsExactly(sdkVersionFrom(ANDROID_L_API_VERSION)); }
@Override protected SdkVersionTargeting getTargetingValue(VariantTargeting variantTargeting) { return variantTargeting.getSdkVersionTargeting(); }
private static boolean targetsPreP(ModuleSplit moduleSplit) { int sdkVersion = Iterables.getOnlyElement( moduleSplit.getVariantTargeting().getSdkVersionTargeting().getValueList()) .getMin() .getValue(); return sdkVersion < ANDROID_P_API_VERSION; }
private static boolean targetsPreM(ModuleSplit moduleSplit) { int sdkVersion = Iterables.getOnlyElement( moduleSplit.getVariantTargeting().getSdkVersionTargeting().getValueList()) .getMin() .getValue(); return sdkVersion < ANDROID_M_API_VERSION; }