Search in sources :

Example 11 with LocalProperties

use of com.android.tools.idea.gradle.util.LocalProperties in project android by JetBrains.

the class GradleProjectSyncData method needsAndroidSdkSync.

private static boolean needsAndroidSdkSync(@NotNull final Project project) {
    if (IdeInfo.getInstance().isAndroidStudio()) {
        final File ideSdkPath = IdeSdks.getInstance().getAndroidSdkPath();
        if (ideSdkPath != null) {
            try {
                LocalProperties localProperties = new LocalProperties(project);
                File projectSdkPath = localProperties.getAndroidSdkPath();
                return projectSdkPath == null || !filesEqual(ideSdkPath, projectSdkPath);
            } catch (IOException ignored) {
            }
        }
        return true;
    }
    return false;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) VfsUtilCore.virtualToIoFile(com.intellij.openapi.vfs.VfsUtilCore.virtualToIoFile) VfsUtil.findFileByIoFile(com.intellij.openapi.vfs.VfsUtil.findFileByIoFile) LocalProperties(com.android.tools.idea.gradle.util.LocalProperties)

Example 12 with LocalProperties

use of com.android.tools.idea.gradle.util.LocalProperties in project android by JetBrains.

the class GradleBuildInvoker method executeTasks.

public void executeTasks(@NotNull List<String> gradleTasks, @NotNull List<String> commandLineArguments) {
    List<String> jvmArguments = new ArrayList<>();
    if (ApplicationManager.getApplication().isUnitTestMode()) {
        // Projects in tests may not have a local.properties, set ANDROID_HOME JVM argument if that's the case.
        LocalProperties localProperties;
        try {
            localProperties = new LocalProperties(myProject);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        if (localProperties.getAndroidSdkPath() == null) {
            File androidHomePath = IdeSdks.getInstance().getAndroidSdkPath();
            // In Android Studio, the Android SDK home path will never be null. It may be null when running in IDEA.
            if (androidHomePath != null) {
                jvmArguments.add(AndroidGradleSettings.createAndroidHomeJvmArg(androidHomePath.getPath()));
            }
        }
    }
    Request request = new Request(myProject, gradleTasks);
    // @formatter:off
    request.setJvmArguments(jvmArguments).setCommandLineArguments(commandLineArguments);
    // @formatter:on
    executeTasks(request);
}
Also used : IOException(java.io.IOException) LocalProperties(com.android.tools.idea.gradle.util.LocalProperties) File(java.io.File)

Example 13 with LocalProperties

use of com.android.tools.idea.gradle.util.LocalProperties in project android by JetBrains.

the class GradleAndroidSdkEventListener method afterSdkPathChange.

/**
   * Updates the path of the SDK manager in the project's local.properties file, only if the new path and the path in the file are
   * different. This method will request a project sync, unless an error ocurred when updating the local.properties file.
   *
   * @param sdkPath the new Android SDK path.
   * @param project one of the projects currently open in the IDE.
   */
@Override
public void afterSdkPathChange(@NotNull File sdkPath, @NotNull Project project) {
    if (!isBuildWithGradle(project)) {
        return;
    }
    LocalProperties localProperties;
    try {
        localProperties = new LocalProperties(project);
    } catch (IOException e) {
        // Exception thrown when local.properties file exists but cannot be read (e.g. no writing permissions.)
        logAndShowErrorWhenUpdatingLocalProperties(project, e, "read", sdkPath);
        return;
    }
    if (!filesEqual(sdkPath, localProperties.getAndroidSdkPath())) {
        try {
            localProperties.setAndroidSdkPath(sdkPath);
            localProperties.save();
        } catch (IOException e) {
            logAndShowErrorWhenUpdatingLocalProperties(project, e, "update", sdkPath);
            return;
        }
    }
    if (!ApplicationManager.getApplication().isUnitTestMode()) {
        GradleSyncInvoker.getInstance().requestProjectSyncAndSourceGeneration(project, null);
    }
}
Also used : IOException(java.io.IOException) LocalProperties(com.android.tools.idea.gradle.util.LocalProperties)

Example 14 with LocalProperties

use of com.android.tools.idea.gradle.util.LocalProperties in project android by JetBrains.

the class InstallNdkHyperlink method setNdkPath.

private static boolean setNdkPath(@NotNull Project project, @Nullable String ndkPath) {
    LocalProperties localProperties;
    try {
        localProperties = new LocalProperties(project);
    } catch (IOException e) {
        String msg = String.format("Unable to read local.properties file of Project '%1$s':\n%2$s", project.getName(), e.getMessage());
        Messages.showErrorDialog(project, msg, ERROR_TITLE);
        return false;
    }
    try {
        localProperties.setAndroidNdkPath(ndkPath == null ? null : new File(ndkPath));
        localProperties.save();
    } catch (IOException e) {
        String msg = String.format("Unable to save local.properties file of Project '%1$s: %2$s", localProperties.getPropertiesFilePath().getPath(), e.getMessage());
        Messages.showErrorDialog(project, msg, ERROR_TITLE);
        return false;
    }
    return true;
}
Also used : IOException(java.io.IOException) LocalProperties(com.android.tools.idea.gradle.util.LocalProperties) File(java.io.File)

Example 15 with LocalProperties

use of com.android.tools.idea.gradle.util.LocalProperties in project android by JetBrains.

the class AndroidRunSdkToolAction method doAction.

public void doAction(@NotNull Project project) {
    if (IdeInfo.getInstance().isAndroidStudio()) {
        File androidHome = IdeSdks.getInstance().getAndroidSdkPath();
        if (androidHome != null) {
            doRunTool(project, androidHome.getPath());
            return;
        }
    }
    // Gradle project.
    try {
        LocalProperties localProperties = new LocalProperties(project);
        File androidSdkPath = localProperties.getAndroidSdkPath();
        if (androidSdkPath != null) {
            doRunTool(project, androidSdkPath.getPath());
            return;
        }
    } catch (IOException ignored) {
        LOG.info(String.format("Unable to read local.properties file from project '%1$s'", project.getName()), ignored);
    }
    List<AndroidFacet> facets = ProjectFacetManager.getInstance(project).getFacets(AndroidFacet.ID);
    assert facets.size() > 0;
    Set<String> sdkSet = new HashSet<>();
    for (AndroidFacet facet : facets) {
        AndroidSdkData sdkData = facet.getConfiguration().getAndroidSdk();
        if (sdkData != null) {
            sdkSet.add(sdkData.getLocation().getPath());
        }
    }
    if (sdkSet.size() == 0) {
        Messages.showErrorDialog(project, AndroidBundle.message("specify.platform.error"), CommonBundle.getErrorTitle());
        return;
    }
    String sdkPath = sdkSet.iterator().next();
    if (sdkSet.size() > 1) {
        String[] sdks = ArrayUtil.toStringArray(sdkSet);
        int index = Messages.showChooseDialog(project, AndroidBundle.message("android.choose.sdk.label"), AndroidBundle.message("android.choose.sdk.title"), Messages.getQuestionIcon(), sdks, sdkPath);
        if (index < 0) {
            return;
        }
        sdkPath = sdks[index];
    }
    doRunTool(project, sdkPath);
}
Also used : AndroidSdkData(org.jetbrains.android.sdk.AndroidSdkData) IOException(java.io.IOException) File(java.io.File) LocalProperties(com.android.tools.idea.gradle.util.LocalProperties) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) HashSet(com.intellij.util.containers.HashSet)

