use of com.android.tools.idea.gradle.dsl.model.GradleBuildModel in project android by JetBrains.
the class AndroidModelTest method testAddAndApplyBlockStatements.
public void testAddAndApplyBlockStatements() throws Exception {
String text = "android.defaultConfig.applicationId \"com.example.myapplication\"\n" + "android.defaultConfig.proguardFiles \"proguard-android.txt\", \"proguard-rules.pro\"";
writeToBuildFile(text);
GradleBuildModel buildModel = getGradleBuildModel();
AndroidModel android = buildModel.android();
assertNotNull(android);
ProductFlavorModel defaultConfig = android.defaultConfig();
assertEquals("applicationId", "com.example.myapplication", defaultConfig.applicationId());
assertEquals("proguardFiles", ImmutableList.of("proguard-android.txt", "proguard-rules.pro"), defaultConfig.proguardFiles());
defaultConfig.setDimension("abcd");
assertEquals("applicationId", "com.example.myapplication", defaultConfig.applicationId());
assertEquals("proguardFiles", ImmutableList.of("proguard-android.txt", "proguard-rules.pro"), defaultConfig.proguardFiles());
assertEquals("dimension", "abcd", defaultConfig.dimension());
applyChangesAndReparse(buildModel);
android = buildModel.android();
assertNotNull(android);
defaultConfig = android.defaultConfig();
assertEquals("applicationId", "com.example.myapplication", defaultConfig.applicationId());
assertEquals("proguardFiles", ImmutableList.of("proguard-android.txt", "proguard-rules.pro"), defaultConfig.proguardFiles());
assertEquals("dimension", "abcd", defaultConfig.dimension());
}
use of com.android.tools.idea.gradle.dsl.model.GradleBuildModel in project android by JetBrains.
the class AndroidModelTest method testReplaceAndResetListElements.
public void testReplaceAndResetListElements() throws Exception {
String text = "android { \n" + " flavorDimensions \"abi\", \"version\"\n" + "}";
writeToBuildFile(text);
GradleBuildModel buildModel = getGradleBuildModel();
AndroidModel android = buildModel.android();
assertNotNull(android);
assertEquals("flavorDimensions", ImmutableList.of("abi", "version"), android.flavorDimensions());
android.replaceFlavorDimension("abi", "xyz");
assertEquals("flavorDimensions", ImmutableList.of("xyz", "version"), android.flavorDimensions());
buildModel.resetState();
assertEquals("flavorDimensions", ImmutableList.of("abi", "version"), android.flavorDimensions());
}
use of com.android.tools.idea.gradle.dsl.model.GradleBuildModel in project android by JetBrains.
the class GradleSyncTest method aarSourceAttachments.
@Test
public void aarSourceAttachments() throws IOException {
guiTest.importSimpleApplication();
IdeFrameFixture ideFrame = guiTest.ideFrame();
Project project = ideFrame.getProject();
Module appModule = ideFrame.getModule("app");
execute(new GuiTask() {
@Override
protected void executeInEDT() throws Throwable {
runWriteCommandAction(project, () -> {
GradleBuildModel buildModel = GradleBuildModel.get(appModule);
String newDependency = "com.mapbox.mapboxsdk:mapbox-android-sdk:0.7.4@aar";
buildModel.dependencies().addArtifact(COMPILE, newDependency);
buildModel.applyChanges();
});
}
});
ideFrame.requestProjectSync().waitForGradleProjectSyncToFinish();
// Verify that the library has sources.
LibraryTable libraryTable = ProjectLibraryTable.getInstance(project);
String libraryName = "mapbox-android-sdk-0.7.4";
Library library = libraryTable.getLibraryByName(libraryName);
VirtualFile[] files = library.getFiles(SOURCES);
assertThat(files).asList().hasSize(1);
}
use of com.android.tools.idea.gradle.dsl.model.GradleBuildModel in project android by JetBrains.
the class GradleProjectDependencyParser method parse.
@NotNull
private static Set<String> parse(@NotNull VirtualFile moduleRoot, @NotNull Project project) {
VirtualFile buildFile = moduleRoot.findChild(FN_BUILD_GRADLE);
if (buildFile == null) {
return Collections.emptySet();
} else {
Set<String> result = Sets.newHashSet();
GradleBuildModel buildModel = parseBuildFile(buildFile, project);
DependenciesModel dependenciesModel = buildModel.dependencies();
if (dependenciesModel != null) {
for (ModuleDependencyModel dependency : dependenciesModel.modules()) {
String modulePath = dependency.path().value();
result.add(modulePath);
}
}
return result;
}
}
use of com.android.tools.idea.gradle.dsl.model.GradleBuildModel in project android by JetBrains.
the class InferSupportAnnotationsAction method checkModules.
// For Android we need to check SDK version and possibly update the gradle project file
protected boolean checkModules(@NotNull Project project, @NotNull AnalysisScope scope, @NotNull Map<Module, PsiFile> modules) {
Set<Module> modulesWithoutAnnotations = new HashSet<>();
Set<Module> modulesWithLowVersion = new HashSet<>();
for (Module module : modules.keySet()) {
AndroidModuleInfo info = AndroidModuleInfo.get(module);
if (info != null && info.getBuildSdkVersion() != null && info.getBuildSdkVersion().getFeatureLevel() < MIN_SDK_WITH_NULLABLE) {
modulesWithLowVersion.add(module);
}
GradleBuildModel buildModel = GradleBuildModel.get(module);
if (buildModel == null) {
Logger.getInstance(InferSupportAnnotationsAction.class).warn("Unable to find Gradle build model for module " + module.getModuleFilePath());
continue;
}
boolean dependencyFound = false;
DependenciesModel dependenciesModel = buildModel.dependencies();
if (dependenciesModel != null) {
for (ArtifactDependencyModel dependency : dependenciesModel.artifacts(COMPILE)) {
String notation = dependency.compactNotation().value();
if (notation.startsWith(SdkConstants.APPCOMPAT_LIB_ARTIFACT) || notation.startsWith(SdkConstants.SUPPORT_LIB_ARTIFACT) || notation.startsWith(SdkConstants.ANNOTATIONS_LIB_ARTIFACT)) {
dependencyFound = true;
break;
}
}
}
if (!dependencyFound) {
modulesWithoutAnnotations.add(module);
}
}
if (!modulesWithLowVersion.isEmpty()) {
Messages.showErrorDialog(project, String.format("Infer Support Annotations requires the project sdk level be set to %1$d or greater.", MIN_SDK_WITH_NULLABLE), "Infer Support Annotations");
return false;
}
if (modulesWithoutAnnotations.isEmpty()) {
return true;
}
String moduleNames = StringUtil.join(modulesWithoutAnnotations, Module::getName, ", ");
int count = modulesWithoutAnnotations.size();
String message = String.format("The %1$s %2$s %3$sn't refer to the existing '%4$s' library with Android nullity annotations. \n\n" + "Would you like to add the %5$s now?", pluralize("module", count), moduleNames, count > 1 ? "do" : "does", SupportLibrary.SUPPORT_ANNOTATIONS.getArtifactId(), pluralize("dependency", count));
if (Messages.showOkCancelDialog(project, message, "Infer Nullity Annotations", Messages.getErrorIcon()) == Messages.OK) {
LocalHistoryAction action = LocalHistory.getInstance().startAction(ADD_DEPENDENCY);
try {
new WriteCommandAction(project, ADD_DEPENDENCY) {
@Override
protected void run(@NotNull Result result) throws Throwable {
RepositoryUrlManager manager = RepositoryUrlManager.get();
String annotationsLibraryCoordinate = manager.getLibraryStringCoordinate(SupportLibrary.SUPPORT_ANNOTATIONS, true);
for (Module module : modulesWithoutAnnotations) {
addDependency(module, annotationsLibraryCoordinate);
}
GradleSyncInvoker.Request request = new GradleSyncInvoker.Request().setGenerateSourcesOnSuccess(false);
GradleSyncInvoker.getInstance().requestProjectSync(project, request, new GradleSyncListener.Adapter() {
@Override
public void syncSucceeded(@NotNull Project project) {
restartAnalysis(project, scope);
}
});
}
}.execute();
} finally {
action.finish();
}
}
return false;
}
Aggregations