Unverified Commit 75470a00 authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

[flutter_tools] unify gradle/android sdk reinitialize checks in cache.dart (#76342)

parent c45e459e
......@@ -31,7 +31,6 @@ import 'gradle_errors.dart';
import 'gradle_utils.dart';
/// The directory where the APK artifact is generated.
@visibleForTesting
Directory getApkDirectory(FlutterProject project) {
return project.isModule
? project.android.buildDirectory
......@@ -107,50 +106,6 @@ Iterable<String> _apkFilesFor(AndroidBuildInfo androidBuildInfo) {
return <String>['app$flavorString-$buildType.apk'];
}
/// Returns true if the current version of the Gradle plugin is supported.
bool _isSupportedVersion(AndroidProject project) {
final FileSystem fileSystem = project.hostAppGradleRoot.fileSystem;
final File plugin = project.hostAppGradleRoot.childFile(
fileSystem.path.join('buildSrc', 'src', 'main', 'groovy', 'FlutterPlugin.groovy'));
if (plugin.existsSync()) {
return false;
}
final File appGradle = project.hostAppGradleRoot.childFile(
fileSystem.path.join('app', 'build.gradle'));
if (!appGradle.existsSync()) {
return false;
}
for (final String line in appGradle.readAsLinesSync()) {
if (line.contains(RegExp(r'apply from: .*/flutter.gradle')) ||
line.contains("def flutterPluginVersion = 'managed'")) {
return true;
}
}
return false;
}
/// Runs `gradlew dependencies`, ensuring that dependencies are resolved and
/// potentially downloaded.
Future<void> checkGradleDependencies(Logger logger, ProcessUtils processUtils) async {
final Status progress = logger.startProgress(
'Ensuring gradle dependencies are up to date...',
);
final FlutterProject flutterProject = FlutterProject.current();
await processUtils.run(<String>[
globals.gradleUtils.getExecutable(flutterProject),
'dependencies',
],
throwOnError: true,
workingDirectory: flutterProject.android.hostAppGradleRoot.path,
environment: <String, String>{
if (javaPath != null)
'JAVA_HOME': javaPath,
},
);
globals.androidSdk?.reinitialize();
progress.stop();
}
/// Tries to create `settings_aar.gradle` in an app project by removing the subprojects
/// from the existing `settings.gradle` file. This operation will fail if the existing
/// `settings.gradle` file has local edits.
......@@ -330,10 +285,7 @@ class AndroidGradleBuilder implements AndroidBuilder {
assert(localGradleErrors != null);
assert(globals.androidSdk != null);
if (!project.android.isUsingGradle) {
_exitWithProjectNotUsingGradleMessage(_usage);
}
if (!_isSupportedVersion(project.android)) {
if (!project.android.isSupportedVersion) {
_exitWithUnsupportedProjectMessage(_usage);
}
final Directory buildDirectory = project.android.buildDirectory;
......@@ -915,24 +867,6 @@ void _exitWithUnsupportedProjectMessage(Usage usage) {
);
}
void _exitWithProjectNotUsingGradleMessage(Usage usage) {
BuildEvent('unsupported-project', eventError: 'app-not-using-gradle', flutterUsage: usage).send();
throwToolExit(
'$warningMark The build process for Android has changed, and the '
'current project configuration is no longer valid. Please consult\n\n'
'https://github.com/flutter/flutter/wiki/Upgrading-Flutter-projects-to-build-with-gradle\n\n'
'for details on how to upgrade the project.'
);
}
/// Returns the apk file created by [buildGradleProject]
Future<File> getGradleAppOut(AndroidProject androidProject, Usage usage) async {
if (!_isSupportedVersion(androidProject)) {
_exitWithUnsupportedProjectMessage(usage);
}
return getApkDirectory(androidProject.parent).childFile('app.apk');
}
/// Returns [true] if the current app uses AndroidX.
// TODO(egarciad): https://github.com/flutter/flutter/issues/40800
// Remove `FlutterManifest.usesAndroidX` and provide a unified `AndroidProject.usesAndroidX`.
......
......@@ -64,9 +64,6 @@ class ApplicationPackageFactory {
case TargetPlatform.android_arm64:
case TargetPlatform.android_x64:
case TargetPlatform.android_x86:
if (_androidSdk?.licensesAvailable == true && _androidSdk?.latestVersion == null) {
await checkGradleDependencies(_logger, _processUtils);
}
if (applicationBinary == null) {
return await AndroidApk.fromAndroidProject(
FlutterProject.current().android,
......@@ -220,8 +217,8 @@ class AndroidApk extends ApplicationPackage {
}) async {
File apkFile;
if (androidProject.isUsingGradle) {
apkFile = await getGradleAppOut(androidProject, globals.flutterUsage);
if (androidProject.isUsingGradle && androidProject.isSupportedVersion) {
apkFile = getApkDirectory(androidProject.parent).childFile('app.apk');
if (apkFile.existsSync()) {
// Grab information from the .apk. The gradle build script might alter
// the application Id, so we need to look at what was actually built.
......
......@@ -1201,6 +1201,7 @@ class AndroidMavenArtifacts extends ArtifactSet {
} finally {
status.stop();
tempDir.deleteSync(recursive: true);
globals.androidSdk?.reinitialize();
}
}
......
......@@ -822,6 +822,31 @@ class AndroidProject extends FlutterProjectPlatform {
/// True if the Flutter project is using the AndroidX support library.
bool get usesAndroidX => parent.usesAndroidX;
/// Returns true if the current version of the Gradle plugin is supported.
bool get isSupportedVersion => _isSupportedVersion ??= _computeSupportedVersion();
bool _isSupportedVersion;
bool _computeSupportedVersion() {
final FileSystem fileSystem = hostAppGradleRoot.fileSystem;
final File plugin = hostAppGradleRoot.childFile(
fileSystem.path.join('buildSrc', 'src', 'main', 'groovy', 'FlutterPlugin.groovy'));
if (plugin.existsSync()) {
return false;
}
final File appGradle = hostAppGradleRoot.childFile(
fileSystem.path.join('app', 'build.gradle'));
if (!appGradle.existsSync()) {
return false;
}
for (final String line in appGradle.readAsLinesSync()) {
if (line.contains(RegExp(r'apply from: .*/flutter.gradle')) ||
line.contains("def flutterPluginVersion = 'managed'")) {
return true;
}
}
return false;
}
/// True, if the app project is using Kotlin.
bool get isKotlin {
final File gradleFile = hostAppGradleRoot.childDirectory('app').childFile('build.gradle');
......
......@@ -96,10 +96,6 @@ void main() {
testUsingContext('Licenses available, build tools not, apk exists', () async {
when(sdk.latestVersion).thenReturn(null);
final FlutterProject project = FlutterProject.fromDirectoryTest(fs.currentDirectory);
final File gradle = project.android.hostAppGradleRoot.childFile(
globals.platform.isWindows ? 'gradlew.bat' : 'gradlew',
)..createSync(recursive: true);
project.android.hostAppGradleRoot
.childFile('gradle.properties')
.writeAsStringSync('irrelevant');
......@@ -111,8 +107,6 @@ void main() {
gradleWrapperDir.childFile('gradlew').writeAsStringSync('irrelevant');
gradleWrapperDir.childFile('gradlew.bat').writeAsStringSync('irrelevant');
fakeProcessManager.addCommand(FakeCommand(command: <String>[gradle.path, 'dependencies']));
await ApplicationPackageFactory.instance.getPackageForPlatform(
TargetPlatform.android_arm,
buildInfo: null,
......
......@@ -767,6 +767,7 @@ void main() {
group('AndroidMavenArtifacts', () {
MemoryFileSystem memoryFileSystem;
Cache cache;
FakeAndroidSdk fakeAndroidSdk;
setUp(() {
memoryFileSystem = MemoryFileSystem.test();
......@@ -774,6 +775,7 @@ void main() {
fileSystem: memoryFileSystem,
processManager: FakeProcessManager.any(),
);
fakeAndroidSdk = FakeAndroidSdk();
});
testWithoutContext('AndroidMavenArtifacts has a specified development artifact', () async {
......@@ -793,6 +795,7 @@ void main() {
await mavenArtifacts.update(MockArtifactUpdater(), BufferLogger.test(), memoryFileSystem, MockOperatingSystemUtils());
expect(await mavenArtifacts.isUpToDate(memoryFileSystem), isFalse);
expect(fakeAndroidSdk.reinitialized, true);
}, overrides: <Type, Generator>{
Cache: () => cache,
FileSystem: () => memoryFileSystem,
......@@ -806,7 +809,7 @@ void main() {
'resolveDependencies',
])
]),
AndroidSdk: () => FakeAndroidSdk()
AndroidSdk: () => fakeAndroidSdk
});
testUsingContext('AndroidMavenArtifacts is a no-op if the Android SDK is absent', () async {
......@@ -903,4 +906,11 @@ class FakeCache extends Cache {
return stampFile;
}
}
class FakeAndroidSdk extends Fake implements AndroidSdk {}
class FakeAndroidSdk extends Fake implements AndroidSdk {
bool reinitialized = false;
@override
void reinitialize() {
reinitialized = true;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment