Search in sources :

Example 31 with AndroidModuleModel

use of com.android.tools.idea.gradle.project.model.AndroidModuleModel 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 32 with AndroidModuleModel

use of com.android.tools.idea.gradle.project.model.AndroidModuleModel in project android by JetBrains.

the class ExcludedRoots method addLibraryPaths.

private void addLibraryPaths(@NotNull Module module) {
    AndroidModuleModel model = AndroidModuleModel.get(module);
    if (model != null) {
        BaseArtifact exclude = myAndroidTest ? model.getUnitTestArtifactInSelectedVariant() : model.getAndroidTestArtifactInSelectedVariant();
        BaseArtifact include = myAndroidTest ? model.getAndroidTestArtifactInSelectedVariant() : model.getUnitTestArtifactInSelectedVariant();
        if (exclude != null) {
            addLibraryPaths(exclude, model);
        }
        if (include != null) {
            removeLibraryPaths(include, model);
        }
    }
}
Also used : AndroidModuleModel(com.android.tools.idea.gradle.project.model.AndroidModuleModel)

Example 33 with AndroidModuleModel

use of com.android.tools.idea.gradle.project.model.AndroidModuleModel in project android by JetBrains.

the class ExcludedRoots method addFolderPathsFromExcludedModules.

private void addFolderPathsFromExcludedModules() {
    for (Module module : myExcludedModules) {
        ModuleRootManager rootManager = ModuleRootManager.getInstance(module);
        for (ContentEntry entry : rootManager.getContentEntries()) {
            for (SourceFolder sourceFolder : entry.getSourceFolders()) {
                myExcludedRoots.add(urlToFilePath(sourceFolder.getUrl()));
            }
            CompilerModuleExtension compiler = rootManager.getModuleExtension(CompilerModuleExtension.class);
            String url = compiler.getCompilerOutputUrl();
            if (isNotEmpty(url)) {
                myExcludedRoots.add(urlToFilePath(url));
            }
        }
        AndroidModuleModel androidModuleModel = AndroidModuleModel.get(module);
        if (androidModuleModel != null) {
            myExcludedRoots.add(androidModuleModel.getMainArtifact().getJavaResourcesFolder());
        }
    }
}
Also used : SourceFolder(com.intellij.openapi.roots.SourceFolder) ContentEntry(com.intellij.openapi.roots.ContentEntry) AndroidModuleModel(com.android.tools.idea.gradle.project.model.AndroidModuleModel) ModuleRootManager(com.intellij.openapi.roots.ModuleRootManager) Module(com.intellij.openapi.module.Module) CompilerModuleExtension(com.intellij.openapi.roots.CompilerModuleExtension)

Example 34 with AndroidModuleModel

use of com.android.tools.idea.gradle.project.model.AndroidModuleModel in project android by JetBrains.

the class AndroidTestRunConfiguration method supportsRunningLibraryProjects.

@Override
protected Pair<Boolean, String> supportsRunningLibraryProjects(@NotNull AndroidFacet facet) {
    if (!facet.requiresAndroidModel()) {
        // Non Gradle projects always require an application
        return Pair.create(Boolean.FALSE, AndroidBundle.message("android.cannot.run.library.project.error"));
    }
    // TODO: Resolve direct AndroidGradleModel dep (b/22596984)
    AndroidModuleModel androidModel = AndroidModuleModel.get(facet);
    if (androidModel == null) {
        return Pair.create(Boolean.FALSE, AndroidBundle.message("android.cannot.run.library.project.error"));
    }
    // Gradle only supports testing against a single build type (which could be anything, but is "debug" build type by default)
    // Currently, the only information the model exports that we can use to detect whether the current build type
    // is testable is by looking at the test task name and checking whether it is null.
    AndroidArtifact testArtifact = androidModel.getAndroidTestArtifactInSelectedVariant();
    String testTask = testArtifact != null ? testArtifact.getAssembleTaskName() : null;
    return new Pair<Boolean, String>(testTask != null, AndroidBundle.message("android.cannot.run.library.project.in.this.buildtype"));
}
Also used : AndroidModuleModel(com.android.tools.idea.gradle.project.model.AndroidModuleModel) AndroidArtifact(com.android.builder.model.AndroidArtifact) Pair(com.intellij.openapi.util.Pair)

Example 35 with AndroidModuleModel

use of com.android.tools.idea.gradle.project.model.AndroidModuleModel in project android by JetBrains.

the class TestArtifactSearchScopes method resolveDependencies.

@VisibleForTesting
void resolveDependencies() {
    AndroidModuleModel androidModel = getAndroidModel();
    if (androidModel == null || alreadyResolved) {
        return;
    }
    mainDependencies = extractMainDependencies(androidModel);
    androidTestDependencies = extractAndroidTestDependencies(androidModel);
    unitTestDependencies = extractUnitTestDependencies(androidModel);
    // mainDependencies' mainDependencies should be merged to own mainDependencies, others shouldn't be merged
    mergeSubmoduleDependencies(mainDependencies, mainDependencies, null, null);
    // androidTestDependencies' mainDependencies and androidTestDependencies should be merged to own androidTestDependencies, others
    // shouldn't be merged
    mergeSubmoduleDependencies(androidTestDependencies, androidTestDependencies, androidTestDependencies, null);
    // unitTestDependencies' mainDependencies and unitTestDependencies should be merged to own unitTestDependencies, others shouldn't be
    // merged
    mergeSubmoduleDependencies(unitTestDependencies, unitTestDependencies, null, unitTestDependencies);
    alreadyResolved = true;
}
Also used : AndroidModuleModel(com.android.tools.idea.gradle.project.model.AndroidModuleModel) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

AndroidModuleModel (com.android.tools.idea.gradle.project.model.AndroidModuleModel)117 Module (com.intellij.openapi.module.Module)54 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)26 File (java.io.File)24 NotNull (org.jetbrains.annotations.NotNull)21 VirtualFile (com.intellij.openapi.vfs.VirtualFile)19 Nullable (org.jetbrains.annotations.Nullable)18 AndroidProject (com.android.builder.model.AndroidProject)12 GradleVersion (com.android.ide.common.repository.GradleVersion)11 NdkModuleModel (com.android.tools.idea.gradle.project.model.NdkModuleModel)10 Project (com.intellij.openapi.project.Project)9 Variant (com.android.builder.model.Variant)8 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)7 AndroidLibrary (com.android.builder.model.AndroidLibrary)5 PsiFile (com.intellij.psi.PsiFile)5 AndroidArtifact (com.android.builder.model.AndroidArtifact)4 AndroidArtifactOutput (com.android.builder.model.AndroidArtifactOutput)4 NativeAndroidProject (com.android.builder.model.NativeAndroidProject)4 ModuleNodeBuilder (com.android.tools.idea.gradle.AndroidModelView.ModuleNodeBuilder)4 AndroidVersion (com.android.sdklib.AndroidVersion)3