Skip to content

Commit

Permalink
Refactor gradle.dart (flutter#43479)
Browse files Browse the repository at this point in the history
  • Loading branch information
Emmanuel Garcia authored and Inconnu08 committed Nov 5, 2019
1 parent 4be2109 commit 72c2bdc
Show file tree
Hide file tree
Showing 18 changed files with 2,561 additions and 1,863 deletions.
92 changes: 19 additions & 73 deletions packages/flutter_tools/lib/src/android/android_builder.dart
Expand Up @@ -6,19 +6,23 @@ import 'dart:async';

import 'package:meta/meta.dart';

import '../base/common.dart';
import '../android/gradle_errors.dart';
import '../base/context.dart';
import '../base/file_system.dart';
import '../build_info.dart';
import '../project.dart';

import 'android_sdk.dart';
import 'gradle.dart';

/// The builder in the current context.
AndroidBuilder get androidBuilder => context.get<AndroidBuilder>() ?? _AndroidBuilderImpl();
AndroidBuilder get androidBuilder {
return context.get<AndroidBuilder>() ?? const _AndroidBuilderImpl();
}

/// Provides the methods to build Android artifacts.
// TODO(egarciad): https://github.com/flutter/flutter/issues/43863
abstract class AndroidBuilder {
const AndroidBuilder();
/// Builds an AAR artifact.
Future<void> buildAar({
@required FlutterProject project,
Expand All @@ -44,7 +48,7 @@ abstract class AndroidBuilder {

/// Default implementation of [AarBuilder].
class _AndroidBuilderImpl extends AndroidBuilder {
_AndroidBuilderImpl();
const _AndroidBuilderImpl();

/// Builds the AAR and POM files for the current Flutter module or plugin.
@override
Expand All @@ -54,27 +58,18 @@ class _AndroidBuilderImpl extends AndroidBuilder {
@required String target,
@required String outputDir,
}) async {
if (!project.android.isUsingGradle) {
throwToolExit(
'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.'
);
}
if (!project.manifest.isModule && !project.manifest.isPlugin) {
throwToolExit('AARs can only be built for plugin or module projects.');
}
// Validate that we can find an Android SDK.
if (androidSdk == null) {
throwToolExit('No Android SDK found. Try setting the `ANDROID_SDK_ROOT` environment variable.');
}
try {
Directory outputDirectory =
fs.directory(outputDir ?? project.android.buildDirectory);
if (project.isModule) {
// Module projects artifacts are located in `build/host`.
outputDirectory = outputDirectory.childDirectory('host');
}
await buildGradleAar(
project: project,
androidBuildInfo: androidBuildInfo,
target: target,
outputDir: outputDir,
outputDir: outputDirectory,
);
} finally {
androidSdk.reinitialize();
Expand All @@ -88,24 +83,13 @@ class _AndroidBuilderImpl extends AndroidBuilder {
@required AndroidBuildInfo androidBuildInfo,
@required String target,
}) async {
if (!project.android.isUsingGradle) {
throwToolExit(
'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.'
);
}
// Validate that we can find an android sdk.
if (androidSdk == null) {
throwToolExit('No Android SDK found. Try setting the ANDROID_SDK_ROOT environment variable.');
}
try {
await buildGradleProject(
await buildGradleApp(
project: project,
androidBuildInfo: androidBuildInfo,
target: target,
isBuildingBundle: false,
localGradleErrors: gradleErrors,
);
} finally {
androidSdk.reinitialize();
Expand All @@ -119,54 +103,16 @@ class _AndroidBuilderImpl extends AndroidBuilder {
@required AndroidBuildInfo androidBuildInfo,
@required String target,
}) async {
if (!project.android.isUsingGradle) {
throwToolExit(
'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.'
);
}
// Validate that we can find an android sdk.
if (androidSdk == null) {
throwToolExit('No Android SDK found. Try setting the ANDROID_HOME environment variable.');
}

try {
await buildGradleProject(
await buildGradleApp(
project: project,
androidBuildInfo: androidBuildInfo,
target: target,
isBuildingBundle: true,
localGradleErrors: gradleErrors,
);
} finally {
androidSdk.reinitialize();
}
}
}

/// A fake implementation of [AndroidBuilder].
@visibleForTesting
class FakeAndroidBuilder implements AndroidBuilder {
@override
Future<void> buildAar({
@required FlutterProject project,
@required AndroidBuildInfo androidBuildInfo,
@required String target,
@required String outputDir,
}) async {}

@override
Future<void> buildApk({
@required FlutterProject project,
@required AndroidBuildInfo androidBuildInfo,
@required String target,
}) async {}

@override
Future<void> buildAab({
@required FlutterProject project,
@required AndroidBuildInfo androidBuildInfo,
@required String target,
}) async {}
}
2 changes: 1 addition & 1 deletion packages/flutter_tools/lib/src/android/android_studio.dart
Expand Up @@ -236,7 +236,7 @@ class AndroidStudio implements Comparable<AndroidStudio> {

// Read all $HOME/.AndroidStudio*/system/.home files. There may be several
// pointing to the same installation, so we grab only the latest one.
if (fs.directory(homeDirPath).existsSync()) {
if (homeDirPath != null && fs.directory(homeDirPath).existsSync()) {
for (FileSystemEntity entity in fs.directory(homeDirPath).listSync(followLinks: false)) {
if (entity is Directory && entity.basename.startsWith('.AndroidStudio')) {
final AndroidStudio studio = AndroidStudio.fromHomeDot(entity);
Expand Down

0 comments on commit 72c2bdc

Please sign in to comment.