Search in sources :

Example 26 with AndroidSdkData

use of org.jetbrains.android.sdk.AndroidSdkData in project android by JetBrains.

the class IdeSdks method createAndroidSdkPerAndroidTarget.

/**
   * Creates a set of IntelliJ SDKs (one for each build target) corresponding to the Android SDK in the given directory, if SDKs with the
   * default naming convention and each individual build target do not already exist. If IntelliJ SDKs do exist, they are not updated.
   */
@NotNull
private List<Sdk> createAndroidSdkPerAndroidTarget(@NotNull File androidSdkPath, @Nullable Sdk javaSdk) {
    AndroidSdkData sdkData = getSdkData(androidSdkPath);
    if (sdkData == null) {
        return Collections.emptyList();
    }
    IAndroidTarget[] targets = sdkData.getTargets(false);
    if (targets.length == 0) {
        return Collections.emptyList();
    }
    List<Sdk> sdks = new ArrayList<>();
    Sdk ideJdk = javaSdk != null ? javaSdk : getJdk();
    if (ideJdk != null) {
        for (IAndroidTarget target : targets) {
            if (target.isPlatform() && !doesIdeAndroidSdkExist(target)) {
                String name = myAndroidSdks.chooseNameForNewLibrary(target);
                Sdk sdk = myAndroidSdks.create(target, sdkData.getLocation(), name, ideJdk, true);
                if (sdk != null) {
                    sdks.add(sdk);
                }
            }
        }
    }
    return sdks;
}
Also used : AndroidSdkData(org.jetbrains.android.sdk.AndroidSdkData) ArrayList(java.util.ArrayList) IAndroidTarget(com.android.sdklib.IAndroidTarget) SdkPaths.validateAndroidSdk(com.android.tools.idea.sdk.SdkPaths.validateAndroidSdk) NotNull(org.jetbrains.annotations.NotNull)

Example 27 with AndroidSdkData

use of org.jetbrains.android.sdk.AndroidSdkData in project android by JetBrains.

the class IdeSdks method setAndroidSdkPath.

/**
   * Sets the path of Android Studio's Android SDK. This method should be called in a write action. It is assumed that the given path has
   * been validated by {@link #isValidAndroidSdkPath(File)}. This method will fail silently if the given path is not valid.
   *
   * @param path the path of the Android SDK.
   * @see com.intellij.openapi.application.Application#runWriteAction(Runnable)
   */
@NotNull
public List<Sdk> setAndroidSdkPath(@NotNull File path, @Nullable Sdk javaSdk, @Nullable Project currentProject) {
    if (isValidAndroidSdkPath(path)) {
        ApplicationManager.getApplication().assertWriteAccessAllowed();
        // given path as well in order to be able to fallback to it later if there is still no android sdk configured within the ide.
        if (currentProject != null && !currentProject.isDisposed()) {
            String sdkPath = toCanonicalPath(path.getAbsolutePath());
            PropertiesComponent.getInstance(currentProject).setValue(ANDROID_SDK_PATH_KEY, sdkPath);
            if (!currentProject.isDefault()) {
                // Store default sdk path for default project as well in order to be able to re-use it for another ide projects if necessary.
                PropertiesComponent component = PropertiesComponent.getInstance(ProjectManager.getInstance().getDefaultProject());
                component.setValue(ANDROID_SDK_PATH_KEY, sdkPath);
            }
        }
        // Since removing SDKs is *not* asynchronous, we force an update of the SDK Manager.
        // If we don't force this update, AndroidSdks will still use the old SDK until all SDKs are properly deleted.
        AndroidSdkData oldSdkData = getSdkData(path);
        myAndroidSdks.setSdkData(oldSdkData);
        // Set up a list of SDKs we don't need any more. At the end we'll delete them.
        List<Sdk> sdksToDelete = new ArrayList<>();
        File resolved = resolvePath(path);
        // Parse out the new SDK. We'll need its targets to set up IntelliJ SDKs for each.
        AndroidSdkData sdkData = getSdkData(resolved, true);
        if (sdkData != null) {
            // Iterate over all current existing IJ Android SDKs
            for (Sdk sdk : myAndroidSdks.getAllAndroidSdks()) {
                if (sdk.getName().startsWith(SDK_NAME_PREFIX)) {
                    sdksToDelete.add(sdk);
                }
            }
        }
        for (Sdk sdk : sdksToDelete) {
            ProjectJdkTable.getInstance().removeJdk(sdk);
        }
        // If there are any API targets that we haven't created IntelliJ SDKs for yet, fill those in.
        List<Sdk> sdks = createAndroidSdkPerAndroidTarget(resolved, javaSdk);
        afterAndroidSdkPathUpdate(resolved);
        return sdks;
    }
    return Collections.emptyList();
}
Also used : AndroidSdkData(org.jetbrains.android.sdk.AndroidSdkData) ArrayList(java.util.ArrayList) SdkPaths.validateAndroidSdk(com.android.tools.idea.sdk.SdkPaths.validateAndroidSdk) PropertiesComponent(com.intellij.ide.util.PropertiesComponent) File(java.io.File) NotNull(org.jetbrains.annotations.NotNull)

Example 28 with AndroidSdkData

use of org.jetbrains.android.sdk.AndroidSdkData in project android by JetBrains.

the class AndroidSdks method tryToCreate.

@Nullable
public Sdk tryToCreate(@NotNull File sdkPath, @NotNull String targetHashString) {
    AndroidSdkData sdkData = getSdkData(sdkPath);
    if (sdkData != null) {
        sdkData.getSdkHandler().getSdkManager(new StudioLoggerProgressIndicator(AndroidSdks.class)).markInvalid();
        IAndroidTarget target = sdkData.findTargetByHashString(targetHashString);
        if (target != null) {
            return create(target, sdkData.getLocation(), true);
        }
    }
    return null;
}
Also used : StudioLoggerProgressIndicator(com.android.tools.idea.sdk.progress.StudioLoggerProgressIndicator) AndroidSdkData(org.jetbrains.android.sdk.AndroidSdkData) IAndroidTarget(com.android.sdklib.IAndroidTarget) Nullable(org.jetbrains.annotations.Nullable)

Example 29 with AndroidSdkData

use of org.jetbrains.android.sdk.AndroidSdkData in project android by JetBrains.

the class RepositoryUrlManager method getLibraryStringCoordinate.

@Nullable
public String getLibraryStringCoordinate(SupportLibrary library, boolean preview) {
    AndroidSdkData sdk = AndroidSdks.getInstance().tryToChooseAndroidSdk();
    if (sdk == null) {
        return null;
    }
    String revision = getLibraryRevision(library.getGroupId(), library.getArtifactId(), null, preview, sdk.getLocation(), FileOpUtils.create());
    if (revision == null) {
        return null;
    }
    return library.getGradleCoordinate(revision).toString();
}
Also used : AndroidSdkData(org.jetbrains.android.sdk.AndroidSdkData) Nullable(org.jetbrains.annotations.Nullable)

Example 30 with AndroidSdkData

use of org.jetbrains.android.sdk.AndroidSdkData in project android by JetBrains.

the class TemplateUtils method getKnownVersions.

/**
   * Returns a list of known API names
   *
   * @return a list of string API names, starting from 1 and up through the
   * maximum known versions (with no gaps)
   */
@NotNull
public static String[] getKnownVersions() {
    final AndroidSdkData sdkData = AndroidSdks.getInstance().tryToChooseAndroidSdk();
    assert sdkData != null;
    int max = SdkVersionInfo.HIGHEST_KNOWN_STABLE_API;
    IAndroidTarget[] targets = sdkData.getTargets();
    SparseArray<IAndroidTarget> apiTargets = null;
    for (IAndroidTarget target : targets) {
        if (target.isPlatform()) {
            AndroidVersion version = target.getVersion();
            if (!version.isPreview()) {
                int apiLevel = version.getApiLevel();
                max = Math.max(max, apiLevel);
                if (apiLevel > SdkVersionInfo.HIGHEST_KNOWN_API) {
                    if (apiTargets == null) {
                        apiTargets = new SparseArray<IAndroidTarget>();
                    }
                    apiTargets.put(apiLevel, target);
                }
            }
        }
    }
    String[] versions = new String[max];
    for (int api = 1; api <= max; api++) {
        String name = SdkVersionInfo.getAndroidName(api);
        // noinspection ConstantConditions
        if (name == null) {
            if (apiTargets != null) {
                IAndroidTarget target = apiTargets.get(api);
                if (target != null) {
                    name = AndroidSdkUtils.getTargetLabel(target);
                }
            }
            if (name == null) {
                name = String.format("API %1$d", api);
            }
        }
        versions[api - 1] = name;
    }
    return versions;
}
Also used : AndroidSdkData(org.jetbrains.android.sdk.AndroidSdkData) IAndroidTarget(com.android.sdklib.IAndroidTarget) AndroidVersion(com.android.sdklib.AndroidVersion) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

AndroidSdkData (org.jetbrains.android.sdk.AndroidSdkData)39 IAndroidTarget (com.android.sdklib.IAndroidTarget)15 File (java.io.File)12 VirtualFile (com.intellij.openapi.vfs.VirtualFile)11 Sdk (com.intellij.openapi.projectRoots.Sdk)9 NotNull (org.jetbrains.annotations.NotNull)7 AndroidSdkHandler (com.android.sdklib.repository.AndroidSdkHandler)6 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)6 Nullable (org.jetbrains.annotations.Nullable)6 StudioLoggerProgressIndicator (com.android.tools.idea.sdk.progress.StudioLoggerProgressIndicator)5 AndroidSdkAdditionalData (org.jetbrains.android.sdk.AndroidSdkAdditionalData)5 VfsUtilCore.virtualToIoFile (com.intellij.openapi.vfs.VfsUtilCore.virtualToIoFile)4 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 AndroidVersion (com.android.sdklib.AndroidVersion)3 NewProjectWizardState (com.android.tools.idea.npw.NewProjectWizardState)3 SdkPaths.validateAndroidSdk (com.android.tools.idea.sdk.SdkPaths.validateAndroidSdk)3 TemplateWizardState (com.android.tools.idea.wizard.template.TemplateWizardState)3 AndroidPlatform (org.jetbrains.android.sdk.AndroidPlatform)3 ProgressIndicator (com.android.repository.api.ProgressIndicator)2