|
| 1 | +// Copyright 2014 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:io'; |
| 6 | + |
| 7 | +import 'package:file/src/interface/file_system_entity.dart'; |
| 8 | + |
| 9 | +import '../integration.shard/test_utils.dart'; |
| 10 | +import '../src/common.dart'; |
| 11 | +import '../src/context.dart'; |
| 12 | + |
| 13 | +const String gradleSettingsFileContent = r''' |
| 14 | +pluginManagement { |
| 15 | + def flutterSdkPath = { |
| 16 | + def properties = new Properties() |
| 17 | + file("local.properties").withInputStream { properties.load(it) } |
| 18 | + def flutterSdkPath = properties.getProperty("flutter.sdk") |
| 19 | + assert flutterSdkPath != null, "flutter.sdk not set in local.properties" |
| 20 | + return flutterSdkPath |
| 21 | + }() |
| 22 | +
|
| 23 | + includeBuild("$flutterSdkPath/packages/flutter_tools/gradle") |
| 24 | +
|
| 25 | + repositories { |
| 26 | + google() |
| 27 | + mavenCentral() |
| 28 | + gradlePluginPortal() |
| 29 | + } |
| 30 | +} |
| 31 | +
|
| 32 | +plugins { |
| 33 | + id "dev.flutter.flutter-plugin-loader" version "1.0.0" |
| 34 | + id "com.android.application" version "AGP_REPLACE_ME" apply false |
| 35 | + id "org.jetbrains.kotlin.android" version "KGP_REPLACE_ME" apply false |
| 36 | +} |
| 37 | +
|
| 38 | +include ":app" |
| 39 | +
|
| 40 | +'''; |
| 41 | + |
| 42 | +const String agpReplacementString = 'AGP_REPLACE_ME'; |
| 43 | +const String kgpReplacementString = 'KGP_REPLACE_ME'; |
| 44 | + |
| 45 | +const String gradleWrapperPropertiesFileContent = r''' |
| 46 | +distributionBase=GRADLE_USER_HOME |
| 47 | +distributionPath=wrapper/dists |
| 48 | +zipStoreBase=GRADLE_USER_HOME |
| 49 | +zipStorePath=wrapper/dists |
| 50 | +distributionUrl=https\://services.gradle.org/distributions/gradle-GRADLE_REPLACE_ME-all.zip |
| 51 | +
|
| 52 | +'''; |
| 53 | + |
| 54 | +const String gradleReplacementString = 'GRADLE_REPLACE_ME'; |
| 55 | + |
| 56 | +// This test is currently on the preview shard (but not using the preview |
| 57 | +// version of Android) because it is the only one using Java 11. This test |
| 58 | +// requires Java 11 due to the intentionally low version of Gradle. |
| 59 | +void main() { |
| 60 | + late Directory tempDir; |
| 61 | + |
| 62 | + setUpAll(() async { |
| 63 | + tempDir = createResolvedTempDirectorySync('run_test.'); |
| 64 | + }); |
| 65 | + |
| 66 | + tearDownAll(() async { |
| 67 | + tryToDelete(tempDir as FileSystemEntity); |
| 68 | + }); |
| 69 | + |
| 70 | + testUsingContext( |
| 71 | + 'AGP version out of "warn" support band prints warning but still builds', () async { |
| 72 | + // Create a new flutter project. |
| 73 | + final String flutterBin = fileSystem.path.join(getFlutterRoot(), 'bin', 'flutter'); |
| 74 | + ProcessResult result = await processManager.run(<String>[ |
| 75 | + flutterBin, |
| 76 | + 'create', |
| 77 | + 'dependency_checker_app', |
| 78 | + '--platforms=android', |
| 79 | + ], workingDirectory: tempDir.path); |
| 80 | + expect(result, const ProcessResultMatcher()); |
| 81 | + const String gradleVersion = '7.5'; |
| 82 | + const String agpVersion = '4.2.0'; |
| 83 | + const String kgpVersion = '1.7.10'; |
| 84 | + |
| 85 | + final Directory app = Directory(fileSystem.path.join(tempDir.path, 'dependency_checker_app')); |
| 86 | + |
| 87 | + // Modify gradle version to passed in version. |
| 88 | + final File gradleWrapperProperties = File(fileSystem.path.join( |
| 89 | + app.path, 'android', 'gradle', 'wrapper', 'gradle-wrapper.properties')); |
| 90 | + final String propertyContent = gradleWrapperPropertiesFileContent.replaceFirst( |
| 91 | + gradleReplacementString, |
| 92 | + gradleVersion, |
| 93 | + ); |
| 94 | + await gradleWrapperProperties.writeAsString(propertyContent, flush: true); |
| 95 | + |
| 96 | + final File gradleSettings = File(fileSystem.path.join( |
| 97 | + app.path, 'android', 'settings.gradle')); |
| 98 | + final String settingsContent = gradleSettingsFileContent |
| 99 | + .replaceFirst(agpReplacementString, agpVersion) |
| 100 | + .replaceFirst(kgpReplacementString, kgpVersion); |
| 101 | + await gradleSettings.writeAsString(settingsContent, flush: true); |
| 102 | + |
| 103 | + |
| 104 | + // Ensure that gradle files exists from templates. |
| 105 | + result = await processManager.run(<String>[ |
| 106 | + flutterBin, |
| 107 | + 'build', |
| 108 | + 'apk', |
| 109 | + '--debug', |
| 110 | + ], workingDirectory: app.path); |
| 111 | + expect(result, const ProcessResultMatcher()); |
| 112 | + expect(result.stderr, contains('Please upgrade your Android Gradle ' |
| 113 | + 'Plugin version')); |
| 114 | + }); |
| 115 | + |
| 116 | + testUsingContext( |
| 117 | + 'Gradle version out of "warn" support band prints warning but still builds', () async { |
| 118 | + // Create a new flutter project. |
| 119 | + final String flutterBin = fileSystem.path.join(getFlutterRoot(), 'bin', 'flutter'); |
| 120 | + ProcessResult result = await processManager.run(<String>[ |
| 121 | + flutterBin, |
| 122 | + 'create', |
| 123 | + 'dependency_checker_app', |
| 124 | + '--platforms=android', |
| 125 | + ], workingDirectory: tempDir.path); |
| 126 | + expect(result, const ProcessResultMatcher()); |
| 127 | + const String gradleVersion = '7.0'; |
| 128 | + const String agpVersion = '4.2.0'; |
| 129 | + const String kgpVersion = '1.7.10'; |
| 130 | + |
| 131 | + final Directory app = Directory(fileSystem.path.join(tempDir.path, 'dependency_checker_app')); |
| 132 | + |
| 133 | + // Modify gradle version to passed in version. |
| 134 | + final File gradleWrapperProperties = File(fileSystem.path.join( |
| 135 | + app.path, 'android', 'gradle', 'wrapper', 'gradle-wrapper.properties')); |
| 136 | + final String propertyContent = gradleWrapperPropertiesFileContent.replaceFirst( |
| 137 | + gradleReplacementString, |
| 138 | + gradleVersion, |
| 139 | + ); |
| 140 | + await gradleWrapperProperties.writeAsString(propertyContent, flush: true); |
| 141 | + |
| 142 | + final File gradleSettings = File(fileSystem.path.join( |
| 143 | + app.path, 'android', 'settings.gradle')); |
| 144 | + final String settingsContent = gradleSettingsFileContent |
| 145 | + .replaceFirst(agpReplacementString, agpVersion) |
| 146 | + .replaceFirst(kgpReplacementString, kgpVersion); |
| 147 | + await gradleSettings.writeAsString(settingsContent, flush: true); |
| 148 | + |
| 149 | + |
| 150 | + // Ensure that gradle files exists from templates. |
| 151 | + result = await processManager.run(<String>[ |
| 152 | + flutterBin, |
| 153 | + 'build', |
| 154 | + 'apk', |
| 155 | + '--debug', |
| 156 | + ], workingDirectory: app.path); |
| 157 | + expect(result, const ProcessResultMatcher()); |
| 158 | + expect(result.stderr, contains('Please upgrade your Gradle version')); |
| 159 | + }); |
| 160 | + |
| 161 | + testUsingContext( |
| 162 | + 'Kotlin version out of "warn" support band prints warning but still builds', () async { |
| 163 | + // Create a new flutter project. |
| 164 | + final String flutterBin = fileSystem.path.join(getFlutterRoot(), 'bin', 'flutter'); |
| 165 | + ProcessResult result = await processManager.run(<String>[ |
| 166 | + flutterBin, |
| 167 | + 'create', |
| 168 | + 'dependency_checker_app', |
| 169 | + '--platforms=android', |
| 170 | + ], workingDirectory: tempDir.path); |
| 171 | + expect(result, const ProcessResultMatcher()); |
| 172 | + const String gradleVersion = '7.5'; |
| 173 | + const String agpVersion = '7.4.0'; |
| 174 | + const String kgpVersion = '1.4.10'; |
| 175 | + |
| 176 | + final Directory app = Directory(fileSystem.path.join(tempDir.path, 'dependency_checker_app')); |
| 177 | + |
| 178 | + // Modify gradle version to passed in version. |
| 179 | + final File gradleWrapperProperties = File(fileSystem.path.join( |
| 180 | + app.path, 'android', 'gradle', 'wrapper', 'gradle-wrapper.properties')); |
| 181 | + final String propertyContent = gradleWrapperPropertiesFileContent.replaceFirst( |
| 182 | + gradleReplacementString, |
| 183 | + gradleVersion, |
| 184 | + ); |
| 185 | + await gradleWrapperProperties.writeAsString(propertyContent, flush: true); |
| 186 | + |
| 187 | + final File gradleSettings = File(fileSystem.path.join( |
| 188 | + app.path, 'android', 'settings.gradle')); |
| 189 | + final String settingsContent = gradleSettingsFileContent |
| 190 | + .replaceFirst(agpReplacementString, agpVersion) |
| 191 | + .replaceFirst(kgpReplacementString, kgpVersion); |
| 192 | + await gradleSettings.writeAsString(settingsContent, flush: true); |
| 193 | + |
| 194 | + |
| 195 | + // Ensure that gradle files exists from templates. |
| 196 | + result = await processManager.run(<String>[ |
| 197 | + flutterBin, |
| 198 | + 'build', |
| 199 | + 'apk', |
| 200 | + '--debug', |
| 201 | + ], workingDirectory: app.path); |
| 202 | + expect(result, const ProcessResultMatcher()); |
| 203 | + expect(result.stderr, contains('Please upgrade your Kotlin version')); |
| 204 | + }); |
| 205 | + |
| 206 | + // TODO(gmackall): Add tests for build blocking when the |
| 207 | + // corresponding error versions are enabled. |
| 208 | +} |
0 commit comments