Aggregations

LocalProperties (com.android.tools.idea.gradle.util.LocalProperties)17 File (java.io.File)8 IOException (java.io.IOException)6 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 VfsUtil.findFileByIoFile (com.intellij.openapi.vfs.VfsUtil.findFileByIoFile)2 VfsUtilCore.virtualToIoFile (com.intellij.openapi.vfs.VfsUtilCore.virtualToIoFile)2 HashSet (com.intellij.util.containers.HashSet)2 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)2 AndroidSdkData (org.jetbrains.android.sdk.AndroidSdkData)2 TestUtils.getWorkspaceFile (com.android.testutils.TestUtils.getWorkspaceFile)1 GradleBuildFile (com.android.tools.idea.gradle.parser.GradleBuildFile)1 GradleUtil.getGradleBuildFile (com.android.tools.idea.gradle.util.GradleUtil.getGradleBuildFile)1 IdeSdks (com.android.tools.idea.sdk.IdeSdks)1 ExternalSystemException (com.intellij.openapi.externalSystem.model.ExternalSystemException)1 Project (com.intellij.openapi.project.Project)1 Sdk (com.intellij.openapi.projectRoots.Sdk)1 PsiFile (com.intellij.psi.PsiFile)1 List (java.util.List)1 NotNull (org.jetbrains.annotations.NotNull)1 Test (org.junit.Test)1