Skip to content

Commit 0c0406a

Browse files
authored
Resolve resolve native Flutter dependencies in Android Studio (flutter#167332)
With this fix you can edit Flutter and native code in one Android Studio instance. This fixes flutter#19830 The only downside is that you need to copy the files `settings.gradle`, `build.gradle` and `local.properteis` from the `android` folder to the project root folder. The last one should be added to the `.gitignore` file. The second step is that you need to correct the path in the `settings.gradle` at the end is: ```groovy include ':app' ``` That needs to be replaced with: ```groovy include ':app' project(':app').projectDir = new File(rootDir, 'android/app/') ``` The cool thing is if you have two flutter projects or another gradle module you can include them there too and you get the autocompletion too. ```groovy include ':app', ':app2', ':native-lib' project(':app').projectDir = new File(rootDir, 'android/app/') project(':app2').projectDir = new File(rootDir, 'app2/android/app/') project(':native-lib').projectDir = new File(rootDir, 'example/native-lib/') ``` My assumption is that there will be some change requests and some questions. Feel free to ask <!-- *If you had to change anything in the [flutter/tests] repo, include a link to the migration guide as per the [breaking change policy].* --> ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent d966d99 commit 0c0406a

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

packages/flutter_tools/gradle/src/main/kotlin/FlutterPlugin.kt

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import org.gradle.api.Task
2020
import org.gradle.api.UnknownTaskException
2121
import org.gradle.api.file.Directory
2222
import org.gradle.api.tasks.Copy
23+
import org.gradle.api.tasks.TaskInstantiationException
2324
import org.gradle.api.tasks.TaskProvider
2425
import org.gradle.api.tasks.bundling.Jar
2526
import org.gradle.internal.os.OperatingSystem
@@ -97,6 +98,13 @@ class FlutterPlugin : Plugin<Project> {
9798
repositories.maven {
9899
url = uri(repository!!)
99100
}
101+
if (plugins.hasPlugin("com.android.application") && isInvokedFromAndroidStudio()) {
102+
dependencies.add("compileOnly", "io.flutter:flutter_embedding_debug:$engineVersion")
103+
dependencies.add("compileOnly", "io.flutter:armeabi_v7a_debug:$engineVersion")
104+
dependencies.add("compileOnly", "io.flutter:arm64_v8a_debug:$engineVersion")
105+
dependencies.add("compileOnly", "io.flutter:x86_debug:$engineVersion")
106+
dependencies.add("compileOnly", "io.flutter:x86_64_debug:$engineVersion")
107+
}
100108
}
101109

102110
project.apply {
@@ -292,19 +300,23 @@ class FlutterPlugin : Plugin<Project> {
292300
}
293301

294302
private fun addTaskForLockfileGeneration(rootProject: Project) {
295-
rootProject.tasks.register("generateLockfiles") {
296-
doLast {
297-
rootProject.subprojects.forEach { subproject ->
298-
val gradlew: String =
299-
getExecutableNameForPlatform("${rootProject.projectDir}/gradlew")
300-
val execOps = rootProject.serviceOf<ExecOperations>()
301-
execOps.exec {
302-
workingDir(rootProject.projectDir)
303-
executable(gradlew)
304-
args(":${subproject.name}:dependencies", "--write-locks")
303+
try {
304+
rootProject.tasks.register("generateLockfiles") {
305+
doLast {
306+
rootProject.subprojects.forEach { subproject ->
307+
val gradlew: String =
308+
getExecutableNameForPlatform("${rootProject.projectDir}/gradlew")
309+
val execOps = rootProject.serviceOf<ExecOperations>()
310+
execOps.exec {
311+
workingDir(rootProject.projectDir)
312+
executable(gradlew)
313+
args(":${subproject.name}:dependencies", "--write-locks")
314+
}
305315
}
306316
}
307317
}
318+
} catch (e: TaskInstantiationException) {
319+
// ignored
308320
}
309321
}
310322

@@ -773,4 +785,12 @@ class FlutterPlugin : Plugin<Project> {
773785
return copyFlutterAssetsTask
774786
}
775787
}
788+
789+
/**
790+
* Returns true if the Gradle task is invoked by Android Studio.
791+
*
792+
* This is true when the property `android.injected.invoked.from.ide` is passed to Gradle.
793+
* This property is set by Android Studio when it invokes a Gradle task.
794+
*/
795+
private fun isInvokedFromAndroidStudio(): Boolean = project?.hasProperty("android.injected.invoked.from.ide") == true
776796
}

0 commit comments

Comments
 (0)