Add back ability to override the local engine in Gradle (#34460)

diff --git a/packages/flutter_tools/gradle/flutter.gradle b/packages/flutter_tools/gradle/flutter.gradle
index ae7e293..a69a19c 100644
--- a/packages/flutter_tools/gradle/flutter.gradle
+++ b/packages/flutter_tools/gradle/flutter.gradle
@@ -162,19 +162,6 @@
         String flutterExecutableName = Os.isFamily(Os.FAMILY_WINDOWS) ? "flutter.bat" : "flutter"
         flutterExecutable = Paths.get(flutterRoot.absolutePath, "bin", flutterExecutableName).toFile();
 
-        // x86/x86_64 native library used for debugging only, for now.
-        File flutterX86Jar = project.file("${project.buildDir}/${AndroidProject.FD_INTERMEDIATES}/flutter/flutter-x86.jar")
-        Task debugX86JarTask = project.tasks.create("${FLUTTER_BUILD_PREFIX}X86Jar", Jar) {
-            destinationDir flutterX86Jar.parentFile
-            archiveName flutterX86Jar.name
-            from("${flutterRoot}/bin/cache/artifacts/engine/android-x86/libflutter.so") {
-                into 'lib/x86'
-            }
-            from("${flutterRoot}/bin/cache/artifacts/engine/android-x64/libflutter.so") {
-                into 'lib/x86_64'
-            }
-        }
-
         if (useLocalEngine(project)) {
             String engineOutPath = project.property('localEngineOut')
             File engineOut = project.file(engineOutPath)
@@ -192,10 +179,22 @@
             // However, we use the same engine for each of the build types.
             project.android.buildTypes.each {
                 addApiDependencies(project, it, project.files {
-                    [debugX86JarTask, flutterJar]
+                    flutterJar
                 })
             }
         } else {
+            // x86/x86_64 native library used for debugging only, for now.
+            File flutterX86Jar = project.file("${project.buildDir}/${AndroidProject.FD_INTERMEDIATES}/flutter/flutter-x86.jar")
+            Task debugX86JarTask = project.tasks.create("${FLUTTER_BUILD_PREFIX}X86Jar", Jar) {
+                destinationDir flutterX86Jar.parentFile
+                archiveName flutterX86Jar.name
+                from("${flutterRoot}/bin/cache/artifacts/engine/android-x86/libflutter.so") {
+                    into 'lib/x86'
+                }
+                from("${flutterRoot}/bin/cache/artifacts/engine/android-x64/libflutter.so") {
+                    into 'lib/x86_64'
+                }
+            }
             String basePlatformArch = getBasePlatform(project)
             // This is meant to include the compiled classes only, however it will include `libflutter.so` as well.
             Path baseEnginePath = Paths.get(flutterRoot.absolutePath, "bin", "cache", "artifacts", "engine")
@@ -527,6 +526,10 @@
                     if (getBasePlatform(project) == targetArch) {
                         return
                     }
+                    // Don't include `libflutter.so` for other architectures when a local engine is specified.
+                    if (useLocalEngine(project)) {
+                        return
+                    }
                     def engineArtifactSubdir = getEngineArtifactDirName(variant.buildType, targetArch);
                     // Include `libflutter.so`.
                     // TODO(blasten): The libs should be outside `flutter.jar` when the artifacts are downloaded.