Search in sources :

Example 21 with GradleCoordinate

use of com.android.ide.common.repository.GradleCoordinate in project android by JetBrains.

the class GradleDependencyManager method ensureLibraryIsIncluded.

/**
   * Ensures that all the specified dependencies are included in the specified module.
   * <p/>
   * If some dependencies are missing a dialog is presented to the user if those dependencies should be added to the module.
   * If the user agrees the dependencies are added. The caller may supply a callback to determine when the requested dependencies
   * have been added (this make take several seconds).
   *
   * @param module       the module to add dependencies to
   * @param dependencies the dependencies of interest.
   * @param callback     an optional callback to signal to completion of the added dependencies
   * @return true if the dependencies were already present in the module or if the user requested adding them, and
   * false if the dependency was missing and the user declined adding them
   */
public boolean ensureLibraryIsIncluded(@NotNull Module module, @NotNull Iterable<GradleCoordinate> dependencies, @Nullable Runnable callback) {
    List<GradleCoordinate> missing = findMissingDependencies(module, dependencies);
    if (missing.isEmpty()) {
        return true;
    }
    GradleBuildModel buildModel = GradleBuildModel.get(module);
    if (buildModel == null) {
        return false;
    }
    if (userWantToAddDependencies(module, missing)) {
        addDependenciesInTransaction(buildModel, module, missing, callback);
        return true;
    }
    return false;
}
Also used : GradleCoordinate(com.android.ide.common.repository.GradleCoordinate) GradleBuildModel(com.android.tools.idea.gradle.dsl.model.GradleBuildModel)

Example 22 with GradleCoordinate

use of com.android.ide.common.repository.GradleCoordinate in project android by JetBrains.

the class GradleDependencyManager method addDependencies.

private static void addDependencies(@NotNull GradleBuildModel buildModel, @NotNull Module module, @NotNull List<GradleCoordinate> coordinates) {
    ModuleRootModificationUtil.updateModel(module, model -> {
        DependenciesModel dependenciesModel = buildModel.dependencies();
        for (GradleCoordinate coordinate : coordinates) {
            dependenciesModel.addArtifact(COMPILE, coordinate.toString());
        }
        buildModel.applyChanges();
    });
}
Also used : GradleCoordinate(com.android.ide.common.repository.GradleCoordinate) DependenciesModel(com.android.tools.idea.gradle.dsl.model.dependencies.DependenciesModel)

Example 23 with GradleCoordinate

use of com.android.ide.common.repository.GradleCoordinate in project android by JetBrains.

the class UpgradeConstraintLayoutFix method apply.

public static void apply(@Nullable Module module) {
    if (module != null) {
        StudioSdkUtil.reloadRemoteSdkWithModalProgress();
        AndroidSdkHandler sdkHandler = AndroidSdks.getInstance().tryToChooseSdkHandler();
        StudioLoggerProgressIndicator progress = new StudioLoggerProgressIndicator(AndroidLintMissingConstraintsInspection.class);
        RepoPackage p = SdkMavenRepository.findLatestVersion(LATEST_KNOWN_VERSION, sdkHandler, progress);
        if (p != null) {
            GradleCoordinate gc = SdkMavenRepository.getCoordinateFromSdkPath(p.getPath());
            if (gc != null) {
                // should always be the case unless the version suffix is somehow wrong
                // Update version dependency in the module. Note that this will trigger a sync too.
                GradleDependencyManager manager = GradleDependencyManager.getInstance(module.getProject());
                manager.updateLibrariesToVersion(module, Collections.singletonList(gc), null);
            }
        }
    }
}
Also used : StudioLoggerProgressIndicator(com.android.tools.idea.sdk.progress.StudioLoggerProgressIndicator) GradleCoordinate(com.android.ide.common.repository.GradleCoordinate) AndroidSdkHandler(com.android.sdklib.repository.AndroidSdkHandler) GradleDependencyManager(com.android.tools.idea.gradle.dependencies.GradleDependencyManager) RepoPackage(com.android.repository.api.RepoPackage)

Example 24 with GradleCoordinate

use of com.android.ide.common.repository.GradleCoordinate in project android by JetBrains.

the class ImportSummary method createSummary.

/**
   * Provides the summary
   */
