public BuildRule createBuildRule()

in src/com/facebook/buck/android/AndroidInstrumentationApkDescription.java [98:289]


  public BuildRule createBuildRule(
      BuildRuleCreationContextWithTargetGraph context,
      BuildTarget buildTarget,
      BuildRuleParams params,
      AndroidInstrumentationApkDescriptionArg args) {
    ActionGraphBuilder graphBuilder = context.getActionGraphBuilder();
    params = params.withoutExtraDeps();
    BuildRule installableApk = graphBuilder.getRule(args.getApk());
    if (!(installableApk instanceof HasInstallableApk)
        || (installableApk instanceof AndroidInstrumentationApk)) {
      throw new HumanReadableException(
          "In %s, apk='%s' must be an android_binary() or apk_genrule() but was %s().",
          buildTarget, installableApk.getFullyQualifiedName(), installableApk.getType());
    }
    AndroidBinary apkUnderTest =
        ApkGenruleDescription.getUnderlyingApk((HasInstallableApk) installableApk);

    ImmutableSet<JavaLibrary> apkUnderTestTransitiveClasspathDeps =
        apkUnderTest.getTransitiveClasspathDeps();

    ImmutableSet.Builder<BuildTarget> buildTargetsToExclude = ImmutableSet.builder();
    apkUnderTest.getRulesToExcludeFromDex().get().stream()
        .map(BuildRule::getBuildTarget)
        .forEach(buildTargetsToExclude::add);
    apkUnderTestTransitiveClasspathDeps.stream()
        .map(BuildRule::getBuildTarget)
        .forEach(buildTargetsToExclude::add);

    APKModule rootAPKModule = APKModule.of(APKModuleGraph.ROOT_APKMODULE_NAME, true, true);
    AndroidPackageableCollection.ResourceDetails resourceDetails =
        apkUnderTest.getAndroidPackageableCollection().getResourceDetails().get(rootAPKModule);
    ImmutableSet<BuildTarget> resourcesToExclude =
        ImmutableSet.copyOf(
            Iterables.concat(
                resourceDetails.getResourcesWithNonEmptyResDir(),
                resourceDetails.getResourcesWithEmptyResButNonEmptyAssetsDir()));

    ImmutableCollection<SourcePath> nativeLibsToExclude =
        apkUnderTest
            .getAndroidPackageableCollection()
            .getNativeLibsDirectories()
            .get(rootAPKModule);

    ImmutableCollection<NativeLinkableGroup> nativeLinkablesToExcludeGroup =
        apkUnderTest.getAndroidPackageableCollection().getNativeLinkables().get(rootAPKModule);

    ImmutableCollection<SourcePath> nativeLibAssetsToExclude =
        apkUnderTest
            .getAndroidPackageableCollection()
            .getNativeLibAssetsDirectories()
            .get(rootAPKModule);

    ImmutableCollection<NativeLinkableGroup> nativeLinkableGroupAssetsToExclude =
        apkUnderTest
            .getAndroidPackageableCollection()
            .getNativeLinkablesAssets()
            .get(rootAPKModule);

    ListeningExecutorService dxExecutorService =
        toolchainProvider
            .getByName(
                DxToolchain.DEFAULT_NAME, buildTarget.getTargetConfiguration(), DxToolchain.class)
            .getDxExecutorService();

    boolean shouldProguard =
        apkUnderTest.getProguardConfig().isPresent()
            || !ProGuardObfuscateStep.SdkProguardType.NONE.equals(
                apkUnderTest.getSdkProguardConfig());
    NonPreDexedDexBuildable.NonPredexedDexBuildableArgs nonPreDexedDexBuildableArgs =
        ImmutableNonPredexedDexBuildableArgs.builder()
            .setProguardAgentPath(proGuardConfig.getProguardAgentPath())
            .setProguardJarOverride(
                proGuardConfig.getProguardJarOverride(buildTarget.getTargetConfiguration()))
            .setProguardMaxHeapSize(proGuardConfig.getProguardMaxHeapSize())
            .setSdkProguardConfig(apkUnderTest.getSdkProguardConfig())
            .setPreprocessJavaClassesBash(Optional.empty())
            .setReorderClassesIntraDex(false)
            .setDexReorderToolFile(Optional.empty())
            .setDexReorderDataDumpFile(Optional.empty())
            .setDxExecutorService(dxExecutorService)
            .setDxMaxHeapSize(Optional.empty())
            .setOptimizationPasses(apkUnderTest.getOptimizationPasses())
            .setProguardJvmArgs(apkUnderTest.getProguardJvmArgs())
            .setSkipProguard(apkUnderTest.getSkipProguard())
            .setJavaRuntimeLauncher(apkUnderTest.getJavaRuntimeLauncher())
            .setProguardConfigPath(apkUnderTest.getProguardConfig())
            .setShouldProguard(shouldProguard)
            .build();

    AndroidPlatformTarget androidPlatformTarget =
        toolchainProvider.getByName(
            AndroidPlatformTarget.DEFAULT_NAME,
            buildTarget.getTargetConfiguration(),
            AndroidPlatformTarget.class);

    String dexTool = args.getDexTool();
    AndroidBinaryGraphEnhancer graphEnhancer =
        new AndroidBinaryGraphEnhancer(
            toolchainProvider,
            context.getCellPathResolver(),
            buildTarget,
            context.getProjectFilesystem(),
            androidPlatformTarget,
            params,
            graphBuilder,
            args.getAaptMode(),
            ImmutableList.of(),
            ResourceCompressionMode.DISABLED,
            FilterResourcesSteps.ResourceFilter.EMPTY_FILTER,
            /* bannedDuplicateResourceTypes */ EnumSet.noneOf(RType.class),
            Optional.empty(),
            /* resourceUnionPackage */ Optional.empty(),
            /* locales */ ImmutableSet.of(),
            /* localizedStringFileName */ null,
            args.getManifest(),
            args.getManifestSkeleton(),
            /* moduleManifestSkeleton */ Optional.empty(),
            PackageType.INSTRUMENTED,
            apkUnderTest.getCpuFilters(),
            /* shouldBuildStringSourceMap */ false,
            /* shouldPreDex */ dexTool == DxStep.D8,
            DexSplitMode.NO_SPLIT,
            buildTargetsToExclude.build(),
            resourcesToExclude,
            nativeLibsToExclude,
            nativeLinkablesToExcludeGroup,
            nativeLibAssetsToExclude,
            nativeLinkableGroupAssetsToExclude,
            /* skipCrunchPngs */ false,
            args.getIncludesVectorDrawables(),
            /* noAutoVersionResources */ false,
            /* noVersionTransitionsResources */ false,
            /* noAutoAddOverlayResources */ false,
            androidBuckConfig.getAaptNoResourceRemoval(),
            javaBuckConfig,
            javacFactory,
            toolchainProvider
                .getByName(
                    JavacOptionsProvider.DEFAULT_NAME,
                    buildTarget.getTargetConfiguration(),
                    JavacOptionsProvider.class)
                .getJavacOptions(),
            EnumSet.noneOf(ExopackageMode.class),
            /* buildConfigValues */ BuildConfigFields.of(),
            /* buildConfigValuesFile */ Optional.empty(),
            /* xzCompressionLevel */ XzStep.DEFAULT_COMPRESSION_LEVEL,
            /* trimResourceIds */ false,
            /* keepResourcePattern */ Optional.empty(),
            false,
            /* nativeLibraryMergeMap */ Optional.empty(),
            /* nativeLibraryMergeGlue */ Optional.empty(),
            /* nativeLibraryMergeCodeGenerator */ Optional.empty(),
            /* nativeLibraryProguardConfigGenerator */ Optional.empty(),
            Optional.empty(),
            RelinkerMode.DISABLED,
            ImmutableList.of(),
            dxExecutorService,
            apkUnderTest.getManifestEntries(),
            cxxBuckConfig,
            new APKModuleGraph(context.getTargetGraph(), buildTarget),
            dxConfig,
            dexTool,
            /* postFilterResourcesCommands */ Optional.empty(),
            nonPreDexedDexBuildableArgs,
            createRulesToExcludeFromDexSupplier(
                apkUnderTest.getRulesToExcludeFromDex(), apkUnderTestTransitiveClasspathDeps),
            false,
            new NoopAndroidNativeTargetConfigurationMatcher(),
            androidBuckConfig.getFailOnLegacyAaptErrors(),
            false /* useAapt2LocaleFiltering */,
            ImmutableSet.of());

    AndroidGraphEnhancementResult enhancementResult = graphEnhancer.createAdditionalBuildables();
    AndroidBinaryFilesInfo filesInfo =
        new AndroidBinaryFilesInfo(enhancementResult, EnumSet.noneOf(ExopackageMode.class), false);
    return new AndroidInstrumentationApk(
        buildTarget,
        context.getProjectFilesystem(),
        toolchainProvider.getByName(
            AndroidSdkLocation.DEFAULT_NAME,
            buildTarget.getTargetConfiguration(),
            AndroidSdkLocation.class),
        androidPlatformTarget,
        params,
        graphBuilder,
        apkUnderTest,
        enhancementResult,
        filesInfo.getDexFilesInfo(),
        filesInfo.getNativeFilesInfo(),
        filesInfo.getResourceFilesInfo(),
        filesInfo.getExopackageInfo());
  }