use of com.android.ide.common.repository.GradleCoordinate in project android by JetBrains.
the class GradleDependencyManager method updateDependencies.
private static void updateDependencies(@NotNull GradleBuildModel buildModel, @NotNull Module module, @NotNull List<GradleCoordinate> coordinates) {
ModuleRootModificationUtil.updateModel(module, model -> {
DependenciesModel dependenciesModel = buildModel.dependencies();
for (GradleCoordinate gc : coordinates) {
List<ArtifactDependencyModel> artifacts = Lists.newArrayList(dependenciesModel.artifacts());
for (ArtifactDependencyModel m : artifacts) {
if (gc.getGroupId() != null && gc.getGroupId().equals(m.group().value()) && gc.getArtifactId() != null && gc.getArtifactId().equals(m.name().value()) && !gc.getRevision().equals(m.version().value())) {
dependenciesModel.remove(m);
dependenciesModel.addArtifact(m.configurationName(), gc.toString());
}
}
}
buildModel.applyChanges();
});
}
use of com.android.ide.common.repository.GradleCoordinate in project android by JetBrains.
the class GradleDependencyManager method findMissingDependencies.
/**
* Returns the dependencies that are NOT included in the specified module.
* Note: the version of the dependency is disregarded.
*
* @param module the module to check dependencies in
* @param dependencies the dependencies of interest.
* @return a list of the dependencies NOT included in the module
*/
@NotNull
public List<GradleCoordinate> findMissingDependencies(@NotNull Module module, @NotNull Iterable<GradleCoordinate> dependencies) {
AndroidModuleModel gradleModel = AndroidModuleModel.get(module);
GradleBuildModel buildModel = GradleBuildModel.get(module);
if (gradleModel == null && buildModel == null) {
return Collections.emptyList();
}
RepositoryUrlManager manager = RepositoryUrlManager.get();
List<GradleCoordinate> missingLibraries = Lists.newArrayList();
for (GradleCoordinate coordinate : dependencies) {
GradleCoordinate resolvedCoordinate = manager.resolveDynamicCoordinate(coordinate, null);
if (resolvedCoordinate == null) {
// We don't have anything installed, but we can keep trying with the unresolved coordinate if we have enough info
if (coordinate.getArtifactId() == null || coordinate.getGroupId() == null) {
// TODO Should this be an error ?
continue;
}
} else {
coordinate = resolvedCoordinate;
}
boolean dependencyFound = false;
// First look in the model returned by Gradle.
if (gradleModel != null && GradleUtil.dependsOn(gradleModel, String.format("%s:%s", coordinate.getGroupId(), coordinate.getArtifactId()))) {
// GradleUtil.dependsOn method only checks the android library dependencies.
// TODO: Consider updating it to also check for java library dependencies.
dependencyFound = true;
} else if (buildModel != null) {
// Now, check in the model obtained from the gradle files.
for (ArtifactDependencyModel dependency : buildModel.dependencies().artifacts(COMPILE)) {
if (Objects.equal(coordinate.getGroupId(), dependency.group().value()) && Objects.equal(coordinate.getArtifactId(), dependency.name().value())) {
dependencyFound = true;
break;
}
}
}
if (!dependencyFound) {
missingLibraries.add(coordinate);
}
}
return missingLibraries;
}
use of com.android.ide.common.repository.GradleCoordinate in project android by JetBrains.
the class GradleDependencyManager method userWantToAddDependencies.
private static boolean userWantToAddDependencies(@NotNull Module module, @NotNull Collection<GradleCoordinate> missing) {
String libraryNames = StringUtil.join(missing, GradleCoordinate::getArtifactId, ", ");
String message = String.format("This operation requires the %1$s %2$s. \n\nWould you like to add %3$s %1$s now?", pluralize("library", missing.size()), libraryNames, pluralize("this", missing.size()));
Project project = module.getProject();
return Messages.showOkCancelDialog(project, message, "Add Project Dependency", Messages.getErrorIcon()) == Messages.OK;
}
use of com.android.ide.common.repository.GradleCoordinate in project android by JetBrains.
the class EclipseImportModule method initDependencies.
@Override
protected void initDependencies() {
super.initDependencies();
for (String reference : myProject.getInferredLibraries()) {
if (reference.equals(APPCOMPAT_ARTIFACT)) {
GradleCoordinate dependency = getAppCompatDependency();
if (dependency != null) {
myDependencies.add(dependency);
myImporter.getSummary().reportReplacedLib(reference, Collections.singletonList(dependency));
}
} else if (reference.equals(SUPPORT_ARTIFACT)) {
GradleCoordinate dependency = getSupportLibDependency();
if (dependency != null) {
myDependencies.add(dependency);
myImporter.getSummary().reportReplacedLib(reference, Collections.singletonList(dependency));
}
} else if (reference.equals(GRIDLAYOUT_ARTIFACT)) {
GradleCoordinate dependency = getGridLayoutDependency();
if (dependency != null) {
myDependencies.add(dependency);
myImporter.getSummary().reportReplacedLib(reference, Collections.singletonList(dependency));
}
} else if (reference.equals(MEDIA_ROUTER_ARTIFACT)) {
GradleCoordinate dependency = getMediaRouterDependency();
if (dependency != null) {
myDependencies.add(dependency);
myImporter.getSummary().reportReplacedLib(reference, Collections.singletonList(dependency));
}
}
}
for (File jar : myProject.getJarPaths()) {
if (myImporter.isReplaceJars()) {
GradleCoordinate dependency = guessDependency(jar);
if (dependency != null) {
myDependencies.add(dependency);
myImporter.getSummary().reportReplacedJar(jar, dependency);
continue;
}
}
myJarDependencies.add(getJarOutputRelativePath(jar));
}
for (File jar : myProject.getTestJarPaths()) {
if (myImporter.isReplaceJars()) {
GradleCoordinate dependency = guessDependency(jar);
if (dependency != null) {
myTestDependencies.add(dependency);
myImporter.getSummary().reportReplacedJar(jar, dependency);
continue;
}
}
// Test jars unconditionally get copied into the libs/ folder
myTestJarDependencies.add(getTestJarOutputRelativePath(jar));
}
}
use of com.android.ide.common.repository.GradleCoordinate in project android by JetBrains.
the class GradleFilePsiMerger method mergeDependencies.
private static void mergeDependencies(@NotNull PsiElement fromRoot, @NotNull PsiElement toRoot, @NotNull Project project, @Nullable String supportLibVersionFilter) {
Map<String, Multimap<String, GradleCoordinate>> dependencies = Maps.newHashMap();
final List<String> unparsedDependencies = Lists.newArrayList();
// Load existing dependencies into the map for the existing build.gradle
pullDependenciesIntoMap(toRoot, dependencies, null);
// Load dependencies into the map for the new build.gradle
pullDependenciesIntoMap(fromRoot, dependencies, unparsedDependencies);
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
RepositoryUrlManager urlManager = RepositoryUrlManager.get();
ImmutableList<String> configurations = CONFIGURATION_ORDERING.immutableSortedCopy(dependencies.keySet());
AndroidSdkData sdk = AndroidSdks.getInstance().tryToChooseAndroidSdk();
if (sdk != null) {
for (String configurationName : configurations) {
List<GradleCoordinate> resolved = urlManager.resolveDynamicSdkDependencies(dependencies.get(configurationName), supportLibVersionFilter, sdk, FileOpUtils.create());
for (GradleCoordinate dependency : resolved) {
PsiElement dependencyElement = factory.createStatementFromText(String.format("%s '%s'\n", configurationName, dependency.toString()));
toRoot.addBefore(dependencyElement, toRoot.getLastChild());
}
}
}
for (String dependency : unparsedDependencies) {
PsiElement dependencyElement = factory.createStatementFromText(dependency);
toRoot.addBefore(dependencyElement, toRoot.getLastChild());
}
}
Aggregations