@NonNull
public String createSummary() {
    StringBuilder sb = new StringBuilder(2000);
    sb.append(MSG_HEADER);
    List<String> problems = Lists.newArrayList();
    problems.addAll(myImporter.getErrors());
    problems.addAll(myImporter.getWarnings());
    if (!problems.isEmpty()) {
        sb.append("\n");
        for (String warning : problems) {
            sb.append(" * ");
            if (myWrapErrorMessages) {
                sb.append(SdkUtils.wrap(warning, 80, "   "));
            } else {
                sb.append(warning);
            }
            sb.append("\n");
        }
    }
    if (myHasRiskyPathChars) {
        sb.append(MSG_RISKY_PROJECT_LOCATION);
        String path = myDestDir.getPath();
        sb.append(path).append("\n");
        for (int i = 0, n = path.length(); i < n; i++) {
            char c = path.charAt(i);
            sb.append(isRiskyPathChar(c) ? '-' : ' ');
        }
        sb.append("\n");
    }
    if (myManifestsMayDiffer) {
        sb.append(MSG_MANIFEST);
    }
    if (!myNotMigrated.isEmpty()) {
        sb.append(MSG_UNHANDLED);
        List<String> modules = Lists.newArrayList(myNotMigrated.keySet());
        Collections.sort(modules);
        for (String module : modules) {
            if (modules.size() > 1) {
                sb.append("From ").append(module).append(":\n");
            }
            List<String> sorted = new ArrayList<String>(myNotMigrated.get(module));
            Collections.sort(sorted);
            for (String path : sorted) {
                sb.append("* ").append(path).append("\n");
            }
        }
    }
    if (!myJarDependencies.isEmpty()) {
        sb.append(MSG_REPLACED_JARS);
        // TODO: Also add note here about switching to AAR's potentially also creating
        // compilation errors because it now enforces that app min sdk version is >= library
        // min sdk version, and suggesting that they re-run import with replaceJars=false
        // if this leads to problems.
        List<File> files = Lists.newArrayList(myJarDependencies.keySet());
        Collections.sort(files);
        for (File file : files) {
            String jar = file.getName();
            GradleCoordinate dependency = myJarDependencies.get(file);
            sb.append(jar).append(" => ").append(dependency).append("\n");
        }
    }
    if (!myGuessedDependencyVersions.isEmpty()) {
        sb.append(MSG_GUESSED_VERSIONS);
        Collections.sort(myGuessedDependencyVersions);
        for (String replaced : myGuessedDependencyVersions) {
            sb.append(replaced).append("\n");
        }
    }
    if (!myLibDependencies.isEmpty()) {
        sb.append(MSG_REPLACED_LIBS);
        List<String> modules = Lists.newArrayList(myLibDependencies.keySet());
        Collections.sort(modules);
        for (String module : modules) {
            List<GradleCoordinate> dependencies = myLibDependencies.get(module);
            if (dependencies.size() == 1) {
                sb.append(module).append(" => ").append(dependencies).append("\n");
            } else {
                sb.append(module).append(" =>\n");
                for (GradleCoordinate dependency : dependencies) {
                    sb.append("    ").append(dependency).append("\n");
                }
            }
        }
    }
    if (!myMoved.isEmpty()) {
        sb.append(MSG_FOLDER_STRUCTURE);
        List<ImportModule> modules = Lists.newArrayList(myMoved.keySet());
        Collections.sort(modules);
        for (ImportModule module : modules) {
            if (modules.size() > 1) {
                sb.append("In ").append(module.getOriginalName()).append(":\n");
            }
            Map<File, File> map = myMoved.get(module);
            List<File> sorted = new ArrayList<File>(map.keySet());
            Collections.sort(sorted);
            for (File from : sorted) {
                sb.append("* ");
                File to = map.get(from);
                assert to != null : from;
                File fromRelative = null;
                File toRelative = null;
                try {
                    fromRelative = module.computeProjectRelativePath(from);
                    if (myDestDir != null) {
                        toRelative = GradleImport.computeRelativePath(myDestDir.getCanonicalFile(), to);
                    }
                } catch (IOException ioe) {
                // pass; use full path
                }
                if (fromRelative == null) {
                    fromRelative = from;
                }
                if (toRelative == null) {
                    toRelative = to;
                }
                sb.append(fromRelative.getPath());
                if (from.isDirectory()) {
                    sb.append(File.separator);
                }
                sb.append(" => ");
                sb.append(toRelative.getPath());
                if (to.isDirectory()) {
                    sb.append(File.separator);
                }
                sb.append("\n");
            }
        }
    }
    if (myImporter.needSupportRepository() && myImporter.isMissingSupportRepository()) {
        sb.append(MSG_MISSING_REPO_1);
        sb.append(myImporter.getSdkLocation()).append("\n");
        sb.append(MSG_MISSING_REPO_2);
    }
    if (myImporter.needGoogleRepository() && myImporter.isMissingGoogleRepository()) {
        sb.append(MSG_MISSING_GOOGLE_REPOSITORY_1);
        sb.append(myImporter.getSdkLocation()).append("\n");
        sb.append(MSG_MISSING_GOOGLE_REPOSITORY_2);
    }
    if (Revision.parseRevision(myImporter.getBuildToolsVersion()).getMajor() < 19) {
        sb.append(MSG_BUILD_TOOLS_VERSION);
    }
    if (!myIgnoredUserHomeProGuardFiles.isEmpty()) {
        sb.append(MSG_USER_HOME_PROGUARD);
        Collections.sort(myIgnoredUserHomeProGuardFiles);
        for (String path : myIgnoredUserHomeProGuardFiles) {
            sb.append(path).append("\n");
        }
    }
    sb.append(MSG_FOOTER);
    return sb.toString().replace("\n", GradleImport.NL);
}
Also used : GradleCoordinate(com.android.ide.common.repository.GradleCoordinate) IOException(java.io.IOException) File(java.io.File) NonNull(com.android.annotations.NonNull)

