Search in sources :

Example 1 with JavaArtifact

use of com.android.builder.model.JavaArtifact in project android by JetBrains.

the class AndroidJunitPatcher method patchJavaParameters.

@Override
public void patchJavaParameters(@Nullable Module module, @NotNull JavaParameters javaParameters) {
    // Only patch if the project is a Gradle project.
    if (module == null || !isBuildWithGradle(module.getProject())) {
        return;
    }
    AndroidModuleModel androidModel = AndroidModuleModel.get(module);
    if (androidModel == null) {
        return;
    }
    // Modify the class path only if we're dealing with the unit test artifact.
    JavaArtifact testArtifact = androidModel.getUnitTestArtifactInSelectedVariant();
    if (testArtifact == null) {
        return;
    }
    PathsList classPath = javaParameters.getClassPath();
    TestArtifactSearchScopes testScopes = TestArtifactSearchScopes.get(module);
    if (testScopes == null) {
        return;
    }
    // Filter the library / module dependencies that are in android test
    FileRootSearchScope excludeScope = testScopes.getUnitTestExcludeScope();
    // complexity. TODO change the {@code PathList} API.
    for (String path : classPath.getPathList()) {
        if (excludeScope.accept(new File(path))) {
            classPath.remove(path);
        }
    }
    AndroidPlatform platform = AndroidPlatform.getInstance(module);
    if (platform == null) {
        return;
    }
    String originalClassPath = classPath.getPathsString();
    try {
        handlePlatformJar(classPath, platform, testArtifact);
        handleJavaResources(module, androidModel, classPath);
    } catch (RuntimeException e) {
        throw new RuntimeException(String.format("Error patching the JUnit class path. Original class path:%n%s", originalClassPath), e);
    }
}
Also used : JavaArtifact(com.android.builder.model.JavaArtifact) PathsList(com.intellij.util.PathsList) AndroidModuleModel(com.android.tools.idea.gradle.project.model.AndroidModuleModel) AndroidPlatform(org.jetbrains.android.sdk.AndroidPlatform) File(java.io.File)

Example 2 with JavaArtifact

use of com.android.builder.model.JavaArtifact in project android by JetBrains.

the class CompilerOutputModuleSetupStep method doSetUpModule.

@Override
protected void doSetUpModule(@NotNull Module module, @NotNull IdeModifiableModelsProvider ideModelsProvider, @NotNull AndroidModuleModel androidModel, @Nullable SyncAction.ModuleModels gradleModels, @Nullable ProgressIndicator indicator) {
    GradleVersion modelVersion = androidModel.getModelVersion();
    if (modelVersion == null) {
        // We are dealing with old model that does not have the 'class' folder.
        return;
    }
    Variant selectedVariant = androidModel.getSelectedVariant();
    File mainClassesFolder = selectedVariant.getMainArtifact().getClassesFolder();
    JavaArtifact testArtifact = androidModel.getUnitTestArtifactInSelectedVariant();
    File testClassesFolder = testArtifact == null ? null : testArtifact.getClassesFolder();
    ModifiableRootModel rootModel = ideModelsProvider.getModifiableRootModel(module);
    myCompilerSettingsSetup.setOutputPaths(rootModel, mainClassesFolder, testClassesFolder);
}
Also used : Variant(com.android.builder.model.Variant) ModifiableRootModel(com.intellij.openapi.roots.ModifiableRootModel) JavaArtifact(com.android.builder.model.JavaArtifact) GradleVersion(com.android.ide.common.repository.GradleVersion) File(java.io.File)

Example 3 with JavaArtifact

use of com.android.builder.model.JavaArtifact in project android by JetBrains.

the class AndroidJunitPatcher method handleJavaResources.

/**
   * Puts folders with merged java resources for the selected variant of every module on the classpath.
   *
   * <p>The problem we're solving here is that CompilerModuleExtension supports only one directory for "compiler output". When IJ compiles
   * Java projects, it copies resources to the output classes dir. This is something our Gradle plugin doesn't do, so we need to add the
   * resource directories to the classpath here.
   *
   * <p>We need to do this for every project dependency as well, since we're using classes and resources directories of these directly.
   *
   * @see <a href="http://b.android.com/172409">Bug 172409</a>
   */
private static void handleJavaResources(@NotNull Module module, @NotNull AndroidModuleModel androidModel, @NotNull PathsList classPath) {
    CompilerManager compilerManager = CompilerManager.getInstance(module.getProject());
    CompileScope scope = compilerManager.createModulesCompileScope(new Module[] { module }, true, true);
    // The only test resources we want to use, are the ones from the module where the test is. They should go first, before main resources.
    JavaArtifact testArtifact = androidModel.getUnitTestArtifactInSelectedVariant();
    if (testArtifact != null) {
        try {
            classPath.add(testArtifact.getJavaResourcesFolder());
        } catch (UnsupportedMethodException ignored) {
        // Java resources were not present in older versions of the gradle plugin.
        }
    }
    FileRootSearchScope excludeScope = null;
    TestArtifactSearchScopes testScopes = TestArtifactSearchScopes.get(module);
    if (testScopes != null) {
        excludeScope = testScopes.getUnitTestExcludeScope();
    }
    for (Module affectedModule : scope.getAffectedModules()) {
        AndroidFacet facet = AndroidFacet.getInstance(affectedModule);
        if (facet != null) {
            AndroidModuleModel affectedAndroidModel = AndroidModuleModel.get(facet);
            if (affectedAndroidModel != null) {
                try {
                    File resourceFolder = affectedAndroidModel.getMainArtifact().getJavaResourcesFolder();
                    if (excludeScope != null && excludeScope.accept(resourceFolder)) {
                        continue;
                    }
                    classPath.add(resourceFolder);
                } catch (UnsupportedMethodException ignored) {
                // Java resources were not present in older versions of the gradle plugin.
                }
            }
        }
    }
}
Also used : CompileScope(com.intellij.openapi.compiler.CompileScope) JavaArtifact(com.android.builder.model.JavaArtifact) UnsupportedMethodException(org.gradle.tooling.model.UnsupportedMethodException) CompilerManager(com.intellij.openapi.compiler.CompilerManager) AndroidModuleModel(com.android.tools.idea.gradle.project.model.AndroidModuleModel) Module(com.intellij.openapi.module.Module) File(java.io.File) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

Aggregations

JavaArtifact (com.android.builder.model.JavaArtifact)3 File (java.io.File)3 AndroidModuleModel (com.android.tools.idea.gradle.project.model.AndroidModuleModel)2 Variant (com.android.builder.model.Variant)1 GradleVersion (com.android.ide.common.repository.GradleVersion)1 CompileScope (com.intellij.openapi.compiler.CompileScope)1 CompilerManager (com.intellij.openapi.compiler.CompilerManager)1 Module (com.intellij.openapi.module.Module)1 ModifiableRootModel (com.intellij.openapi.roots.ModifiableRootModel)1 PathsList (com.intellij.util.PathsList)1 UnsupportedMethodException (org.gradle.tooling.model.UnsupportedMethodException)1 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)1 AndroidPlatform (org.jetbrains.android.sdk.AndroidPlatform)1