use of com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencySpec in project android by JetBrains.
the class AndroidGradleJavaProjectModelModifier method findNewExternalDependencyByExaminingPath.
/**
* Gradle dependencies are stored in following path: xxx/:groupId/:artifactId/:version/xxx/:artifactId-:version.jar
* therefor, if we can't get the artifact information from model, then try to extract from path.
*/
@Nullable
private static ArtifactDependencySpec findNewExternalDependencyByExaminingPath(@NotNull Library library) {
VirtualFile[] files = library.getFiles(OrderRootType.CLASSES);
if (files.length == 0) {
return null;
}
File file = virtualToIoFile(files[0]);
String libraryName = library.getName();
if (libraryName == null) {
return null;
}
List<String> pathSegments = splitPath(file.getPath());
for (int i = 1; i < pathSegments.size() - 2; i++) {
if (libraryName.startsWith(pathSegments.get(i))) {
String groupId = pathSegments.get(i - 1);
String artifactId = pathSegments.get(i);
String version = pathSegments.get(i + 1);
if (libraryName.endsWith(version)) {
return new ArtifactDependencySpec(artifactId, groupId, version);
}
}
}
return null;
}
use of com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencySpec in project android by JetBrains.
the class AndroidGradleJavaProjectModelModifier method findNewExternalDependency.
@Nullable
private static ArtifactDependencySpec findNewExternalDependency(@NotNull Library library, @NotNull AndroidModuleModel androidModel) {
GradleVersion modelVersion = androidModel.getModelVersion();
JavaLibrary matchedLibrary = null;
for (BaseArtifact testArtifact : androidModel.getTestArtifactsInSelectedVariant()) {
matchedLibrary = findMatchedLibrary(library, testArtifact, modelVersion);
if (matchedLibrary != null) {
break;
}
}
if (matchedLibrary == null) {
Variant selectedVariant = androidModel.getSelectedVariant();
matchedLibrary = findMatchedLibrary(library, selectedVariant.getMainArtifact(), modelVersion);
}
if (matchedLibrary == null) {
return null;
}
// TODO use getRequestedCoordinates once the interface is fixed.
MavenCoordinates coordinates = matchedLibrary.getResolvedCoordinates();
if (coordinates == null) {
return null;
}
return new ArtifactDependencySpec(coordinates.getArtifactId(), coordinates.getGroupId(), coordinates.getVersion());
}
use of com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencySpec in project android by JetBrains.
the class GradleOperations method removeDependencies.
@Override
public void removeDependencies(@NotNull Module module, @NotNull DeveloperServiceMetadata metadata) {
final GradleBuildModel buildModel = GradleBuildModel.get(module);
if (buildModel != null) {
boolean dependenciesChanged = false;
DependenciesModel dependenciesModel = buildModel.dependencies();
if (dependenciesModel != null) {
for (ArtifactDependencyModel dependency : dependenciesModel.artifacts()) {
ArtifactDependencySpec spec = ArtifactDependencySpec.create(dependency);
for (String dependencyValue : metadata.getDependencies()) {
if (spec.equals(ArtifactDependencySpec.create(dependencyValue))) {
dependenciesModel.remove(dependency);
dependenciesChanged = true;
break;
}
}
}
}
final Project project = module.getProject();
if (dependenciesChanged) {
new WriteCommandAction.Simple(project, "Uninstall " + metadata.getName()) {
@Override
public void run() {
buildModel.applyChanges();
}
}.execute();
}
GradleSyncInvoker.getInstance().requestProjectSyncAndSourceGeneration(project, null);
}
}
use of com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencySpec in project android by JetBrains.
the class AddGradleDependencyTest method testAddLibDependencyDeclaredInJavaProject.
@RunIn(TestGroup.UNRELIABLE)
@Test
public void testAddLibDependencyDeclaredInJavaProject() throws IOException {
guiTest.importProjectAndWaitForProjectSyncToFinish("MultiModule");
GradleBuildModelFixture library3BuildModel = guiTest.ideFrame().parseBuildFileForModule("library3");
ArtifactDependencySpec gson = new ArtifactDependencySpec("gson", "com.google.code.gson", "2.4");
library3BuildModel.getTarget().dependencies().addArtifact(COMPILE, gson);
library3BuildModel.applyChanges();
guiTest.ideFrame().requestProjectSync().waitForGradleProjectSyncToFinish();
EditorFixture editor = guiTest.ideFrame().getEditor().open("app/src/main/java/com/android/multimodule/MainActivity.java");
String classToImport = "com.google.gson.Gson";
addImport(editor, classToImport);
editor.waitForCodeAnalysisHighlightCount(ERROR, 1);
moveCaretToClassName(editor, classToImport);
editor.invokeQuickfixAction("Add library 'gson-2.4' to classpath");
guiTest.ideFrame().waitForGradleProjectSyncToFinish();
editor.waitForCodeAnalysisHighlightCount(ERROR, 0);
GradleBuildModelFixture appBuildModel = guiTest.ideFrame().parseBuildFileForModule("app");
appBuildModel.requireDependency(COMPILE, gson);
verifyUndo(editor, 1);
}
use of com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencySpec in project android by JetBrains.
the class AddGradleDependencyTest method testAddLibDependencyDeclaredInAndroidProject.
@RunIn(TestGroup.UNRELIABLE)
@Test
public void testAddLibDependencyDeclaredInAndroidProject() throws IOException {
guiTest.importProjectAndWaitForProjectSyncToFinish("MultiModule");
GradleBuildModelFixture appBuildModel = guiTest.ideFrame().parseBuildFileForModule("app");
ArtifactDependencySpec gson = new ArtifactDependencySpec("gson", "com.google.code.gson", "2.4");
appBuildModel.getTarget().dependencies().addArtifact(COMPILE, gson);
appBuildModel.applyChanges();
guiTest.ideFrame().requestProjectSync().waitForGradleProjectSyncToFinish();
EditorFixture editor = guiTest.ideFrame().getEditor().open("library3/src/main/java/com/example/MyLibrary.java");
String classToImport = "com.google.gson.Gson";
addImport(editor, classToImport);
editor.waitForCodeAnalysisHighlightCount(ERROR, 1);
moveCaretToClassName(editor, classToImport);
editor.invokeQuickfixAction("Add library 'gson-2.4' to classpath");
guiTest.ideFrame().waitForGradleProjectSyncToFinish();
editor.waitForCodeAnalysisHighlightCount(ERROR, 0);
verifyUndo(editor, 1);
}
Aggregations