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()); }
/** * Given a set of potentially overlapping sdk targetings generate set of disjoint sdk targetings * covering all of them. * * <p>Assumption: There are no sdk range gaps in targetings. */ private static ImmutableList<SdkVersionTargeting> generateAllSdkTargetings( ImmutableList<SdkVersionTargeting> sdkVersionTargetings) { sdkVersionTargetings.forEach( sdkVersionTargeting -> checkState(sdkVersionTargeting.getValueList().size() == 1)); ImmutableList<Integer> minSdkValues = sdkVersionTargetings .stream() .map(sdkVersionTargeting -> sdkVersionTargeting.getValue(0).getMin().getValue()) .distinct() .sorted() .collect(toImmutableList()); ImmutableSet<SdkVersion> sdkVersions = minSdkValues.stream().map(TargetingProtoUtils::sdkVersionFrom).collect(toImmutableSet()); return sdkVersions .stream() .map( sdkVersion -> sdkVersionTargeting( sdkVersion, Sets.difference(sdkVersions, ImmutableSet.of(sdkVersion)).immutableCopy())) .collect(toImmutableList()); }
/** Extracts the minimum sdk (inclusive) supported by the targeting. */ public static int getMinSdk(SdkVersionTargeting sdkVersionTargeting) { if (sdkVersionTargeting.getValueList().isEmpty()) { return 1; } return Iterables.getOnlyElement(sdkVersionTargeting.getValueList()).getMin().getValue(); }
@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)); }
@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)); }
@Override protected ImmutableList<SdkVersion> getValues(VariantTargeting targeting) { return ImmutableList.copyOf(targeting.getSdkVersionTargeting().getValueList()); }
DeviceSpecFromTargetingBuilder setSdkVersion(SdkVersionTargeting sdkVersionTargeting) { if (!sdkVersionTargeting.equals(SdkVersionTargeting.getDefaultInstance())) { deviceSpec.setSdkVersion( Iterables.getOnlyElement(sdkVersionTargeting.getValueList()).getMin().getValue()); } return this; }
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; }