Revert "Revert "Use FlutterProject to locate files (#18913)" (#19409)" (#19456)

With a fix of a path being printed relative instead of absolute.
This commit is contained in:
Sigurd Meldgaard
2018-07-20 08:00:30 +02:00
committed by GitHub
parent d2d17abeec
commit 2d3a5c744d
25 changed files with 438 additions and 392 deletions

View File

@@ -21,6 +21,7 @@ import '../base/utils.dart';
import '../build_info.dart';
import '../device.dart';
import '../globals.dart';
import '../project.dart';
import '../protocol_discovery.dart';
import 'adb.dart';
@@ -241,7 +242,7 @@ class AndroidDevice extends Device {
String _getSourceSha1(ApplicationPackage app) {
final AndroidApk apk = app;
final File shaFile = fs.file('${apk.apkPath}.sha1');
final File shaFile = fs.file('${apk.file.path}.sha1');
return shaFile.existsSync() ? shaFile.readAsStringSync() : '';
}
@@ -269,16 +270,16 @@ class AndroidDevice extends Device {
@override
Future<bool> installApp(ApplicationPackage app) async {
final AndroidApk apk = app;
if (!fs.isFileSync(apk.apkPath)) {
printError('"${apk.apkPath}" does not exist.');
if (!apk.file.existsSync()) {
printError('"${fs.path.relative(apk.file.path)}" does not exist.');
return false;
}
if (!await _checkForSupportedAdbVersion() || !await _checkForSupportedAndroidVersion())
return false;
final Status status = logger.startProgress('Installing ${apk.apkPath}...', expectSlowOperation: true);
final RunResult installResult = await runAsync(adbCommandForDevice(<String>['install', '-t', '-r', apk.apkPath]));
final Status status = logger.startProgress('Installing ${fs.path.relative(apk.file.path)}...', expectSlowOperation: true);
final RunResult installResult = await runAsync(adbCommandForDevice(<String>['install', '-t', '-r', apk.file.path]));
status.stop();
// Some versions of adb exit with exit code 0 even on failure :(
// Parsing the output to check for failures.
@@ -374,12 +375,13 @@ class AndroidDevice extends Device {
if (!prebuiltApplication) {
printTrace('Building APK');
await buildApk(
project: new FlutterProject(fs.currentDirectory),
target: mainPath,
buildInfo: buildInfo,
);
// Package has been built, so we can get the updated application ID and
// activity name from the .apk.
package = await AndroidApk.fromCurrentDirectory();
package = await AndroidApk.fromAndroidProject(new FlutterProject(fs.currentDirectory).android);
}
printTrace("Stopping app '${package.name}' on $name.");

View File

@@ -4,17 +4,22 @@
import 'dart:async';
import 'package:meta/meta.dart';
import '../base/common.dart';
import '../build_info.dart';
import '../globals.dart';
import '../project.dart';
import 'android_sdk.dart';
import 'gradle.dart';
Future<Null> buildApk({
String target,
@required FlutterProject project,
@required String target,
BuildInfo buildInfo = BuildInfo.debug
}) async {
if (!isProjectUsingGradle()) {
if (!project.android.isUsingGradle()) {
throwToolExit(
'The build process for Android has changed, and the current project configuration\n'
'is no longer valid. Please consult\n\n'
@@ -33,5 +38,9 @@ Future<Null> buildApk({
throwToolExit('Try re-installing or updating your Android SDK.');
}
return buildGradleProject(buildInfo, target);
return buildGradleProject(
project: project,
buildInfo: buildInfo,
target: target,
);
}

View File

@@ -4,6 +4,8 @@
import 'dart:async';
import 'package:meta/meta.dart';
import '../android/android_sdk.dart';
import '../artifacts.dart';
import '../base/common.dart';
@@ -14,16 +16,13 @@ import '../base/platform.dart';
import '../base/process.dart';
import '../base/utils.dart';
import '../build_info.dart';
import '../bundle.dart' as bundle;
import '../cache.dart';
import '../flutter_manifest.dart';
import '../globals.dart';
import '../project.dart';
import 'android_sdk.dart';
import 'android_studio.dart';
const String gradleManifestPath = 'android/app/src/main/AndroidManifest.xml';
const String gradleAppOutV1 = 'android/app/build/outputs/apk/app-debug.apk';
const String gradleAppOutDirV1 = 'android/app/build/outputs/apk';
const String gradleVersion = '4.1';
final RegExp _assembleTaskPattern = new RegExp(r'assemble([^:]+): task ');
@@ -45,9 +44,6 @@ final RegExp ndkMessageFilter = new RegExp(r'^(?!NDK is missing a ".*" directory
r'|If you are using NDK, verify the ndk.dir is set to a valid NDK directory. It is currently set to .*)');
bool isProjectUsingGradle() {
return fs.isFileSync('android/build.gradle');
}
FlutterPluginVersion get flutterPluginVersion {
final File plugin = fs.file('android/buildSrc/src/main/groovy/FlutterPlugin.groovy');
@@ -69,18 +65,17 @@ FlutterPluginVersion get flutterPluginVersion {
return FlutterPluginVersion.none;
}
/// Returns the path to the apk file created by [buildGradleProject], relative
/// to current directory.
Future<String> getGradleAppOut() async {
/// Returns the apk file created by [buildGradleProject]
Future<File> getGradleAppOut(AndroidProject androidProject) async {
switch (flutterPluginVersion) {
case FlutterPluginVersion.none:
// Fall through. Pretend we're v1, and just go with it.
case FlutterPluginVersion.v1:
return gradleAppOutV1;
return androidProject.gradleAppOutV1File;
case FlutterPluginVersion.managed:
// Fall through. The managed plugin matches plugin v2 for now.
case FlutterPluginVersion.v2:
return fs.path.relative(fs.path.join((await _gradleProject()).apkDirectory, 'app.apk'));
return fs.file((await _gradleProject()).apkDirectory.childFile('app.apk'));
}
return null;
}
@@ -94,12 +89,13 @@ Future<GradleProject> _gradleProject() async {
// of calculating the app properties using Gradle. This may take minutes.
Future<GradleProject> _readGradleProject() async {
final String gradle = await _ensureGradle();
await updateLocalProperties();
final FlutterProject flutterProject = new FlutterProject(fs.currentDirectory);
await updateLocalProperties(project: flutterProject);
try {
final Status status = logger.startProgress('Resolving dependencies...', expectSlowOperation: true);
final RunResult runResult = await runCheckedAsync(
<String>[gradle, 'app:properties'],
workingDirectory: 'android',
workingDirectory: flutterProject.android.directory.path,
environment: _gradleEnv,
);
final String properties = runResult.stdout.trim();
@@ -117,7 +113,7 @@ Future<GradleProject> _readGradleProject() async {
}
}
// Fall back to the default
return new GradleProject(<String>['debug', 'profile', 'release'], <String>[], gradleAppOutDirV1);
return new GradleProject(<String>['debug', 'profile', 'release'], <String>[], flutterProject.android.gradleAppOutV1Directory);
}
void handleKnownGradleExceptions(String exceptionString) {
@@ -199,28 +195,19 @@ distributionUrl=https\\://services.gradle.org/distributions/gradle-$gradleVersio
/// Overwrite android/local.properties in the specified Flutter project, if needed.
///
/// Throws, if `pubspec.yaml` or Android SDK cannot be located.
Future<void> updateLocalProperties({String projectPath, BuildInfo buildInfo}) async {
final Directory android = (projectPath == null)
? fs.directory('android')
: fs.directory(fs.path.join(projectPath, 'android'));
final String flutterManifest = (projectPath == null)
? fs.path.join(bundle.defaultManifestPath)
: fs.path.join(projectPath, bundle.defaultManifestPath);
if (androidSdk == null) {
///
/// If [requireSdk] is `true` this will fail with a tool-exit if no Android Sdk
/// is found.
Future<void> updateLocalProperties({
@required FlutterProject project,
BuildInfo buildInfo,
bool requireAndroidSdk = true,
}) async {
if (requireAndroidSdk && androidSdk == null) {
throwToolExit('Unable to locate Android SDK. Please run `flutter doctor` for more details.');
}
FlutterManifest manifest;
try {
manifest = await FlutterManifest.createFromPath(flutterManifest);
} catch (error) {
throwToolExit('Failed to load pubspec.yaml: $error');
}
updateLocalPropertiesSync(android, manifest, buildInfo);
}
/// Overwrite local.properties in the specified directory, if needed.
void updateLocalPropertiesSync(Directory android, FlutterManifest manifest, [BuildInfo buildInfo]) {
final File localProperties = android.childFile('local.properties');
final File localProperties = await project.androidLocalPropertiesFile;
bool changed = false;
SettingsFile settings;
@@ -238,6 +225,8 @@ void updateLocalPropertiesSync(Directory android, FlutterManifest manifest, [Bui
}
}
final FlutterManifest manifest = await project.manifest;
if (androidSdk != null)
changeIfNecessary('sdk.dir', escapePath(androidSdk.directory));
changeIfNecessary('flutter.sdk', escapePath(Cache.flutterRoot));
@@ -254,7 +243,11 @@ void updateLocalPropertiesSync(Directory android, FlutterManifest manifest, [Bui
settings.writeContents(localProperties);
}
Future<Null> buildGradleProject(BuildInfo buildInfo, String target) async {
Future<Null> buildGradleProject({
@required FlutterProject project,
@required BuildInfo buildInfo,
@required String target,
}) async {
// Update the local.properties file with the build mode, version name and code.
// FlutterPlugin v1 reads local.properties to determine build mode. Plugin v2
// uses the standard Android way to determine what to build, but we still
@@ -263,7 +256,7 @@ Future<Null> buildGradleProject(BuildInfo buildInfo, String target) async {
// and can be overwritten with flutter build command.
// The default Gradle script reads the version name and number
// from the local.properties file.
await updateLocalProperties(buildInfo: buildInfo);
await updateLocalProperties(project: project, buildInfo: buildInfo);
final String gradle = await _ensureGradle();
@@ -271,7 +264,7 @@ Future<Null> buildGradleProject(BuildInfo buildInfo, String target) async {
case FlutterPluginVersion.none:
// Fall through. Pretend it's v1, and just go for it.
case FlutterPluginVersion.v1:
return _buildGradleProjectV1(gradle);
return _buildGradleProjectV1(project, gradle);
case FlutterPluginVersion.managed:
// Fall through. Managed plugin builds the same way as plugin v2.
case FlutterPluginVersion.v2:
@@ -279,7 +272,7 @@ Future<Null> buildGradleProject(BuildInfo buildInfo, String target) async {
}
}
Future<Null> _buildGradleProjectV1(String gradle) async {
Future<Null> _buildGradleProjectV1(FlutterProject project, String gradle) async {
// Run 'gradlew build'.
final Status status = logger.startProgress('Running \'gradlew build\'...', expectSlowOperation: true);
final int exitCode = await runCommandAndStreamOutput(
@@ -293,7 +286,7 @@ Future<Null> _buildGradleProjectV1(String gradle) async {
if (exitCode != 0)
throwToolExit('Gradle build failed: $exitCode', exitCode: exitCode);
printStatus('Built $gradleAppOutV1.');
printStatus('Built ${fs.path.relative(project.android.gradleAppOutV1File.path)}.');
}
Future<Null> _buildGradleProjectV2(String gradle, BuildInfo buildInfo, String target) async {
@@ -371,10 +364,10 @@ Future<Null> _buildGradleProjectV2(String gradle, BuildInfo buildInfo, String ta
if (apkFile == null)
throwToolExit('Gradle build failed to produce an Android package.');
// Copy the APK to app.apk, so `flutter run`, `flutter install`, etc. can find it.
apkFile.copySync(fs.path.join(project.apkDirectory, 'app.apk'));
apkFile.copySync(project.apkDirectory.childFile('app.apk').path);
printTrace('calculateSha: ${project.apkDirectory}/app.apk');
final File apkShaFile = fs.file(fs.path.join(project.apkDirectory, 'app.apk.sha1'));
final File apkShaFile = project.apkDirectory.childFile('app.apk.sha1');
apkShaFile.writeAsStringSync(calculateSha(apkFile));
String appSize;
@@ -390,15 +383,15 @@ File _findApkFile(GradleProject project, BuildInfo buildInfo) {
final String apkFileName = project.apkFileFor(buildInfo);
if (apkFileName == null)
return null;
File apkFile = fs.file(fs.path.join(project.apkDirectory, apkFileName));
File apkFile = fs.file(fs.path.join(project.apkDirectory.path, apkFileName));
if (apkFile.existsSync())
return apkFile;
apkFile = fs.file(fs.path.join(project.apkDirectory, buildInfo.modeName, apkFileName));
apkFile = fs.file(fs.path.join(project.apkDirectory.path, buildInfo.modeName, apkFileName));
if (apkFile.existsSync())
return apkFile;
if (buildInfo.flavor != null) {
// Android Studio Gradle plugin v3 adds flavor to path.
apkFile = fs.file(fs.path.join(project.apkDirectory, buildInfo.flavor, buildInfo.modeName, apkFileName));
apkFile = fs.file(fs.path.join(project.apkDirectory.path, buildInfo.flavor, buildInfo.modeName, apkFileName));
if (apkFile.existsSync())
return apkFile;
}
@@ -453,13 +446,13 @@ class GradleProject {
return new GradleProject(
buildTypes.toList(),
productFlavors.toList(),
fs.path.normalize(fs.path.join(buildDir, 'outputs', 'apk')),
fs.directory(fs.path.join(buildDir, 'outputs', 'apk')),
);
}
final List<String> buildTypes;
final List<String> productFlavors;
final String apkDirectory;
final Directory apkDirectory;
String _buildTypeFor(BuildInfo buildInfo) {
if (buildTypes.contains(buildInfo.modeName))