From 99786b413fba73252c359e814da9e20ade4a7e4d Mon Sep 17 00:00:00 2001 From: Gray Mackall <34871572+gmackall@users.noreply.github.com> Date: Thu, 12 Dec 2024 15:23:29 -0800 Subject: [PATCH] Force automatic ndk download when building for Android (#159756) In preparation for changing engine builds to be unstripped by default https://github.com/flutter/engine/pull/52852, which will allow us to make progress towards resolving https://github.com/flutter/flutter/issues/60240. Tricks AGP in to downloading the NDK when building a flutter app (that uses the FGP, which to my knowledge is all ways of building flutter apk/aab/aar). I want to follow this up by modifying the tool to search for the log line that the NDK is missing (making it throw an error in that case) as a safeguard, because that would be the last line of defense before we accidentally build a bloated app. The safeguard won't work for add to app, from what I understand, because while they use the FGP (so they should be forced to be download the NDK) they don't invoke the flutter tool, and therefore won't invoke the custom error handling of [`gradle_errors.dart`](https://github.com/flutter/flutter/blob/master/packages/flutter_tools/lib/src/android/gradle_errors.dart) Some details 1. Respects existing values for `externalNativeBuild.cmake.path` for apps that actually use it. 2. Silences some warnings that would otherwise appear for add to app builds or builds that manually invoke gradle: ``` C/C++: CMake Warning (dev) in CMakeLists.txt: C/C++: No project() command is present. The top-level CMakeLists.txt file must C/C++: contain a literal, direct call to the project() command. Add a line of C/C++: code such as C/C++: project(ProjectName) C/C++: near the top of the file, but after cmake_minimum_required(). C/C++: CMake is pretending there is a "project(Project)" command on the first C/C++: line. C/C++: This warning is for project developers. Use -Wno-dev to suppress it. C/C++: CMake Warning: C/C++: Manually-specified variables were not used by the project: C/C++: CMAKE_EXPORT_COMPILE_COMMANDS C/C++: CMAKE_LIBRARY_OUTPUT_DIRECTORY C/C++: CMAKE_RUNTIME_OUTPUT_DIRECTORY ``` 3. Our ci installs the NDK at an abnormal place that AGP can't find without help. I've modified all the `build.gradle`s that we have checked in to point to the pre-installed path. **But some of our tests make a new app from the templates, and those tests will now start downloading the NDK** (as they won't be able to find it at it's current path from templates). We could resolve this by actually fixing https://github.com/flutter/flutter/issues/136666. This would be a very significant lift from what I understand - we rely on this hardcoding in a lot of places in our infra. Fixes https://github.com/flutter/flutter/issues/155576 ## 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. - [ ] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. [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 --------- Co-authored-by: Gray Mackall --- .ci.yaml | 6 ++-- dev/a11y_assessments/android/app/build.gradle | 10 +++++- .../complex_layout/android/app/build.gradle | 9 +++++ .../macrobenchmarks/android/app/build.gradle | 9 +++++ .../microbenchmarks/android/app/build.gradle | 9 +++++ .../android/app/build.gradle | 9 +++++ .../android/app/build.gradle | 9 +++++ .../android/app/build.gradle | 9 +++++ .../test_apps/stocks/android/app/build.gradle | 9 +++++ .../tasks/gradle_desugar_classes_test.dart | 2 +- .../android/app/build.gradle | 9 +++++ .../android/app/build.gradle | 10 +++++- .../android_views/android/app/build.gradle | 9 +++++ .../channels/android/app/build.gradle | 10 +++++- .../android/app/build.gradle | 9 +++++ .../android/component1/build.gradle | 9 +++++ .../android/app/build.gradle | 9 +++++ .../flavors/android/app/build.gradle | 9 +++++ .../flutter_gallery/android/app/build.gradle | 9 +++++ .../android/app/build.gradle | 9 +++++ .../android/app/build.gradle | 9 +++++ .../app/build.gradle | 9 +++++ .../android/app/build.gradle | 10 +++++- .../non_nullable/android/app/build.gradle | 9 +++++ .../android/app/build.gradle | 9 +++++ .../SampleApp/build.gradle | 9 +++++ .../app/build.gradle | 9 +++++ .../app/build.gradle.kts | 9 +++++ .../android/app/build.gradle | 9 +++++ .../spell_check/android/app/build.gradle | 10 +++++- .../ui/android/app/build.gradle | 9 +++++ dev/manual_tests/android/app/build.gradle | 9 +++++ dev/tracing_tests/android/app/build.gradle | 9 +++++ .../flutter_view/android/app/build.gradle | 9 +++++ .../hello_world/android/app/build.gradle.kts | 9 +++++ examples/image_list/android/app/build.gradle | 9 +++++ examples/layers/android/app/build.gradle | 9 +++++ .../platform_channel/android/app/build.gradle | 9 +++++ .../platform_view/android/app/build.gradle | 9 +++++ .../gradle/src/main/groovy/CMakeLists.txt | 3 ++ .../gradle/src/main/groovy/flutter.groovy | 33 ++++++++++++++++++- .../example/android/app/build.gradle | 14 +++++--- 42 files changed, 382 insertions(+), 14 deletions(-) create mode 100644 packages/flutter_tools/gradle/src/main/groovy/CMakeLists.txt diff --git a/.ci.yaml b/.ci.yaml index 57d08af984..a5eb7ddba6 100644 --- a/.ci.yaml +++ b/.ci.yaml @@ -5503,7 +5503,7 @@ targets: add_recipes_cq: "true" dependencies: >- [ - {"dependency": "android_sdk", "version": "version:33v6"}, + {"dependency": "android_sdk", "version": "version:35v1"}, {"dependency": "chrome_and_driver", "version": "version:125.0.6422.141"}, {"dependency": "open_jdk", "version": "version:17"}, {"dependency": "goldctl", "version": "git_revision:2387d6fff449587eecbb7e45b2692ca0710b63b9"}, @@ -5521,7 +5521,7 @@ targets: add_recipes_cq: "true" dependencies: >- [ - {"dependency": "android_sdk", "version": "version:33v6"}, + {"dependency": "android_sdk", "version": "version:35v1"}, {"dependency": "chrome_and_driver", "version": "version:125.0.6422.141"}, {"dependency": "open_jdk", "version": "version:17"}, {"dependency": "goldctl", "version": "git_revision:2387d6fff449587eecbb7e45b2692ca0710b63b9"}, @@ -5539,7 +5539,7 @@ targets: add_recipes_cq: "true" dependencies: >- [ - {"dependency": "android_sdk", "version": "version:33v6"}, + {"dependency": "android_sdk", "version": "version:35v1"}, {"dependency": "chrome_and_driver", "version": "version:125.0.6422.141"}, {"dependency": "open_jdk", "version": "version:17"}, {"dependency": "goldctl", "version": "git_revision:2387d6fff449587eecbb7e45b2692ca0710b63b9"}, diff --git a/dev/a11y_assessments/android/app/build.gradle b/dev/a11y_assessments/android/app/build.gradle index c911663423..d040546cc3 100644 --- a/dev/a11y_assessments/android/app/build.gradle +++ b/dev/a11y_assessments/android/app/build.gradle @@ -35,7 +35,15 @@ if (keystorePropertiesFile.exists()) { android { namespace "com.example.a11y_assessments" compileSdk flutter.compileSdkVersion - ndkVersion flutter.ndkVersion + + // Flutter's CI installs the NDK at a non-standard path. + // This non-standard structure is initially created by + // https://github.com/flutter/engine/blob/3.27.0/tools/android_sdk/create_cipd_packages.sh. + String systemNdkPath = System.getenv("ANDROID_NDK_PATH") + if (systemNdkPath != null) { + ndkVersion = flutter.ndkVersion + ndkPath = systemNdkPath + } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 diff --git a/dev/benchmarks/complex_layout/android/app/build.gradle b/dev/benchmarks/complex_layout/android/app/build.gradle index f86cb35c1d..8b00ebb415 100644 --- a/dev/benchmarks/complex_layout/android/app/build.gradle +++ b/dev/benchmarks/complex_layout/android/app/build.gradle @@ -29,6 +29,15 @@ android { namespace "com.yourcompany.complexLayout" compileSdk flutter.compileSdkVersion + // Flutter's CI installs the NDK at a non-standard path. + // This non-standard structure is initially created by + // https://github.com/flutter/engine/blob/3.27.0/tools/android_sdk/create_cipd_packages.sh. + String systemNdkPath = System.getenv("ANDROID_NDK_PATH") + if (systemNdkPath != null) { + ndkVersion = flutter.ndkVersion + ndkPath = systemNdkPath + } + compileOptions { sourceCompatibility JavaVersion.VERSION_11 targetCompatibility JavaVersion.VERSION_11 diff --git a/dev/benchmarks/macrobenchmarks/android/app/build.gradle b/dev/benchmarks/macrobenchmarks/android/app/build.gradle index 06acb88de4..f358286d00 100644 --- a/dev/benchmarks/macrobenchmarks/android/app/build.gradle +++ b/dev/benchmarks/macrobenchmarks/android/app/build.gradle @@ -29,6 +29,15 @@ android { namespace "com.example.macrobenchmarks" compileSdk flutter.compileSdkVersion + // Flutter's CI installs the NDK at a non-standard path. + // This non-standard structure is initially created by + // https://github.com/flutter/engine/blob/3.27.0/tools/android_sdk/create_cipd_packages.sh. + String systemNdkPath = System.getenv("ANDROID_NDK_PATH") + if (systemNdkPath != null) { + ndkVersion = flutter.ndkVersion + ndkPath = systemNdkPath + } + compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 diff --git a/dev/benchmarks/microbenchmarks/android/app/build.gradle b/dev/benchmarks/microbenchmarks/android/app/build.gradle index 141ac9622f..e3fc68e472 100644 --- a/dev/benchmarks/microbenchmarks/android/app/build.gradle +++ b/dev/benchmarks/microbenchmarks/android/app/build.gradle @@ -29,6 +29,15 @@ android { namespace "com.yourcompany.microbenchmarks" compileSdk flutter.compileSdkVersion + // Flutter's CI installs the NDK at a non-standard path. + // This non-standard structure is initially created by + // https://github.com/flutter/engine/blob/3.27.0/tools/android_sdk/create_cipd_packages.sh. + String systemNdkPath = System.getenv("ANDROID_NDK_PATH") + if (systemNdkPath != null) { + ndkVersion = flutter.ndkVersion + ndkPath = systemNdkPath + } + compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 diff --git a/dev/benchmarks/multiple_flutters/android/app/build.gradle b/dev/benchmarks/multiple_flutters/android/app/build.gradle index 6a671e475e..9cea465111 100644 --- a/dev/benchmarks/multiple_flutters/android/app/build.gradle +++ b/dev/benchmarks/multiple_flutters/android/app/build.gradle @@ -16,6 +16,15 @@ android { namespace "dev.flutter.multipleflutters" compileSdk 34 + // Flutter's CI installs the NDK at a non-standard path. + // This non-standard structure is initially created by + // https://github.com/flutter/engine/blob/3.27.0/tools/android_sdk/create_cipd_packages.sh. + String systemNdkPath = System.getenv("ANDROID_NDK_PATH") + if (systemNdkPath != null) { + ndkVersion = "26.3.11579264" // This version must exactly match the version of the NDK that the recipe pulls from CIPD. + ndkPath = systemNdkPath + } + compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 diff --git a/dev/benchmarks/platform_views_layout/android/app/build.gradle b/dev/benchmarks/platform_views_layout/android/app/build.gradle index 63c7e0bee4..9504f0ae17 100644 --- a/dev/benchmarks/platform_views_layout/android/app/build.gradle +++ b/dev/benchmarks/platform_views_layout/android/app/build.gradle @@ -29,6 +29,15 @@ android { namespace "dev.benchmarks.platform_views_layout" compileSdk flutter.compileSdkVersion + // Flutter's CI installs the NDK at a non-standard path. + // This non-standard structure is initially created by + // https://github.com/flutter/engine/blob/3.27.0/tools/android_sdk/create_cipd_packages.sh. + String systemNdkPath = System.getenv("ANDROID_NDK_PATH") + if (systemNdkPath != null) { + ndkVersion = flutter.ndkVersion + ndkPath = systemNdkPath + } + compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 diff --git a/dev/benchmarks/platform_views_layout_hybrid_composition/android/app/build.gradle b/dev/benchmarks/platform_views_layout_hybrid_composition/android/app/build.gradle index 2beb7dc539..ee181f49dc 100644 --- a/dev/benchmarks/platform_views_layout_hybrid_composition/android/app/build.gradle +++ b/dev/benchmarks/platform_views_layout_hybrid_composition/android/app/build.gradle @@ -29,6 +29,15 @@ android { namespace "dev.benchmarks.platform_views_layout_hybrid_composition" compileSdk flutter.compileSdkVersion + // Flutter's CI installs the NDK at a non-standard path. + // This non-standard structure is initially created by + // https://github.com/flutter/engine/blob/3.27.0/tools/android_sdk/create_cipd_packages.sh. + String systemNdkPath = System.getenv("ANDROID_NDK_PATH") + if (systemNdkPath != null) { + ndkVersion = flutter.ndkVersion + ndkPath = systemNdkPath + } + compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 diff --git a/dev/benchmarks/test_apps/stocks/android/app/build.gradle b/dev/benchmarks/test_apps/stocks/android/app/build.gradle index 5e7907e25e..6dbd28b9da 100644 --- a/dev/benchmarks/test_apps/stocks/android/app/build.gradle +++ b/dev/benchmarks/test_apps/stocks/android/app/build.gradle @@ -29,6 +29,15 @@ android { namespace "io.flutter.examples.stocks" compileSdk flutter.compileSdkVersion + // Flutter's CI installs the NDK at a non-standard path. + // This non-standard structure is initially created by + // https://github.com/flutter/engine/blob/3.27.0/tools/android_sdk/create_cipd_packages.sh. + String systemNdkPath = System.getenv("ANDROID_NDK_PATH") + if (systemNdkPath != null) { + ndkVersion = flutter.ndkVersion + ndkPath = systemNdkPath + } + compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 diff --git a/dev/devicelab/bin/tasks/gradle_desugar_classes_test.dart b/dev/devicelab/bin/tasks/gradle_desugar_classes_test.dart index 2d6062ed35..5427ab8bd4 100644 --- a/dev/devicelab/bin/tasks/gradle_desugar_classes_test.dart +++ b/dev/devicelab/bin/tasks/gradle_desugar_classes_test.dart @@ -15,7 +15,7 @@ Future main() async { try { await runProjectTest((FlutterProject flutterProject) async { section('APK contains plugin classes'); - await flutterProject.setMinSdkVersion(20); + await flutterProject.setMinSdkVersion(21); await flutterProject.addPlugin('google_maps_flutter:^2.2.1'); await inDirectory(flutterProject.rootPath, () async { diff --git a/dev/integration_tests/android_semantics_testing/android/app/build.gradle b/dev/integration_tests/android_semantics_testing/android/app/build.gradle index 25afe619de..1bcd40e4a3 100644 --- a/dev/integration_tests/android_semantics_testing/android/app/build.gradle +++ b/dev/integration_tests/android_semantics_testing/android/app/build.gradle @@ -20,6 +20,15 @@ android { namespace = "com.yourcompany.platforminteraction" compileSdk flutter.compileSdkVersion + // Flutter's CI installs the NDK at a non-standard path. + // This non-standard structure is initially created by + // https://github.com/flutter/engine/blob/3.27.0/tools/android_sdk/create_cipd_packages.sh. + String systemNdkPath = System.getenv("ANDROID_NDK_PATH") + if (systemNdkPath != null) { + ndkVersion = flutter.ndkVersion + ndkPath = systemNdkPath + } + compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 diff --git a/dev/integration_tests/android_verified_input/android/app/build.gradle b/dev/integration_tests/android_verified_input/android/app/build.gradle index 995fb86b32..e6f0187e1e 100644 --- a/dev/integration_tests/android_verified_input/android/app/build.gradle +++ b/dev/integration_tests/android_verified_input/android/app/build.gradle @@ -29,7 +29,15 @@ if (flutterVersionName == null) { android { namespace "io.flutter.integration.android_verified_input" compileSdk flutter.compileSdkVersion - ndkVersion flutter.ndkVersion + + // Flutter's CI installs the NDK at a non-standard path. + // This non-standard structure is initially created by + // https://github.com/flutter/engine/blob/3.27.0/tools/android_sdk/create_cipd_packages.sh. + String systemNdkPath = System.getenv("ANDROID_NDK_PATH") + if (systemNdkPath != null) { + ndkVersion = flutter.ndkVersion + ndkPath = systemNdkPath + } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 diff --git a/dev/integration_tests/android_views/android/app/build.gradle b/dev/integration_tests/android_views/android/app/build.gradle index 8596326f55..a376d48367 100644 --- a/dev/integration_tests/android_views/android/app/build.gradle +++ b/dev/integration_tests/android_views/android/app/build.gradle @@ -29,6 +29,15 @@ android { namespace = 'io.flutter.integration.platformviews' compileSdk flutter.compileSdkVersion + // Flutter's CI installs the NDK at a non-standard path. + // This non-standard structure is initially created by + // https://github.com/flutter/engine/blob/3.27.0/tools/android_sdk/create_cipd_packages.sh. + String systemNdkPath = System.getenv("ANDROID_NDK_PATH") + if (systemNdkPath != null) { + ndkVersion = flutter.ndkVersion + ndkPath = systemNdkPath + } + compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 diff --git a/dev/integration_tests/channels/android/app/build.gradle b/dev/integration_tests/channels/android/app/build.gradle index 17f8f1332c..29638d599d 100644 --- a/dev/integration_tests/channels/android/app/build.gradle +++ b/dev/integration_tests/channels/android/app/build.gradle @@ -29,7 +29,15 @@ if (flutterVersionName == null) { android { namespace "com.example.channels" compileSdk flutter.compileSdkVersion - ndkVersion flutter.ndkVersion + + // Flutter's CI installs the NDK at a non-standard path. + // This non-standard structure is initially created by + // https://github.com/flutter/engine/blob/3.27.0/tools/android_sdk/create_cipd_packages.sh. + String systemNdkPath = System.getenv("ANDROID_NDK_PATH") + if (systemNdkPath != null) { + ndkVersion = flutter.ndkVersion + ndkPath = systemNdkPath + } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 diff --git a/dev/integration_tests/deferred_components_test/android/app/build.gradle b/dev/integration_tests/deferred_components_test/android/app/build.gradle index 69750fb03d..f17f930b4b 100644 --- a/dev/integration_tests/deferred_components_test/android/app/build.gradle +++ b/dev/integration_tests/deferred_components_test/android/app/build.gradle @@ -36,6 +36,15 @@ android { namespace "io.flutter.integration.deferred_components_test" compileSdk flutter.compileSdkVersion + // Flutter's CI installs the NDK at a non-standard path. + // This non-standard structure is initially created by + // https://github.com/flutter/engine/blob/3.27.0/tools/android_sdk/create_cipd_packages.sh. + String systemNdkPath = System.getenv("ANDROID_NDK_PATH") + if (systemNdkPath != null) { + ndkVersion = flutter.ndkVersion + ndkPath = systemNdkPath + } + sourceSets { main.java.srcDirs += 'src/main/kotlin' } diff --git a/dev/integration_tests/deferred_components_test/android/component1/build.gradle b/dev/integration_tests/deferred_components_test/android/component1/build.gradle index 9daab6d9c9..5354de1031 100644 --- a/dev/integration_tests/deferred_components_test/android/component1/build.gradle +++ b/dev/integration_tests/deferred_components_test/android/component1/build.gradle @@ -26,6 +26,15 @@ android { namespace "io.flutter.integration.deferred_components_test.component1" compileSdk 35 + // Flutter's CI installs the NDK at a non-standard path. + // This non-standard structure is initially created by + // https://github.com/flutter/engine/blob/3.27.0/tools/android_sdk/create_cipd_packages.sh. + String systemNdkPath = System.getenv("ANDROID_NDK_PATH") + if (systemNdkPath != null) { + ndkVersion = "26.3.11579264" // This version must exactly match the version of the NDK that the recipe pulls from CIPD. + ndkPath = systemNdkPath + } + sourceSets { applicationVariants.all { variant -> main.assets.srcDirs += "${project.layout.buildDirectory.get()}/intermediates/flutter/${variant.name}/deferred_assets" diff --git a/dev/integration_tests/external_textures/android/app/build.gradle b/dev/integration_tests/external_textures/android/app/build.gradle index 7e1c89b3a5..c6adeef108 100644 --- a/dev/integration_tests/external_textures/android/app/build.gradle +++ b/dev/integration_tests/external_textures/android/app/build.gradle @@ -19,6 +19,15 @@ android { namespace = "io.flutter.externalui" compileSdk flutter.compileSdkVersion + // Flutter's CI installs the NDK at a non-standard path. + // This non-standard structure is initially created by + // https://github.com/flutter/engine/blob/3.27.0/tools/android_sdk/create_cipd_packages.sh. + String systemNdkPath = System.getenv("ANDROID_NDK_PATH") + if (systemNdkPath != null) { + ndkVersion = flutter.ndkVersion + ndkPath = systemNdkPath + } + compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 diff --git a/dev/integration_tests/flavors/android/app/build.gradle b/dev/integration_tests/flavors/android/app/build.gradle index 5b091f702a..9044bc258a 100644 --- a/dev/integration_tests/flavors/android/app/build.gradle +++ b/dev/integration_tests/flavors/android/app/build.gradle @@ -19,6 +19,15 @@ android { namespace "com.yourcompany.flavors" compileSdk flutter.compileSdkVersion + // Flutter's CI installs the NDK at a non-standard path. + // This non-standard structure is initially created by + // https://github.com/flutter/engine/blob/3.27.0/tools/android_sdk/create_cipd_packages.sh. + String systemNdkPath = System.getenv("ANDROID_NDK_PATH") + if (systemNdkPath != null) { + ndkVersion = flutter.ndkVersion + ndkPath = systemNdkPath + } + compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 diff --git a/dev/integration_tests/flutter_gallery/android/app/build.gradle b/dev/integration_tests/flutter_gallery/android/app/build.gradle index 5c5fa7d61c..df976a59cd 100644 --- a/dev/integration_tests/flutter_gallery/android/app/build.gradle +++ b/dev/integration_tests/flutter_gallery/android/app/build.gradle @@ -45,6 +45,15 @@ android { namespace "io.flutter.demo.gallery" compileSdk flutter.compileSdkVersion + // Flutter's CI installs the NDK at a non-standard path. + // This non-standard structure is initially created by + // https://github.com/flutter/engine/blob/3.27.0/tools/android_sdk/create_cipd_packages.sh. + String systemNdkPath = System.getenv("ANDROID_NDK_PATH") + if (systemNdkPath != null) { + ndkVersion = flutter.ndkVersion + ndkPath = systemNdkPath + } + compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 diff --git a/dev/integration_tests/gradle_deprecated_settings/android/app/build.gradle b/dev/integration_tests/gradle_deprecated_settings/android/app/build.gradle index ecb2deeb6d..64c3432e3b 100644 --- a/dev/integration_tests/gradle_deprecated_settings/android/app/build.gradle +++ b/dev/integration_tests/gradle_deprecated_settings/android/app/build.gradle @@ -22,6 +22,15 @@ android { namespace = "com.yourcompany.flavors" compileSdk 34 + // Flutter's CI installs the NDK at a non-standard path. + // This non-standard structure is initially created by + // https://github.com/flutter/engine/blob/3.27.0/tools/android_sdk/create_cipd_packages.sh. + String systemNdkPath = System.getenv("ANDROID_NDK_PATH") + if (systemNdkPath != null) { + ndkVersion = flutter.ndkVersion + ndkPath = systemNdkPath + } + compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 diff --git a/dev/integration_tests/hybrid_android_views/android/app/build.gradle b/dev/integration_tests/hybrid_android_views/android/app/build.gradle index 5fb0d47446..b595440083 100644 --- a/dev/integration_tests/hybrid_android_views/android/app/build.gradle +++ b/dev/integration_tests/hybrid_android_views/android/app/build.gradle @@ -29,6 +29,15 @@ android { namespace = "io.flutter.integration.platformviews" compileSdk flutter.compileSdkVersion + // Flutter's CI installs the NDK at a non-standard path. + // This non-standard structure is initially created by + // https://github.com/flutter/engine/blob/3.27.0/tools/android_sdk/create_cipd_packages.sh. + String systemNdkPath = System.getenv("ANDROID_NDK_PATH") + if (systemNdkPath != null) { + ndkVersion = flutter.ndkVersion + ndkPath = systemNdkPath + } + compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 diff --git a/dev/integration_tests/module_host_with_custom_build_v2_embedding/app/build.gradle b/dev/integration_tests/module_host_with_custom_build_v2_embedding/app/build.gradle index 1e69c1f986..75f07cb601 100644 --- a/dev/integration_tests/module_host_with_custom_build_v2_embedding/app/build.gradle +++ b/dev/integration_tests/module_host_with_custom_build_v2_embedding/app/build.gradle @@ -8,6 +8,15 @@ android { namespace = "io.flutter.addtoapp" compileSdk 34 + // Flutter's CI installs the NDK at a non-standard path. + // This non-standard structure is initially created by + // https://github.com/flutter/engine/blob/3.27.0/tools/android_sdk/create_cipd_packages.sh. + String systemNdkPath = System.getenv("ANDROID_NDK_PATH") + if (systemNdkPath != null) { + ndkVersion = "26.3.11579264" // This version must exactly match the version of the NDK that the recipe pulls from CIPD. + ndkPath = systemNdkPath + } + compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 diff --git a/dev/integration_tests/native_driver_test/android/app/build.gradle b/dev/integration_tests/native_driver_test/android/app/build.gradle index c5b106e0c9..4734c2bdd7 100644 --- a/dev/integration_tests/native_driver_test/android/app/build.gradle +++ b/dev/integration_tests/native_driver_test/android/app/build.gradle @@ -12,7 +12,15 @@ plugins { android { namespace = "com.example.native_driver_test" compileSdk = flutter.compileSdkVersion - ndkVersion = flutter.ndkVersion + + // Flutter's CI installs the NDK at a non-standard path. + // This non-standard structure is initially created by + // https://github.com/flutter/engine/blob/3.27.0/tools/android_sdk/create_cipd_packages.sh. + String systemNdkPath = System.getenv("ANDROID_NDK_PATH") + if (systemNdkPath != null) { + ndkVersion = flutter.ndkVersion + ndkPath = systemNdkPath + } compileOptions { sourceCompatibility = JavaVersion.VERSION_1_8 diff --git a/dev/integration_tests/non_nullable/android/app/build.gradle b/dev/integration_tests/non_nullable/android/app/build.gradle index 9822c24687..3b305e5ad8 100644 --- a/dev/integration_tests/non_nullable/android/app/build.gradle +++ b/dev/integration_tests/non_nullable/android/app/build.gradle @@ -30,6 +30,15 @@ android { namespace = "com.example.non_nullable" compileSdk flutter.compileSdkVersion + // Flutter's CI installs the NDK at a non-standard path. + // This non-standard structure is initially created by + // https://github.com/flutter/engine/blob/3.27.0/tools/android_sdk/create_cipd_packages.sh. + String systemNdkPath = System.getenv("ANDROID_NDK_PATH") + if (systemNdkPath != null) { + ndkVersion = flutter.ndkVersion + ndkPath = systemNdkPath + } + compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 diff --git a/dev/integration_tests/platform_interaction/android/app/build.gradle b/dev/integration_tests/platform_interaction/android/app/build.gradle index 7f8fea246c..5dfccb53e2 100644 --- a/dev/integration_tests/platform_interaction/android/app/build.gradle +++ b/dev/integration_tests/platform_interaction/android/app/build.gradle @@ -19,6 +19,15 @@ android { namespace = "com.yourcompany.platforminteraction" compileSdk flutter.compileSdkVersion + // Flutter's CI installs the NDK at a non-standard path. + // This non-standard structure is initially created by + // https://github.com/flutter/engine/blob/3.27.0/tools/android_sdk/create_cipd_packages.sh. + String systemNdkPath = System.getenv("ANDROID_NDK_PATH") + if (systemNdkPath != null) { + ndkVersion = flutter.ndkVersion + ndkPath = systemNdkPath + } + compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 diff --git a/dev/integration_tests/pure_android_host_apps/android_custom_host_app/SampleApp/build.gradle b/dev/integration_tests/pure_android_host_apps/android_custom_host_app/SampleApp/build.gradle index 1ec1e7bc65..0227cf37b2 100644 --- a/dev/integration_tests/pure_android_host_apps/android_custom_host_app/SampleApp/build.gradle +++ b/dev/integration_tests/pure_android_host_apps/android_custom_host_app/SampleApp/build.gradle @@ -8,6 +8,15 @@ android { namespace = "io.flutter.add2app" compileSdk 34 + // Flutter's CI installs the NDK at a non-standard path. + // This non-standard structure is initially created by + // https://github.com/flutter/engine/blob/3.27.0/tools/android_sdk/create_cipd_packages.sh. + String systemNdkPath = System.getenv("ANDROID_NDK_PATH") + if (systemNdkPath != null) { + ndkVersion = "26.3.11579264" // This version must exactly match the version of the NDK that the recipe pulls from CIPD. + ndkPath = systemNdkPath + } + compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 diff --git a/dev/integration_tests/pure_android_host_apps/android_host_app_v2_embedding/app/build.gradle b/dev/integration_tests/pure_android_host_apps/android_host_app_v2_embedding/app/build.gradle index 71abced98b..fee774a0c7 100644 --- a/dev/integration_tests/pure_android_host_apps/android_host_app_v2_embedding/app/build.gradle +++ b/dev/integration_tests/pure_android_host_apps/android_host_app_v2_embedding/app/build.gradle @@ -8,6 +8,15 @@ android { namespace = "io.flutter.add2app" compileSdk 34 + // Flutter's CI installs the NDK at a non-standard path. + // This non-standard structure is initially created by + // https://github.com/flutter/engine/blob/3.27.0/tools/android_sdk/create_cipd_packages.sh. + String systemNdkPath = System.getenv("ANDROID_NDK_PATH") + if (systemNdkPath != null) { + ndkVersion = "26.3.11579264" // This version must exactly match the version of the NDK that the recipe pulls from CIPD. + ndkPath = systemNdkPath + } + compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 diff --git a/dev/integration_tests/pure_android_host_apps/host_app_kotlin_gradle_dsl/app/build.gradle.kts b/dev/integration_tests/pure_android_host_apps/host_app_kotlin_gradle_dsl/app/build.gradle.kts index 80fc8488e9..d7189b8355 100644 --- a/dev/integration_tests/pure_android_host_apps/host_app_kotlin_gradle_dsl/app/build.gradle.kts +++ b/dev/integration_tests/pure_android_host_apps/host_app_kotlin_gradle_dsl/app/build.gradle.kts @@ -7,6 +7,15 @@ android { namespace = "com.example.myapplication" compileSdk = 34 + // Flutter's CI installs the NDK at a non-standard path. + // This non-standard structure is initially created by + // https://github.com/flutter/engine/blob/3.27.0/tools/android_sdk/create_cipd_packages.sh. + val systemNdkPath: String? = System.getenv("ANDROID_NDK_PATH") + if (systemNdkPath != null) { + ndkVersion = "26.3.11579264" // This version must exactly match the version of the NDK that the recipe pulls from CIPD. + ndkPath = systemNdkPath + } + defaultConfig { applicationId = "com.example.myapplication" minSdk = 24 diff --git a/dev/integration_tests/release_smoke_test/android/app/build.gradle b/dev/integration_tests/release_smoke_test/android/app/build.gradle index 357e24908a..526a60cf47 100644 --- a/dev/integration_tests/release_smoke_test/android/app/build.gradle +++ b/dev/integration_tests/release_smoke_test/android/app/build.gradle @@ -30,6 +30,15 @@ android { namespace = "com.example.release_smoke_test" compileSdk flutter.compileSdkVersion + // Flutter's CI installs the NDK at a non-standard path. + // This non-standard structure is initially created by + // https://github.com/flutter/engine/blob/3.27.0/tools/android_sdk/create_cipd_packages.sh. + String systemNdkPath = System.getenv("ANDROID_NDK_PATH") + if (systemNdkPath != null) { + ndkVersion = flutter.ndkVersion + ndkPath = systemNdkPath + } + compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 diff --git a/dev/integration_tests/spell_check/android/app/build.gradle b/dev/integration_tests/spell_check/android/app/build.gradle index 40d896e873..242e3d7360 100644 --- a/dev/integration_tests/spell_check/android/app/build.gradle +++ b/dev/integration_tests/spell_check/android/app/build.gradle @@ -29,7 +29,15 @@ if (flutterVersionName == null) { android { namespace "com.example.spell_check" compileSdk flutter.compileSdkVersion - ndkVersion flutter.ndkVersion + + // Flutter's CI installs the NDK at a non-standard path. + // This non-standard structure is initially created by + // https://github.com/flutter/engine/blob/3.27.0/tools/android_sdk/create_cipd_packages.sh. + String systemNdkPath = System.getenv("ANDROID_NDK_PATH") + if (systemNdkPath != null) { + ndkVersion = flutter.ndkVersion + ndkPath = systemNdkPath + } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 diff --git a/dev/integration_tests/ui/android/app/build.gradle b/dev/integration_tests/ui/android/app/build.gradle index 0b7c795741..b661791560 100644 --- a/dev/integration_tests/ui/android/app/build.gradle +++ b/dev/integration_tests/ui/android/app/build.gradle @@ -12,6 +12,15 @@ android { namespace "com.yourcompany.integration_ui" compileSdk flutter.compileSdkVersion + // Flutter's CI installs the NDK at a non-standard path. + // This non-standard structure is initially created by + // https://github.com/flutter/engine/blob/3.27.0/tools/android_sdk/create_cipd_packages.sh. + String systemNdkPath = System.getenv("ANDROID_NDK_PATH") + if (systemNdkPath != null) { + ndkVersion = flutter.ndkVersion + ndkPath = systemNdkPath + } + compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 diff --git a/dev/manual_tests/android/app/build.gradle b/dev/manual_tests/android/app/build.gradle index 8321f807a8..7ae76f585d 100644 --- a/dev/manual_tests/android/app/build.gradle +++ b/dev/manual_tests/android/app/build.gradle @@ -30,6 +30,15 @@ android { namespace = "dev.flutter.manual_tests" compileSdk flutter.compileSdkVersion + // Flutter's CI installs the NDK at a non-standard path. + // This non-standard structure is initially created by + // https://github.com/flutter/engine/blob/3.27.0/tools/android_sdk/create_cipd_packages.sh. + String systemNdkPath = System.getenv("ANDROID_NDK_PATH") + if (systemNdkPath != null) { + ndkVersion = flutter.ndkVersion + ndkPath = systemNdkPath + } + compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 diff --git a/dev/tracing_tests/android/app/build.gradle b/dev/tracing_tests/android/app/build.gradle index a3bd4a16a4..438f04ee24 100644 --- a/dev/tracing_tests/android/app/build.gradle +++ b/dev/tracing_tests/android/app/build.gradle @@ -30,6 +30,15 @@ android { namespace = "com.example.tracing_tests" compileSdk flutter.compileSdkVersion + // Flutter's CI installs the NDK at a non-standard path. + // This non-standard structure is initially created by + // https://github.com/flutter/engine/blob/3.27.0/tools/android_sdk/create_cipd_packages.sh. + String systemNdkPath = System.getenv("ANDROID_NDK_PATH") + if (systemNdkPath != null) { + ndkVersion = flutter.ndkVersion + ndkPath = systemNdkPath + } + compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 diff --git a/examples/flutter_view/android/app/build.gradle b/examples/flutter_view/android/app/build.gradle index 7c18e6456f..1feb5a505b 100644 --- a/examples/flutter_view/android/app/build.gradle +++ b/examples/flutter_view/android/app/build.gradle @@ -11,6 +11,15 @@ android { namespace "com.example.view" compileSdk flutter.compileSdkVersion + // Flutter's CI installs the NDK at a non-standard path. + // This non-standard structure is initially created by + // https://github.com/flutter/engine/blob/3.27.0/tools/android_sdk/create_cipd_packages.sh. + String systemNdkPath = System.getenv("ANDROID_NDK_PATH") + if (systemNdkPath != null) { + ndkVersion = flutter.ndkVersion + ndkPath = systemNdkPath + } + compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 diff --git a/examples/hello_world/android/app/build.gradle.kts b/examples/hello_world/android/app/build.gradle.kts index 175e23095a..d0e5ec689c 100644 --- a/examples/hello_world/android/app/build.gradle.kts +++ b/examples/hello_world/android/app/build.gradle.kts @@ -11,6 +11,15 @@ android { namespace = "io.flutter.examples.hello_world" compileSdk = flutter.compileSdkVersion + // Flutter's CI installs the NDK at a non-standard path. + // This non-standard structure is initially created by + // https://github.com/flutter/engine/blob/3.27.0/tools/android_sdk/create_cipd_packages.sh. + val systemNdkPath: String? = System.getenv("ANDROID_NDK_PATH") + if (systemNdkPath != null) { + ndkVersion = flutter.ndkVersion + ndkPath = systemNdkPath + } + compileOptions { sourceCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_1_8 diff --git a/examples/image_list/android/app/build.gradle b/examples/image_list/android/app/build.gradle index e4b8b76b39..1adead8eb0 100644 --- a/examples/image_list/android/app/build.gradle +++ b/examples/image_list/android/app/build.gradle @@ -29,6 +29,15 @@ android { namespace "com.example.image_list" compileSdk flutter.compileSdkVersion + // Flutter's CI installs the NDK at a non-standard path. + // This non-standard structure is initially created by + // https://github.com/flutter/engine/blob/3.27.0/tools/android_sdk/create_cipd_packages.sh. + String systemNdkPath = System.getenv("ANDROID_NDK_PATH") + if (systemNdkPath != null) { + ndkVersion = flutter.ndkVersion + ndkPath = systemNdkPath + } + compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 diff --git a/examples/layers/android/app/build.gradle b/examples/layers/android/app/build.gradle index 87d5fd2ee6..3cb9dd323c 100644 --- a/examples/layers/android/app/build.gradle +++ b/examples/layers/android/app/build.gradle @@ -11,6 +11,15 @@ android { namespace "io.flutter.examples.Layers" compileSdk flutter.compileSdkVersion + // Flutter's CI installs the NDK at a non-standard path. + // This non-standard structure is initially created by + // https://github.com/flutter/engine/blob/3.27.0/tools/android_sdk/create_cipd_packages.sh. + String systemNdkPath = System.getenv("ANDROID_NDK_PATH") + if (systemNdkPath != null) { + ndkVersion = flutter.ndkVersion + ndkPath = systemNdkPath + } + compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 diff --git a/examples/platform_channel/android/app/build.gradle b/examples/platform_channel/android/app/build.gradle index 6da008a41b..762775b520 100644 --- a/examples/platform_channel/android/app/build.gradle +++ b/examples/platform_channel/android/app/build.gradle @@ -29,6 +29,15 @@ android { namespace "com.example.platformchannel" compileSdk flutter.compileSdkVersion + // Flutter's CI installs the NDK at a non-standard path. + // This non-standard structure is initially created by + // https://github.com/flutter/engine/blob/3.27.0/tools/android_sdk/create_cipd_packages.sh. + String systemNdkPath = System.getenv("ANDROID_NDK_PATH") + if (systemNdkPath != null) { + ndkVersion = flutter.ndkVersion + ndkPath = systemNdkPath + } + compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 diff --git a/examples/platform_view/android/app/build.gradle b/examples/platform_view/android/app/build.gradle index 2fda0ca212..b66e071395 100644 --- a/examples/platform_view/android/app/build.gradle +++ b/examples/platform_view/android/app/build.gradle @@ -29,6 +29,15 @@ android { namespace "io.flutter.examples.platform_view" compileSdk flutter.compileSdkVersion + // Flutter's CI installs the NDK at a non-standard path. + // This non-standard structure is initially created by + // https://github.com/flutter/engine/blob/3.27.0/tools/android_sdk/create_cipd_packages.sh. + String systemNdkPath = System.getenv("ANDROID_NDK_PATH") + if (systemNdkPath != null) { + ndkVersion = flutter.ndkVersion + ndkPath = systemNdkPath + } + compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 diff --git a/packages/flutter_tools/gradle/src/main/groovy/CMakeLists.txt b/packages/flutter_tools/gradle/src/main/groovy/CMakeLists.txt new file mode 100644 index 0000000000..ae7354f572 --- /dev/null +++ b/packages/flutter_tools/gradle/src/main/groovy/CMakeLists.txt @@ -0,0 +1,3 @@ +# Empty file to trick the Android Gradle Plugin to download the NDK. This is because AGP requires +# the NDK in order to strip debug symbols from native libraries, does not download it in that case +# (instead, AGP only downloads it when AGP thinks it needs the NDK to build a native library). diff --git a/packages/flutter_tools/gradle/src/main/groovy/flutter.groovy b/packages/flutter_tools/gradle/src/main/groovy/flutter.groovy index dfec35b704..e9ef058072 100644 --- a/packages/flutter_tools/gradle/src/main/groovy/flutter.groovy +++ b/packages/flutter_tools/gradle/src/main/groovy/flutter.groovy @@ -62,7 +62,7 @@ class FlutterExtension { * Chosen as default version of the AGP version below as found in * https://developer.android.com/studio/projects/install-ndk#default-ndk-per-agp. */ - public final String ndkVersion = "26.1.10909125" + public final String ndkVersion = "26.3.11579264" /** * Specifies the relative directory to the Flutter project directory. @@ -279,6 +279,7 @@ class FlutterPlugin implements Plugin { extension.flutterVersionName = flutterVersionName ?: "1.0" this.addFlutterTasks(project) + forceNdkDownload(project, flutterRootPath) // By default, assembling APKs generates fat APKs if multiple platforms are passed. // Configuring split per ABI allows to generate separate APKs for each abi. @@ -859,6 +860,36 @@ class FlutterPlugin implements Plugin { return version2 } + private void forceNdkDownload(Project gradleProject, String flutterSdkRootPath) { + // If the project is already configuring a native build, we don't need to do anything. + Boolean forcingNotRequired = gradleProject.android.externalNativeBuild.cmake.path != null + if (forcingNotRequired) { + return + } + + // Otherwise, point to an empty CMakeLists.txt, and ignore associated warnings. + gradleProject.android { + externalNativeBuild { + cmake { + // Respect the existing configuration if it exists - the NDK will already be + // downloaded in this case. + path = flutterSdkRootPath + "/packages/flutter_tools/gradle/src/main/groovy/CMakeLists.txt" + } + } + + defaultConfig { + externalNativeBuild { + cmake { + // CMake will print warnings when you try to build an empty project. + // These arguments silence the warnings - our project is intentionally + // empty. + arguments("-Wno-dev", "--no-warn-unused-cli") + } + } + } + } + } + /** Prints error message and fix for any plugin compileSdkVersion or ndkVersion that are higher than the project. */ private void detectLowCompileSdkVersionOrNdkVersion() { project.afterEvaluate { diff --git a/packages/integration_test/example/android/app/build.gradle b/packages/integration_test/example/android/app/build.gradle index 782992d712..62d30ab06b 100644 --- a/packages/integration_test/example/android/app/build.gradle +++ b/packages/integration_test/example/android/app/build.gradle @@ -26,12 +26,18 @@ if (flutterVersionName == null) { } android { - // Conditional for compatibility with AGP <4.2. - if (project.android.hasProperty("namespace")) { - namespace 'com.example.integration_test_example' - } + namespace 'com.example.integration_test_example' compileSdk flutter.compileSdkVersion + // Flutter's CI installs the NDK at a non-standard path. + // This non-standard structure is initially created by + // https://github.com/flutter/engine/blob/3.27.0/tools/android_sdk/create_cipd_packages.sh. + String systemNdkPath = System.getenv("ANDROID_NDK_PATH") + if (systemNdkPath != null) { + ndkVersion = flutter.ndkVersion + ndkPath = systemNdkPath + } + compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8