Search in sources :

Example 1 with SyncLibraryRegistry

use of com.android.tools.idea.gradle.project.sync.setup.module.SyncLibraryRegistry in project android by JetBrains.

the class AndroidModuleDependenciesSetup method setUpLibraryDependency.

void setUpLibraryDependency(@NotNull Module module, @NotNull IdeModifiableModelsProvider modelsProvider, @NotNull String libraryName, @NotNull DependencyScope scope, @NotNull File artifactPath, @NotNull File[] binaryPaths, @NotNull File[] documentationPaths) {
    boolean newLibrary = false;
    Library library = modelsProvider.getLibraryByName(libraryName);
    if (library == null) {
        library = modelsProvider.createLibrary(libraryName);
        newLibrary = true;
    } else {
        SyncLibraryRegistry registry = SyncLibraryRegistry.getInstance(module.getProject());
        registry.markAsUsed(library, binaryPaths);
    }
    if (newLibrary) {
        updateLibraryRootTypePaths(library, CLASSES, modelsProvider, binaryPaths);
        // It is common that the same dependency is used by more than one module. Here we update the "sources" and "documentation" paths if they
        // were not set before.
        // Example:
        // In a multi-project project, there are 2 modules: 'app'' (an Android app) and 'util' (a Java lib.) Both of them depend on Guava. Since
        // Android artifacts do not support source attachments, the 'app' module may not indicate where to find the sources for Guava, but the
        // 'util' method can, since it is a plain Java module.
        // If the 'Guava' library was already defined when setting up 'app', it won't have source attachments. When setting up 'util' we may
        // have source attachments, but the library may have been already created. Here we just add the "source" paths if they were not already
        // set.
        File sourceJarPath = myLibraryFilePaths.findSourceJarPath(artifactPath);
        if (sourceJarPath != null) {
            updateLibraryRootTypePaths(library, SOURCES, modelsProvider, sourceJarPath);
        }
        updateLibraryRootTypePaths(library, JavadocOrderRootType.getInstance(), modelsProvider, documentationPaths);
        // TODO: Add this to the model instead!
        for (File binaryPath : binaryPaths) {
            String pathName = binaryPath.getName();
            if (pathName.endsWith(FD_RES) && pathName.length() > FD_RES.length() && pathName.charAt(pathName.length() - FD_RES.length() - 1) == separatorChar) {
                File annotations = new File(pathName.substring(0, pathName.length() - FD_RES.length()), FN_ANNOTATIONS_ZIP);
                if (annotations.isFile()) {
                    updateLibraryRootTypePaths(library, AnnotationOrderRootType.getInstance(), modelsProvider, annotations);
                }
            } else if (libraryName.startsWith("support-annotations-") && pathName.endsWith(DOT_JAR)) {
                // The support annotations is a Java library, not an Android library, so it's not distributed as an AAR
                // with its own external annotations. However, there are a few that we want to make available in the
                // IDE (for example, code completion on @VisibleForTesting(otherwise = |), so we bundle these in the
                // platform annotations zip file instead. We'll also need to add this as a root here.
                File annotations = new File(pathName.substring(0, pathName.length() - DOT_JAR.length()) + "-" + FN_ANNOTATIONS_ZIP);
                if (annotations.isFile()) {
                    updateLibraryRootTypePaths(library, AnnotationOrderRootType.getInstance(), modelsProvider, annotations);
                }
            }
        }
    }
    addLibraryAsDependency(library, libraryName, scope, module, modelsProvider);
}
Also used : Library(com.intellij.openapi.roots.libraries.Library) File(java.io.File) SyncLibraryRegistry(com.android.tools.idea.gradle.project.sync.setup.module.SyncLibraryRegistry)

Example 2 with SyncLibraryRegistry

use of com.android.tools.idea.gradle.project.sync.setup.module.SyncLibraryRegistry in project android by JetBrains.

the class LibraryCleanupStep method cleanUpProject.

@Override
public void cleanUpProject(@NotNull Project project, @NotNull IdeModifiableModelsProvider ideModelsProvider, @Nullable ProgressIndicator indicator) {
    SyncLibraryRegistry libraryRegistry = SyncLibraryRegistry.getInstance(project);
    // Remove unused libraries.
    for (Library library : libraryRegistry.getLibrariesToRemove()) {
        ideModelsProvider.removeLibrary(library);
    }
    // Update library URLs if they changed between Gradle sync operations.
    for (SyncLibraryRegistry.LibraryToUpdate libraryToUpdate : libraryRegistry.getLibrariesToUpdate()) {
        Library library = libraryToUpdate.getLibrary();
        Library.ModifiableModel libraryModel = ideModelsProvider.getModifiableLibraryModel(library);
        for (String existingBinaryUrl : libraryModel.getUrls(CLASSES)) {
            libraryModel.removeRoot(existingBinaryUrl, CLASSES);
        }
        for (String newBinaryUrl : libraryToUpdate.getNewBinaryUrls()) {
            libraryModel.addRoot(newBinaryUrl, CLASSES);
        }
    }
    Disposer.dispose(libraryRegistry);
}
Also used : Library(com.intellij.openapi.roots.libraries.Library) SyncLibraryRegistry(com.android.tools.idea.gradle.project.sync.setup.module.SyncLibraryRegistry)

Example 3 with SyncLibraryRegistry

use of com.android.tools.idea.gradle.project.sync.setup.module.SyncLibraryRegistry in project android by JetBrains.

the class JavaModuleDependenciesSetup method setUpLibraryDependency.

void setUpLibraryDependency(@NotNull Module module, @NotNull IdeModifiableModelsProvider modelsProvider, @NotNull String libraryName, @NotNull DependencyScope scope, @NotNull File binaryPath, @Nullable File sourcePath, @Nullable File documentationPath) {
    boolean newLibrary = false;
    Library library = modelsProvider.getLibraryByName(libraryName);
    if (library == null) {
        library = modelsProvider.createLibrary(libraryName);
        newLibrary = true;
    } else {
        SyncLibraryRegistry registry = SyncLibraryRegistry.getInstance(module.getProject());
        registry.markAsUsed(library, binaryPath);
    }
    if (newLibrary) {
        updateLibraryRootPath(library, CLASSES, modelsProvider, binaryPath);
        updateLibraryRootPath(library, SOURCES, modelsProvider, sourcePath);
        updateLibraryRootPath(library, JavadocOrderRootType.getInstance(), modelsProvider, documentationPath);
    }
    addLibraryAsDependency(library, libraryName, scope, module, modelsProvider);
}
Also used : Library(com.intellij.openapi.roots.libraries.Library) SyncLibraryRegistry(com.android.tools.idea.gradle.project.sync.setup.module.SyncLibraryRegistry)

Aggregations

SyncLibraryRegistry (com.android.tools.idea.gradle.project.sync.setup.module.SyncLibraryRegistry)3 Library (com.intellij.openapi.roots.libraries.Library)3 File (java.io.File)1