diff options
author | Yuriy Solodkyy <solodkyy@google.com> | 2021-07-19 18:54:46 +0100 |
---|---|---|
committer | Yuriy Solodkyy <solodkyy@google.com> | 2021-07-19 18:55:48 +0100 |
commit | 59b906078160c6f8e2b901e7e1132fa2f7dc3153 (patch) | |
tree | 2b535a9b6d6560ffa3d1faafba658e4d24ad2ed8 | |
parent | a642098908c25acd35ec4f76a6ff6afd275905ce (diff) | |
download | appindexing-59b906078160c6f8e2b901e7e1132fa2f7dc3153.tar.gz |
Fix getting APK location for app indexing testing
With AGP 4.x+ `IdeAndroidArtifact.outputs` property is not populated
as it is not supported by the newer AGP versions. With this change we
retrieve the location via a helper method in `GradleProjectSystem`.
Unlike the old implementation we do not return the last listed APK in
the case when multiple APKs are produced and instead fail the operation.
Bug: 194058522
Bug: 147586218
Bug: 37797956
Test: n/a (manually tested that APK is located but network error on upload)
Change-Id: I3788d1c20b6b220f80b67ea9851d537410a704f8
-rw-r--r-- | BUILD | 1 | ||||
-rw-r--r-- | google-appindexing.iml | 1 | ||||
-rw-r--r-- | src/com/google/appindexing/fetchasgoogle/FetchAsGoogleTask.java | 63 |
3 files changed, 22 insertions, 43 deletions
@@ -29,6 +29,7 @@ iml_module( "//tools/adt/idea/.idea/libraries:studio-analytics-proto", "//tools/adt/idea/deploy:intellij.android.deploy[module]", "//tools/adt/idea/gradle-dsl:intellij.android.gradle.dsl[module]", + "//tools/adt/idea/project-system-gradle:intellij.android.projectSystem.gradle[module]", "//tools/adt/idea/project-system-gradle-models:intellij.android.projectSystem.gradle.models[module]", ], ) diff --git a/google-appindexing.iml b/google-appindexing.iml index 26594f5..b8a5682 100644 --- a/google-appindexing.iml +++ b/google-appindexing.iml @@ -34,6 +34,7 @@ <orderEntry type="library" name="studio-analytics-proto" level="project" /> <orderEntry type="module" module-name="intellij.android.deploy" /> <orderEntry type="module" module-name="intellij.android.gradle.dsl" /> + <orderEntry type="module" module-name="intellij.android.projectSystem.gradle" /> <orderEntry type="module" module-name="intellij.android.projectSystem.gradle.models" /> </component> </module>
\ No newline at end of file diff --git a/src/com/google/appindexing/fetchasgoogle/FetchAsGoogleTask.java b/src/com/google/appindexing/fetchasgoogle/FetchAsGoogleTask.java index a7729bf..c033dd3 100644 --- a/src/com/google/appindexing/fetchasgoogle/FetchAsGoogleTask.java +++ b/src/com/google/appindexing/fetchasgoogle/FetchAsGoogleTask.java @@ -15,24 +15,25 @@ */ package com.google.appindexing.fetchasgoogle; +import static com.android.tools.idea.projectsystem.gradle.GradleProjectSystemKt.getBuiltApksForSelectedVariant; import static com.google.wireless.android.sdk.stats.GradleSyncStats.Trigger.TRIGGER_BUILD_BEFORE_BUILDING_APK; import com.android.annotations.VisibleForTesting; -import com.android.tools.idea.gradle.model.IdeAndroidArtifact; -import com.android.tools.idea.gradle.model.IdeAndroidArtifactOutput; +import com.android.tools.idea.gradle.project.build.invoker.AssembleInvocationResult; import com.android.tools.idea.gradle.project.build.invoker.GradleBuildInvoker; -import com.android.tools.idea.gradle.project.build.invoker.GradleInvocationResult; import com.android.tools.idea.gradle.project.build.invoker.TestCompileType; -import com.android.tools.idea.gradle.project.model.AndroidModuleModel; import com.android.tools.idea.gradle.project.sync.GradleSyncInvoker; import com.android.tools.idea.gradle.project.sync.GradleSyncListener; import com.android.tools.idea.gradle.project.sync.GradleSyncState; import com.android.tools.idea.model.AndroidModel; +import com.android.tools.idea.run.ApkInfo; import com.google.api.services.fetchasgoogle_pa.model.ApkHolder; import com.google.api.services.fetchasgoogle_pa.model.FetchResponse; import com.google.appindexing.editor.AppIndexingVirtualFile; import com.google.appindexing.fetchasgoogle.FetchAsGoogleClient.FetchAsGoogleException; import com.google.appindexing.util.AppIndexingBundle; +import com.google.common.util.concurrent.ListenableFuture; +import com.intellij.openapi.application.ActionsKt; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.application.ModalityState; import com.intellij.openapi.diagnostic.Logger; @@ -41,16 +42,14 @@ import com.intellij.openapi.module.Module; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.text.StringUtil; import com.intellij.util.ThreeState; -import com.intellij.util.concurrency.Semaphore; -import org.jetbrains.android.facet.AndroidFacet; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - import java.io.File; import java.io.IOException; import java.util.Date; -import java.util.concurrent.atomic.AtomicBoolean; +import java.util.List; import java.util.concurrent.atomic.AtomicReference; +import org.jetbrains.android.facet.AndroidFacet; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; /** * A runnable task which execute a Fetch as Google request, including building apk, uploading and fetching result. @@ -319,6 +318,7 @@ public class FetchAsGoogleTask implements Runnable { private Project myProject; private Module myModule; private static final Logger logger = Logger.getInstance(Helper.class); + private AssembleInvocationResult myAssembleResult; public Helper(@NotNull Project project, @NotNull Module module) { myProject = project; @@ -329,12 +329,7 @@ public class FetchAsGoogleTask implements Runnable { * Builds the APK file, return whether the build is successful. */ public boolean buildApk() { - // We reference the code in MakeBeforeRunTaskProvider#executeTask to write this method. - final AtomicBoolean success = new AtomicBoolean(); try { - final Semaphore done = new Semaphore(); - done.down(); - final AtomicReference<String> errorMsgRef = new AtomicReference<>(); // If the model needs a sync, we need to sync "synchronously" before running. @@ -360,46 +355,28 @@ public class FetchAsGoogleTask implements Runnable { } final GradleBuildInvoker gradleBuildInvoker = GradleBuildInvoker.getInstance(myProject); - final GradleBuildInvoker.AfterGradleInvocationTask afterTask = new GradleBuildInvoker.AfterGradleInvocationTask() { - @Override - public void execute(@NotNull GradleInvocationResult result) { - success.set(result.isBuildSuccessful()); - gradleBuildInvoker.remove(this); - done.up(); - } - }; - - if (myProject.isDisposed()) { - done.up(); - } - else { - ApplicationManager.getApplication().invokeAndWait(() -> { - gradleBuildInvoker.add(afterTask); - gradleBuildInvoker.assemble(new Module[]{myModule}, TestCompileType.ALL); - }, ModalityState.NON_MODAL); - done.waitFor(); - } + ListenableFuture<AssembleInvocationResult> assembleResultFuture = + ActionsKt.invokeAndWaitIfNeeded( + ModalityState.NON_MODAL, + () -> gradleBuildInvoker.assemble(new Module[]{myModule}, TestCompileType.ALL)); + myAssembleResult = assembleResultFuture.get(); + return myAssembleResult.isBuildSuccessful(); } catch (Throwable t) { return false; } - return success.get(); } @Nullable public File getApkFile() { - File apkFile = null; AndroidFacet facet = AndroidFacet.getInstance(myModule); if (facet != null) { - AndroidModuleModel model = AndroidModuleModel.get(facet); - if (model != null) { - IdeAndroidArtifact artifact = model.getMainArtifact(); - for (IdeAndroidArtifactOutput output : artifact.getOutputs()) { - apkFile = output.getOutputFile(); - } + List<ApkInfo> apkInfos = getBuiltApksForSelectedVariant(myAssembleResult, facet, false); + if (apkInfos.size() == 1) { + return apkInfos.get(0).getFile(); } } - return apkFile; + return null; } @Nullable |