Search in sources :

Example 46 with GradleVersion

use of com.android.ide.common.repository.GradleVersion in project android by JetBrains.

the class ChooseActivityTypeStep method determineGradlePluginVersion.

/**
   * Find the most appropriated Gradle Plugin version for the specified project.
   * @param project If {@code null} (ie we are creating a new project) returns the recommended gradle version.
   */
@NotNull
private static String determineGradlePluginVersion(@Nullable Project project) {
    String defaultGradleVersion = AndroidPluginGeneration.ORIGINAL.getLatestKnownVersion();
    if (project == null) {
        return defaultGradleVersion;
    }
    GradleVersion versionInUse = GradleUtil.getAndroidGradleModelVersionInUse(project);
    if (versionInUse != null) {
        return versionInUse.toString();
    }
    AndroidPluginInfo androidPluginInfo = AndroidPluginInfo.searchInBuildFilesOnly(project);
    GradleVersion pluginVersion = (androidPluginInfo == null) ? null : androidPluginInfo.getPluginVersion();
    return (pluginVersion == null) ? defaultGradleVersion : pluginVersion.toString();
}
Also used : AndroidPluginInfo(com.android.tools.idea.gradle.plugin.AndroidPluginInfo) GradleVersion(com.android.ide.common.repository.GradleVersion) NotNull(org.jetbrains.annotations.NotNull)

Example 47 with GradleVersion

use of com.android.ide.common.repository.GradleVersion in project android by JetBrains.

the class BuildSignedApkTest method openAndSignUsingV1AndV2.

@Test
public void openAndSignUsingV1AndV2() throws IOException {
    GradleVersion latestVersion = GradleVersion.parse(SdkConstants.GRADLE_PLUGIN_LATEST_VERSION);
    assume().that(latestVersion.compareIgnoringQualifiers(GradleSignStep.MIN_SIGNATURE_SELECTION_VERSION)).isAtLeast(0);
    File jksFile = new File(myTemporaryFolder.getRoot(), "jks");
    File dstFolder = myTemporaryFolder.newFolder("dst");
    guiTest.importSimpleApplication().openFromMenu(BuildSignedApkDialogKeystoreStepFixture::find, "Build", "Generate Signed APK...").createNew().keyStorePath(jksFile.getAbsolutePath()).password("passwd").passwordConfirm("passwd").alias("key").keyPassword("passwd2").keyPasswordConfirm("passwd2").validity("3").firstAndLastName("Android Studio").organizationalUnit("Android").organization("Google").cityOrLocality("Mountain View").stateOrProvince("California").countryCode("US").clickOk().keyStorePassword("passwd").keyAlias("key").keyPassword("passwd2").clickNext().apkDestinationFolder(dstFolder.getAbsolutePath()).setV1SignatureEnabled(true).setV2SignatureEnabled(true).clickFinish();
    // We should verify that a V2 signature is present, but that is hard to do here.
    File[] apks = dstFolder.listFiles();
    assertThat(apks).hasLength(1);
    File apk = apks[0];
    try (ZipFile zf = new ZipFile(apk)) {
        assertThat(zf.getEntry("META-INF/CERT.SF")).isNotNull();
    }
}
Also used : ZipFile(java.util.zip.ZipFile) GradleVersion(com.android.ide.common.repository.GradleVersion) File(java.io.File) ZipFile(java.util.zip.ZipFile) Test(org.junit.Test)

Example 48 with GradleVersion

use of com.android.ide.common.repository.GradleVersion in project android by JetBrains.

the class ExternalRepository method doRefreshFor.

private void doRefreshFor(@NotNull final String groupId, @NotNull final String artifactId) {
    String url = String.format(URL_TEMPLATE, groupId.replaceAll("\\.", "/"), artifactId);
    Document document;
    try {
        document = JDOMUtil.loadDocument(URI.create(url).toURL());
    } catch (JDOMException e) {
        LOG.warn(String.format("Unexpected exception occurred on attempt to parse document from %s (checking the latest version " + "for artifact '%s:%s')", url, groupId, artifactId));
        return;
    } catch (IOException e) {
        LOG.warn(String.format("Unexpected I/O exception occurred on attempt to check the latest version for artifact '%s:%s' at " + "external repository (url %s)", groupId, artifactId, url));
        return;
    }
    Element versioning = document.getRootElement().getChild(MAVEN_METADATA_VERSIONING);
    if (versioning == null) {
        LOG.warn(String.format("Can't check the latest version for artifact '%s:%s'. Reason: artifact metadata info downloaded from " + "%s has unknown format - expected to find a <%s> element under a root element but it's not there", groupId, artifactId, url, MAVEN_METADATA_VERSIONING));
        return;
    }
    Element latest = versioning.getChild(MAVEN_METADATA_LATEST);
    if (latest == null) {
        LOG.warn(String.format("Can't check the latest version for artifact '%s:%s'. Reason: artifact metadata info downloaded from " + "%s has unknown format - expected to find a <%s> element under a <%s> element but it's not there", groupId, artifactId, url, MAVEN_METADATA_LATEST, MAVEN_METADATA_VERSIONING));
        return;
    }
    try {
        GradleVersion version = GradleVersion.parse(latest.getText());
        myLatestVersionCache.put(Pair.create(groupId, artifactId), version);
    } catch (NumberFormatException e) {
        LOG.warn(String.format("Can't check the latest version for artifact '%s:%s'. Reason: artifact metadata info downloaded from " + "%s has unknown version format - '%s'", groupId, artifactId, url, latest.getText()));
    }
}
Also used : Element(org.jdom.Element) IOException(java.io.IOException) Document(org.jdom.Document) JDOMException(org.jdom.JDOMException) GradleVersion(com.android.ide.common.repository.GradleVersion)