Example 25 with GradleCoordinate

use of com.android.ide.common.repository.GradleCoordinate in project android by JetBrains.

the class GradleImport method appendDependencies.

private static void appendDependencies(@NonNull StringBuilder sb, @NonNull ImportModule module) throws IOException {
    if (!module.getDirectDependencies().isEmpty() || !module.getDependencies().isEmpty() || !module.getJarDependencies().isEmpty() || !module.getTestDependencies().isEmpty() || !module.getTestJarDependencies().isEmpty()) {
        sb.append(NL);
        sb.append("dependencies {").append(NL);
        for (ImportModule lib : module.getDirectDependencies()) {
            if (lib.isReplacedWithDependency()) {
                continue;
            }
            sb.append("    compile project('").append(lib.getModuleReference()).append("')").append(NL);
        }
        for (GradleCoordinate dependency : module.getDependencies()) {
            sb.append("    compile '").append(dependency.toString()).append("'").append(NL);
        }
        for (File jar : module.getJarDependencies()) {
            // Always / in gradle
            String path = jar.getPath().replace(separatorChar, '/');
            sb.append("    compile files('").append(escapeGroovyStringLiteral(path)).append("')").append(NL);
        }
        for (GradleCoordinate dependency : module.getTestDependencies()) {
            sb.append("    androidTestCompile '").append(dependency.toString()).append("'").append(NL);
        }
        for (File jar : module.getTestJarDependencies()) {
            String path = jar.getPath().replace(separatorChar, '/');
            sb.append("    androidTestCompile files('").append(escapeGroovyStringLiteral(path)).append("')").append(NL);
        }
        sb.append("}").append(NL);
    }
}
Also used : GradleCoordinate(com.android.ide.common.repository.GradleCoordinate) File(java.io.File)

Aggregations

GradleCoordinate (com.android.ide.common.repository.GradleCoordinate)35 File (java.io.File)9 GradleDependencyManager (com.android.tools.idea.gradle.dependencies.GradleDependencyManager)5 NotNull (org.jetbrains.annotations.NotNull)5 RemotePackage (com.android.repository.api.RemotePackage)4 AndroidSdkHandler (com.android.sdklib.repository.AndroidSdkHandler)3 GradleBuildModel (com.android.tools.idea.gradle.dsl.model.GradleBuildModel)3 ArtifactDependencyModel (com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyModel)3 StudioLoggerProgressIndicator (com.android.tools.idea.sdk.progress.StudioLoggerProgressIndicator)3 Project (com.intellij.openapi.project.Project)3 IOException (java.io.IOException)3 Nullable (org.jetbrains.annotations.Nullable)3 Nullable (com.android.annotations.Nullable)2 SdkMavenRepository (com.android.ide.common.repository.SdkMavenRepository)2 RepoManager (com.android.repository.api.RepoManager)2 RepoPackage (com.android.repository.api.RepoPackage)2 RepositoryPackages (com.android.repository.impl.meta.RepositoryPackages)2 FakeRepoManager (com.android.repository.testframework.FakeRepoManager)2 DependenciesModel (com.android.tools.idea.gradle.dsl.model.dependencies.DependenciesModel)2 AndroidModuleModel (com.android.tools.idea.gradle.project.model.AndroidModuleModel)2