From be6234d56e849c15a17a692bb22208a226dda3c8 Mon Sep 17 00:00:00 2001 From: Anisov Aleksey <10289829+AnisovAleksey@users.noreply.github.com> Date: Mon, 6 Apr 2020 10:46:44 +0700 Subject: [PATCH] Read custom app project name from gradle.properties (#52791) --- dev/bots/test.dart | 1 + .../module_custom_host_app_name_test.dart | 356 ++++++++++++++++++ .../android_custom_host_app/README.md | 8 + .../SampleApp/build.gradle | 27 ++ .../SampleApp/src/main/AndroidManifest.xml | 13 + .../java/io/flutter/add2app/MainActivity.java | 12 + .../android_custom_host_app/build.gradle | 24 ++ .../android_custom_host_app/gradle.properties | 4 + .../gradle/wrapper/gradle-wrapper.properties | 6 + .../android_custom_host_app/local.properties | 8 + .../android_custom_host_app/settings.gradle | 7 + packages/flutter_tools/gradle/flutter.gradle | 7 +- 12 files changed, 470 insertions(+), 3 deletions(-) create mode 100644 dev/devicelab/bin/tasks/module_custom_host_app_name_test.dart create mode 100644 dev/integration_tests/android_custom_host_app/README.md create mode 100644 dev/integration_tests/android_custom_host_app/SampleApp/build.gradle create mode 100644 dev/integration_tests/android_custom_host_app/SampleApp/src/main/AndroidManifest.xml create mode 100644 dev/integration_tests/android_custom_host_app/SampleApp/src/main/java/io/flutter/add2app/MainActivity.java create mode 100644 dev/integration_tests/android_custom_host_app/build.gradle create mode 100644 dev/integration_tests/android_custom_host_app/gradle.properties create mode 100644 dev/integration_tests/android_custom_host_app/gradle/wrapper/gradle-wrapper.properties create mode 100644 dev/integration_tests/android_custom_host_app/local.properties create mode 100644 dev/integration_tests/android_custom_host_app/settings.gradle diff --git a/dev/bots/test.dart b/dev/bots/test.dart index c0a76814ec..75bc709454 100644 --- a/dev/bots/test.dart +++ b/dev/bots/test.dart @@ -923,6 +923,7 @@ Future _runHostOnlyDeviceLabTests() async { () => _runDevicelabTest('gradle_r8_test', environment: gradleEnvironment), () => _runDevicelabTest('module_host_with_custom_build_test', environment: gradleEnvironment, testEmbeddingV2: true), + () => _runDevicelabTest('module_custom_host_app_name_test', environment: gradleEnvironment), () => _runDevicelabTest('module_test', environment: gradleEnvironment, testEmbeddingV2: true), () => _runDevicelabTest('plugin_dependencies_test', environment: gradleEnvironment), diff --git a/dev/devicelab/bin/tasks/module_custom_host_app_name_test.dart b/dev/devicelab/bin/tasks/module_custom_host_app_name_test.dart new file mode 100644 index 0000000000..ae63ad3868 --- /dev/null +++ b/dev/devicelab/bin/tasks/module_custom_host_app_name_test.dart @@ -0,0 +1,356 @@ +// Copyright 2014 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'dart:async'; +import 'dart:io'; + +import 'package:flutter_devicelab/framework/apk_utils.dart'; +import 'package:flutter_devicelab/framework/framework.dart'; +import 'package:flutter_devicelab/framework/utils.dart'; +import 'package:path/path.dart' as path; + +final String gradlew = Platform.isWindows ? 'gradlew.bat' : 'gradlew'; +final String gradlewExecutable = Platform.isWindows ? '.\\$gradlew' : './$gradlew'; +final String fileReadWriteMode = Platform.isWindows ? 'rw-rw-rw-' : 'rw-r--r--'; + +/// Tests that the Flutter module project template works and supports +/// adding Flutter to an existing Android app. +Future main() async { + await task(() async { + + section('Find Java'); + + final String javaHome = await findJavaHome(); + if (javaHome == null) + return TaskResult.failure('Could not find Java'); + print('\nUsing JAVA_HOME=$javaHome'); + + section('Create Flutter module project'); + + final Directory tempDir = Directory.systemTemp.createTempSync('flutter_module_test.'); + final Directory projectDir = Directory(path.join(tempDir.path, 'hello')); + try { + await inDirectory(tempDir, () async { + await flutter( + 'create', + options: ['--org', 'io.flutter.devicelab', '--template=module', 'hello'], + ); + }); + + section('Add read-only asset'); + + final File readonlyTxtAssetFile = await File(path.join( + projectDir.path, + 'assets/read-only.txt' + )) + .create(recursive: true); + + if (!exists(readonlyTxtAssetFile)) { + return TaskResult.failure('Failed to create read-only asset'); + } + + if (!Platform.isWindows) { + await exec('chmod', [ + '444', + readonlyTxtAssetFile.path + ]); + } + + final File pubspec = File(path.join(projectDir.path, 'pubspec.yaml')); + String content = await pubspec.readAsString(); + content = content.replaceFirst( + '\n # assets:\n', + '\n assets:\n - assets/read-only.txt\n', + ); + await pubspec.writeAsString(content, flush: true); + + section('Add plugins'); + + content = await pubspec.readAsString(); + content = content.replaceFirst( + '\ndependencies:\n', + '\ndependencies:\n device_info: 0.4.1\n package_info: 0.4.0+9\n', + ); + await pubspec.writeAsString(content, flush: true); + await inDirectory(projectDir, () async { + await flutter( + 'packages', + options: ['get'], + ); + }); + + section('Build Flutter module library archive'); + + await inDirectory(Directory(path.join(projectDir.path, '.android')), () async { + await exec( + gradlewExecutable, + ['flutter:assembleDebug'], + environment: { 'JAVA_HOME': javaHome }, + ); + }); + + final bool aarBuilt = exists(File(path.join( + projectDir.path, + '.android', + 'Flutter', + 'build', + 'outputs', + 'aar', + 'flutter-debug.aar', + ))); + + if (!aarBuilt) { + return TaskResult.failure('Failed to build .aar'); + } + + section('Build ephemeral host app'); + + await inDirectory(projectDir, () async { + await flutter( + 'build', + options: ['apk'], + ); + }); + + final bool ephemeralHostApkBuilt = exists(File(path.join( + projectDir.path, + 'build', + 'host', + 'outputs', + 'apk', + 'release', + 'app-release.apk', + ))); + + if (!ephemeralHostApkBuilt) { + return TaskResult.failure('Failed to build ephemeral host .apk'); + } + + section('Clean build'); + + await inDirectory(projectDir, () async { + await flutter('clean'); + }); + + section('Make Android host app editable'); + + await inDirectory(projectDir, () async { + await flutter( + 'make-host-app-editable', + options: ['android'], + ); + }); + + section('Build editable host app'); + + await inDirectory(projectDir, () async { + await flutter( + 'build', + options: ['apk'], + ); + }); + + final bool editableHostApkBuilt = exists(File(path.join( + projectDir.path, + 'build', + 'host', + 'outputs', + 'apk', + 'release', + 'app-release.apk', + ))); + + if (!editableHostApkBuilt) { + return TaskResult.failure('Failed to build editable host .apk'); + } + + section('Add to existing Android app'); + + final Directory hostApp = Directory(path.join(tempDir.path, 'hello_host_app')); + mkdir(hostApp); + recursiveCopy( + Directory( + path.join( + flutterDirectory.path, + 'dev', + 'integration_tests', + 'android_custom_host_app', + ), + ), + hostApp, + ); + copy( + File(path.join(projectDir.path, '.android', gradlew)), + hostApp, + ); + copy( + File(path.join(projectDir.path, '.android', 'gradle', 'wrapper', 'gradle-wrapper.jar')), + Directory(path.join(hostApp.path, 'gradle', 'wrapper')), + ); + + final File analyticsOutputFile = File(path.join(tempDir.path, 'analytics.log')); + + section('Build debug host APK'); + + await inDirectory(hostApp, () async { + if (!Platform.isWindows) { + await exec('chmod', ['+x', 'gradlew']); + } + await exec(gradlewExecutable, + ['SampleApp:assembleDebug'], + environment: { + 'JAVA_HOME': javaHome, + 'FLUTTER_ANALYTICS_LOG_FILE': analyticsOutputFile.path, + }, + ); + }); + + section('Check debug APK exists'); + + final String debugHostApk = path.join( + hostApp.path, + 'SampleApp', + 'build', + 'outputs', + 'apk', + 'debug', + 'SampleApp-debug.apk', + ); + if (!exists(File(debugHostApk))) { + return TaskResult.failure('Failed to build debug host APK'); + } + + section('Check files in debug APK'); + + checkCollectionContains([ + ...flutterAssets, + ...debugAssets, + ...baseApkFiles, + ], await getFilesInApk(debugHostApk)); + + section('Check debug AndroidManifest.xml'); + + final String androidManifestDebug = await getAndroidManifest(debugHostApk); + if (!androidManifestDebug.contains(''' + ''') + ) { + return TaskResult.failure("Debug host APK doesn't contain metadata: flutterProjectType = module "); + } + + final String analyticsOutput = analyticsOutputFile.readAsStringSync(); + if (!analyticsOutput.contains('cd24: android') + || !analyticsOutput.contains('cd25: true') + || !analyticsOutput.contains('viewName: assemble')) { + return TaskResult.failure( + 'Building outer app produced the following analytics: "$analyticsOutput" ' + 'but not the expected strings: "cd24: android", "cd25: true" and ' + '"viewName: assemble"' + ); + } + + section('Check file access modes for read-only asset from Flutter module'); + + final String readonlyDebugAssetFilePath = path.join( + hostApp.path, + 'SampleApp', + 'build', + 'intermediates', + 'merged_assets', + 'debug', + 'out', + 'flutter_assets/assets/read-only.txt', + ); + final File readonlyDebugAssetFile = File(readonlyDebugAssetFilePath); + if (!exists(readonlyDebugAssetFile)) { + return TaskResult.failure('Failed to copy read-only asset file'); + } + + String modes = readonlyDebugAssetFile.statSync().modeString(); + print('\nread-only.txt file access modes = $modes'); + if (modes != null && modes.compareTo(fileReadWriteMode) != 0) { + return TaskResult.failure('Failed to make assets user-readable and writable'); + } + + section('Build release host APK'); + + await inDirectory(hostApp, () async { + await exec(gradlewExecutable, + ['SampleApp:assembleRelease'], + environment: { + 'JAVA_HOME': javaHome, + 'FLUTTER_ANALYTICS_LOG_FILE': analyticsOutputFile.path, + }, + ); + }); + + final String releaseHostApk = path.join( + hostApp.path, + 'SampleApp', + 'build', + 'outputs', + 'apk', + 'release', + 'SampleApp-release-unsigned.apk', + ); + if (!exists(File(releaseHostApk))) { + return TaskResult.failure('Failed to build release host APK'); + } + + section('Check files in release APK'); + + checkCollectionContains([ + ...flutterAssets, + ...baseApkFiles, + 'lib/arm64-v8a/libapp.so', + 'lib/arm64-v8a/libflutter.so', + 'lib/armeabi-v7a/libapp.so', + 'lib/armeabi-v7a/libflutter.so', + ], await getFilesInApk(releaseHostApk)); + + section('Check release AndroidManifest.xml'); + + final String androidManifestRelease = await getAndroidManifest(debugHostApk); + if (!androidManifestRelease.contains(''' + ''') + ) { + return TaskResult.failure("Release host APK doesn't contain metadata: flutterProjectType = module "); + } + + section('Check file access modes for read-only asset from Flutter module'); + + final String readonlyReleaseAssetFilePath = path.join( + hostApp.path, + 'SampleApp', + 'build', + 'intermediates', + 'merged_assets', + 'release', + 'out', + 'flutter_assets/assets/read-only.txt', + ); + final File readonlyReleaseAssetFile = File(readonlyReleaseAssetFilePath); + if (!exists(readonlyReleaseAssetFile)) { + return TaskResult.failure('Failed to copy read-only asset file'); + } + + modes = readonlyReleaseAssetFile.statSync().modeString(); + print('\nread-only.txt file access modes = $modes'); + if (modes != null && modes.compareTo(fileReadWriteMode) != 0) { + return TaskResult.failure('Failed to make assets user-readable and writable'); + } + + return TaskResult.success(null); + } on TaskResult catch (taskResult) { + return taskResult; + } catch (e) { + return TaskResult.failure(e.toString()); + } finally { + rmTree(tempDir); + } + }); +} diff --git a/dev/integration_tests/android_custom_host_app/README.md b/dev/integration_tests/android_custom_host_app/README.md new file mode 100644 index 0000000000..7547c878e5 --- /dev/null +++ b/dev/integration_tests/android_custom_host_app/README.md @@ -0,0 +1,8 @@ +# Android custom host app + +Android host app for a Flutter module created using +``` +$ flutter create -t module hello +``` +and placed in a sibling folder to (a clone of) the host app. +Used by the `module_custom_host_app_name_test.dart` device lab test. diff --git a/dev/integration_tests/android_custom_host_app/SampleApp/build.gradle b/dev/integration_tests/android_custom_host_app/SampleApp/build.gradle new file mode 100644 index 0000000000..5c63a09a53 --- /dev/null +++ b/dev/integration_tests/android_custom_host_app/SampleApp/build.gradle @@ -0,0 +1,27 @@ +// Copyright 2014 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +apply plugin: 'com.android.application' + +android { + compileSdkVersion 28 + + compileOptions { + sourceCompatibility 1.8 + targetCompatibility 1.8 + } + + defaultConfig { + applicationId "io.flutter.add2app" + minSdkVersion 16 + targetSdkVersion 28 + versionCode 1 + versionName "1.0" + } +} + +dependencies { + implementation project(':flutter') + implementation 'androidx.appcompat:appcompat:1.1.0' +} diff --git a/dev/integration_tests/android_custom_host_app/SampleApp/src/main/AndroidManifest.xml b/dev/integration_tests/android_custom_host_app/SampleApp/src/main/AndroidManifest.xml new file mode 100644 index 0000000000..2f3b2713cd --- /dev/null +++ b/dev/integration_tests/android_custom_host_app/SampleApp/src/main/AndroidManifest.xml @@ -0,0 +1,13 @@ + + + + + + + + diff --git a/dev/integration_tests/android_custom_host_app/SampleApp/src/main/java/io/flutter/add2app/MainActivity.java b/dev/integration_tests/android_custom_host_app/SampleApp/src/main/java/io/flutter/add2app/MainActivity.java new file mode 100644 index 0000000000..f9b74d4aad --- /dev/null +++ b/dev/integration_tests/android_custom_host_app/SampleApp/src/main/java/io/flutter/add2app/MainActivity.java @@ -0,0 +1,12 @@ +// Copyright 2014 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +package io.flutter.add2app; + +import android.os.Bundle; +import androidx.appcompat.app.AppCompatActivity; +import io.flutter.embedding.android.FlutterActivity; + +public class MainActivity extends FlutterActivity { +} diff --git a/dev/integration_tests/android_custom_host_app/build.gradle b/dev/integration_tests/android_custom_host_app/build.gradle new file mode 100644 index 0000000000..fdd798e8aa --- /dev/null +++ b/dev/integration_tests/android_custom_host_app/build.gradle @@ -0,0 +1,24 @@ +// Copyright 2014 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +buildscript { + repositories { + google() + jcenter() + } + dependencies { + classpath 'com.android.tools.build:gradle:3.5.0' + } +} + +allprojects { + repositories { + google() + jcenter() + } +} + +task clean(type: Delete) { + delete rootProject.buildDir +} diff --git a/dev/integration_tests/android_custom_host_app/gradle.properties b/dev/integration_tests/android_custom_host_app/gradle.properties new file mode 100644 index 0000000000..759a176741 --- /dev/null +++ b/dev/integration_tests/android_custom_host_app/gradle.properties @@ -0,0 +1,4 @@ +org.gradle.jvmargs=-Xmx1536m +android.useAndroidX=true +android.enableJetifier=true +flutter.hostAppProjectName=SampleApp diff --git a/dev/integration_tests/android_custom_host_app/gradle/wrapper/gradle-wrapper.properties b/dev/integration_tests/android_custom_host_app/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000000..f6c8f50ffd --- /dev/null +++ b/dev/integration_tests/android_custom_host_app/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Mon Jun 25 14:13:36 CEST 2018 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-all.zip diff --git a/dev/integration_tests/android_custom_host_app/local.properties b/dev/integration_tests/android_custom_host_app/local.properties new file mode 100644 index 0000000000..7fb9f64ad5 --- /dev/null +++ b/dev/integration_tests/android_custom_host_app/local.properties @@ -0,0 +1,8 @@ +## This file must *NOT* be checked into Version Control Systems, +# as it contains information specific to your local configuration. +# +# Location of the SDK. This is only used by Gradle. +# For customization when using a Version Control System, please read the +# header note. +#Thu Mar 26 18:32:27 GMT+07:00 2020 +sdk.dir=/Users/alekseyanisov/Library/Android/sdk diff --git a/dev/integration_tests/android_custom_host_app/settings.gradle b/dev/integration_tests/android_custom_host_app/settings.gradle new file mode 100644 index 0000000000..9d54e69fe0 --- /dev/null +++ b/dev/integration_tests/android_custom_host_app/settings.gradle @@ -0,0 +1,7 @@ +// Copyright 2014 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +include ':SampleApp' +setBinding(new Binding([gradle: this])) +evaluate(new File(settingsDir.parentFile, 'hello/.android/include_flutter.groovy')) diff --git a/packages/flutter_tools/gradle/flutter.gradle b/packages/flutter_tools/gradle/flutter.gradle index 3e26e4523a..175bbf1f64 100644 --- a/packages/flutter_tools/gradle/flutter.gradle +++ b/packages/flutter_tools/gradle/flutter.gradle @@ -721,8 +721,9 @@ class FlutterPlugin implements Plugin { return } // Flutter module included as a subproject in add to app. - Project appProject = project.rootProject.findProject(':app') - assert appProject != null + String hostAppProjectName = project.rootProject.hasProperty('flutter.hostAppProjectName') ? project.rootProject.property('flutter.hostAppProjectName') : "app" + Project appProject = project.rootProject.findProject(":${hostAppProjectName}") + assert appProject != null : "Project :${hostAppProjectName} doesn't exist. To custom the host app project name, set `org.gradle.project.flutter.hostAppProjectName=` in gradle.properties." appProject.afterEvaluate { assert appProject.android != null appProject.android.applicationVariants.all { appProjectVariant -> @@ -750,7 +751,7 @@ class FlutterPlugin implements Plugin { } Task mergeAssets = project .tasks - .findByPath(":app:merge${appProjectVariant.name.capitalize()}Assets") + .findByPath(":${hostAppProjectName}:merge${appProjectVariant.name.capitalize()}Assets") assert mergeAssets mergeAssets.dependsOn(copyFlutterAssetsTask) }