forked from firka/flutter
[flutter_tools] unify gradle/android sdk reinitialize checks in cache.dart (#76342)
This commit is contained in:
@@ -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');
|
||||
|
||||
Reference in New Issue
Block a user