Example 49 with GradleVersion

use of com.android.ide.common.repository.GradleVersion in project android by JetBrains.

the class PostSyncProjectSetup method attemptToGenerateSources.

private void attemptToGenerateSources(@NotNull Request request) {
    if (!request.isGenerateSourcesAfterSync()) {
        return;
    }
    boolean cleanProjectAfterSync = request.isCleanProjectAfterSync();
    if (!cleanProjectAfterSync) {
        // Figure out if the plugin version changed. If it did, force a clean.
        // See: https://code.google.com/p/android/issues/detail?id=216616
        Map<String, GradleVersion> previousPluginVersionsPerModule = getPluginVersionsPerModule(myProject);
        storePluginVersionsPerModule(myProject);
        if (previousPluginVersionsPerModule != null && !previousPluginVersionsPerModule.isEmpty()) {
            Map<String, GradleVersion> currentPluginVersionsPerModule = getPluginVersionsPerModule(myProject);
            assert currentPluginVersionsPerModule != null;
            for (Map.Entry<String, GradleVersion> entry : currentPluginVersionsPerModule.entrySet()) {
                String modulePath = entry.getKey();
                GradleVersion previous = previousPluginVersionsPerModule.get(modulePath);
                if (previous == null || entry.getValue().compareTo(previous) != 0) {
                    cleanProjectAfterSync = true;
                    break;
                }
            }
        }
    }
    myProjectBuilder.generateSourcesOnly(cleanProjectAfterSync);
}
Also used : GradleVersion(com.android.ide.common.repository.GradleVersion) Map(java.util.Map)

Example 50 with GradleVersion

use of com.android.ide.common.repository.GradleVersion in project android by JetBrains.

the class ForcedPluginPreviewVersionUpgradeStep method checkAndPerformUpgrade.

@Override
public boolean checkAndPerformUpgrade(@NotNull Project project, @NotNull AndroidPluginInfo pluginInfo) {
    AndroidPluginGeneration pluginGeneration = pluginInfo.getPluginGeneration();
    GradleVersion recommended = GradleVersion.parse(pluginGeneration.getLatestKnownVersion());
    if (!shouldPreviewBeForcedToUpgradePluginVersion(recommended.toString(), pluginInfo.getPluginVersion())) {
        return false;
    }
    GradleSyncState syncState = GradleSyncState.getInstance(project);
    // Update the sync state before starting a new one.
    syncState.syncEnded();
    boolean userAcceptsForcedUpgrade = new ForcedPluginPreviewVersionUpgradeDialog(project, pluginInfo).showAndGet();
    if (userAcceptsForcedUpgrade) {
        AndroidPluginVersionUpdater versionUpdater = AndroidPluginVersionUpdater.getInstance(project);
        versionUpdater.updatePluginVersionAndSync(recommended, GradleVersion.parse(GRADLE_LATEST_VERSION), true);
    } else {
        String[] text = { "The project is using an incompatible version of the " + pluginGeneration.getDescription() + ".", "Please update your project to use version " + pluginGeneration.getLatestKnownVersion() + "." };
        SyncMessage msg = new SyncMessage(SyncMessage.DEFAULT_GROUP, ERROR, text);
        String pluginName = AndroidPluginGeneration.getGroupId() + GRADLE_PATH_SEPARATOR + pluginGeneration.getArtifactId();
        NotificationHyperlink quickFix = new SearchInBuildFilesHyperlink(pluginName);
        msg.add(quickFix);
        SyncMessages.getInstance(project).report(msg);
        syncState.invalidateLastSync("Force plugin upgrade declined");
    }
    return true;
}
Also used : SyncMessage(com.android.tools.idea.gradle.project.sync.messages.SyncMessage) SearchInBuildFilesHyperlink(com.android.tools.idea.gradle.project.sync.hyperlink.SearchInBuildFilesHyperlink) GradleVersion(com.android.ide.common.repository.GradleVersion) AndroidPluginGeneration(com.android.tools.idea.gradle.plugin.AndroidPluginGeneration) AndroidPluginVersionUpdater(com.android.tools.idea.gradle.plugin.AndroidPluginVersionUpdater) GradleSyncState(com.android.tools.idea.gradle.project.sync.GradleSyncState) NotificationHyperlink(com.android.tools.idea.gradle.project.sync.hyperlink.NotificationHyperlink)

Aggregations

GradleVersion (com.android.ide.common.repository.GradleVersion)77 File (java.io.File)25 Test (org.junit.Test)16 Module (com.intellij.openapi.module.Module)12 AndroidModuleModel (com.android.tools.idea.gradle.project.model.AndroidModuleModel)11 Nullable (org.jetbrains.annotations.Nullable)10 Project (com.intellij.openapi.project.Project)6 NotNull (org.jetbrains.annotations.NotNull)6 ArtifactDependencyModel (com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyModel)5 IOException (java.io.IOException)5 AndroidPluginInfo (com.android.tools.idea.gradle.plugin.AndroidPluginInfo)4 FixAndroidGradlePluginVersionHyperlink (com.android.tools.idea.gradle.project.sync.hyperlink.FixAndroidGradlePluginVersionHyperlink)3 NotificationHyperlink (com.android.tools.idea.gradle.project.sync.hyperlink.NotificationHyperlink)3 SyncMessage (com.android.tools.idea.gradle.project.sync.messages.SyncMessage)3 ZipFile (java.util.zip.ZipFile)3 VisibleForTesting (com.android.annotations.VisibleForTesting)2 Variant (com.android.builder.model.Variant)2 IDevice (com.android.ddmlib.IDevice)2 Revision (com.android.repository.Revision)2 GradleBuildModel (com.android.tools.idea.gradle.dsl.model.GradleBuildModel)2