From dcbb4960eb82608177657399abd973cb3b5591d8 Mon Sep 17 00:00:00 2001 From: Matt Perry Date: Fri, 22 Jan 2016 13:20:54 -0500 Subject: [PATCH] 'flutter apk' now supports dynamically registered services. Third-party libraries can now provide their own mojo services. They do so by adding a config.yaml file to their pub package which contains - a list of service names and java classes which handles that service's registration. - a list of pre-built .jar files to statically link with the app's shell when building the app. --- examples/fitness/apk/AndroidManifest.xml | 6 +- .../domokit/fitness/FitnessApplication.java | 36 ------ examples/fitness/flutter.yaml | 2 + examples/fitness/lib/main.dart | 1 + packages/flutter/pubspec.yaml | 4 +- packages/flutter_tools/lib/src/artifacts.dart | 55 +++++++-- .../flutter_tools/lib/src/commands/apk.dart | 105 ++++++++++++++++-- packages/gcm/lib/apk/config.yaml | 13 +++ 8 files changed, 160 insertions(+), 62 deletions(-) delete mode 100644 examples/fitness/apk/src/org/domokit/fitness/FitnessApplication.java create mode 100644 packages/gcm/lib/apk/config.yaml diff --git a/examples/fitness/apk/AndroidManifest.xml b/examples/fitness/apk/AndroidManifest.xml index 2d36384715..69323d5817 100644 --- a/examples/fitness/apk/AndroidManifest.xml +++ b/examples/fitness/apk/AndroidManifest.xml @@ -13,12 +13,12 @@ - - + - + diff --git a/examples/fitness/apk/src/org/domokit/fitness/FitnessApplication.java b/examples/fitness/apk/src/org/domokit/fitness/FitnessApplication.java deleted file mode 100644 index ab3edb3228..0000000000 --- a/examples/fitness/apk/src/org/domokit/fitness/FitnessApplication.java +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright 2015 The Chromium 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 org.domokit.fitness; - -import android.content.Context; - -import org.chromium.mojo.system.Core; -import org.chromium.mojo.system.MessagePipeHandle; -import org.chromium.mojom.gcm.GcmService; -import org.domokit.gcm.RegistrationIntentService; -import org.domokit.sky.shell.ServiceFactory; -import org.domokit.sky.shell.ServiceRegistry; -import org.domokit.sky.shell.SkyApplication; - -/** - * Sky implementation of {@link android.app.Application}, managing application-level global - * state and initializations. - */ -public class FitnessApplication extends SkyApplication { - /** - * Override this function to register more services. - */ - protected void onServiceRegistryAvailable(ServiceRegistry registry) { - super.onServiceRegistryAvailable(registry); - - registry.register(GcmService.MANAGER.getName(), new ServiceFactory() { - @Override - public void connectToService(Context context, Core core, MessagePipeHandle pipe) { - GcmService.MANAGER.bind( - new RegistrationIntentService.MojoService(context), pipe); - } - }); - } -} diff --git a/examples/fitness/flutter.yaml b/examples/fitness/flutter.yaml index 942a5de5ed..f5325d2a4e 100644 --- a/examples/fitness/flutter.yaml +++ b/examples/fitness/flutter.yaml @@ -13,3 +13,5 @@ material-design-icons: - name: navigation/close - name: navigation/menu - name: navigation/more_vert +services: + - gcm diff --git a/examples/fitness/lib/main.dart b/examples/fitness/lib/main.dart index 311bfc7139..c44d54aa8c 100644 --- a/examples/fitness/lib/main.dart +++ b/examples/fitness/lib/main.dart @@ -175,5 +175,6 @@ initGcm() async { } main() { + initGcm(); runApp(new FitnessApp()); } diff --git a/packages/flutter/pubspec.yaml b/packages/flutter/pubspec.yaml index cee899cbb9..afc4e9b18d 100644 --- a/packages/flutter/pubspec.yaml +++ b/packages/flutter/pubspec.yaml @@ -8,8 +8,8 @@ dependencies: collection: '>=1.1.3 <2.0.0' intl: '>=0.12.4+2 <0.13.0' material_design_icons: '>=0.0.3 <0.1.0' - sky_engine: 0.0.85 - sky_services: 0.0.85 + sky_engine: 0.0.86 + sky_services: 0.0.86 vector_math: '>=1.4.5 <2.0.0' quiver: '>=0.21.4 <0.22.0' diff --git a/packages/flutter_tools/lib/src/artifacts.dart b/packages/flutter_tools/lib/src/artifacts.dart index e6a5f4f0d7..04b0d491fd 100644 --- a/packages/flutter_tools/lib/src/artifacts.dart +++ b/packages/flutter_tools/lib/src/artifacts.dart @@ -47,7 +47,7 @@ enum ArtifactType { snapshot, shell, mojo, - androidClassesDex, + androidClassesJar, androidIcuData, androidKeystore, androidLibSkyShell, @@ -124,8 +124,8 @@ class ArtifactStore { ), const Artifact._( name: 'Compiled Java code', - fileName: 'classes.dex', - type: ArtifactType.androidClassesDex, + fileName: 'classes.dex.jar', + type: ArtifactType.androidClassesJar, targetPlatform: TargetPlatform.android ), const Artifact._( @@ -219,14 +219,12 @@ class ArtifactStore { ); } - /// Download the artifacts.zip archive for the given platform from GCS - /// and extract it to the local cache. - static Future _doDownloadArtifactsFromZip(String platform) async { - String url = getCloudStorageBaseUrl(platform) + 'artifacts.zip'; + /// Download a file from the given URL and return the bytes. + static Future> _downloadFile(Uri url) async { logging.info('Downloading $url.'); HttpClient httpClient = new HttpClient(); - HttpClientRequest request = await httpClient.getUrl(Uri.parse(url)); + HttpClientRequest request = await httpClient.getUrl(url); HttpClientResponse response = await request.close(); logging.fine('Received response statusCode=${response.statusCode}'); if (response.statusCode != 200) @@ -237,13 +235,29 @@ class ArtifactStore { responseBody.add(chunk); } - Archive archive = new ZipDecoder().decodeBytes(responseBody.takeBytes()); + return responseBody.takeBytes(); + } + + /// Download a file from the given url and write it to the cache. + static Future _downloadFileToCache(Uri url, File cachedFile) async { + if (!cachedFile.parent.existsSync()) + cachedFile.parent.createSync(recursive: true); + + List fileBytes = await _downloadFile(url); + cachedFile.writeAsBytesSync(fileBytes, flush: true); + } + + /// Download the artifacts.zip archive for the given platform from GCS + /// and extract it to the local cache. + static Future _doDownloadArtifactsFromZip(String platform) async { + String url = getCloudStorageBaseUrl(platform) + 'artifacts.zip'; + List zipBytes = await _downloadFile(Uri.parse(url)); + + Archive archive = new ZipDecoder().decodeBytes(zipBytes); Directory cacheDir = _getCacheDirForPlatform(platform); for (ArchiveFile archiveFile in archive) { File cacheFile = new File(path.join(cacheDir.path, archiveFile.name)); - IOSink sink = cacheFile.openWrite(); - sink.add(archiveFile.content); - await sink.close(); + cacheFile.writeAsBytesSync(archiveFile.content, flush: true); } for (Artifact artifact in knownArtifacts) { @@ -306,6 +320,23 @@ class ArtifactStore { return cachedFile.path; } + static Future getThirdPartyFile(String urlStr, String cacheSubdir) async { + Uri url = Uri.parse(urlStr); + Directory baseDir = _getBaseCacheDir(); + Directory cacheDir = new Directory(path.join( + baseDir.path, 'third_party', cacheSubdir)); + File cachedFile = new File( + path.join(cacheDir.path, url.pathSegments[url.pathSegments.length-1])); + if (!cachedFile.existsSync()) { + await _downloadFileToCache(url, cachedFile); + if (!cachedFile.existsSync()) { + logging.severe('Unable to fetch third-party artifact: $url'); + throw new ProcessExit(2); + } + } + return cachedFile.path; + } + static void clear() { Directory cacheDir = _getBaseCacheDir(); logging.fine('Clearing cache directory ${cacheDir.path}'); diff --git a/packages/flutter_tools/lib/src/commands/apk.dart b/packages/flutter_tools/lib/src/commands/apk.dart index b9bfe124cf..c894d0bea8 100644 --- a/packages/flutter_tools/lib/src/commands/apk.dart +++ b/packages/flutter_tools/lib/src/commands/apk.dart @@ -3,9 +3,11 @@ // found in the LICENSE file. import 'dart:async'; +import 'dart:convert'; import 'dart:io'; import 'package:path/path.dart' as path; +import 'package:yaml/yaml.dart'; import '../artifacts.dart'; import '../base/file_system.dart'; @@ -21,6 +23,9 @@ const String _kDefaultAndroidManifestPath = 'apk/AndroidManifest.xml'; const String _kDefaultOutputPath = 'build/app.apk'; const String _kDefaultResourcesPath = 'apk/res'; +const String _kFlutterManifestPath = 'flutter.yaml'; +const String _kPubspecYamlPath = 'pubspec.yaml'; + // Alias of the key provided in the Chromium debug keystore const String _kDebugKeystoreKeyAlias = "chromiumdebugkey"; @@ -56,6 +61,7 @@ class _ApkBuilder { File _androidJar; File _aapt; + File _dx; File _zipalign; String _jarsigner; @@ -64,12 +70,25 @@ class _ApkBuilder { String buildTools = '$androidSdk/build-tools/$_kBuildToolsVersion'; _aapt = new File('$buildTools/aapt'); + _dx = new File('$buildTools/dx'); _zipalign = new File('$buildTools/zipalign'); _jarsigner = 'jarsigner'; } bool checkSdkPath() { - return (_androidJar.existsSync() && _aapt.existsSync() && _zipalign.existsSync()); + return (_androidJar.existsSync() && _aapt.existsSync() && _dx.existsSync() && _zipalign.existsSync()); + } + + void compileClassesDex(File classesDex, List jars) { + List packageArgs = [_dx.path, + '--dex', + '--force-jumbo', + '--output', classesDex.path + ]; + + packageArgs.addAll(jars.map((File f) => f.path)); + + runCheckedSync(packageArgs); } void package(File outputApk, File androidManifest, Directory assets, Directory artifacts, Directory resources) { @@ -106,12 +125,21 @@ class _ApkComponents { Directory androidSdk; File manifest; File icuData; - File classesDex; + List jars; + List> services = []; File libSkyShell; File debugKeystore; Directory resources; } +// TODO(mpcomplete): find a better home for this. +dynamic _loadYamlFile(String path) { + if (!FileSystemEntity.isFileSync(path)) + return null; + String manifestString = new File(path).readAsStringSync(); + return loadYaml(manifestString); +} + class ApkCommand extends FlutterCommand { final String name = 'apk'; final String description = 'Build an Android APK package.'; @@ -151,6 +179,36 @@ class ApkCommand extends FlutterCommand { help: 'Password for the entry within the keystore.'); } + Future _findServices(_ApkComponents components) async { + if (!ArtifactStore.isPackageRootValid) + return; + + dynamic manifest = _loadYamlFile(_kFlutterManifestPath); + if (manifest['services'] == null) + return; + + for (String service in manifest['services']) { + String serviceRoot = '${ArtifactStore.packageRoot}/$service/apk'; + dynamic serviceConfig = _loadYamlFile('$serviceRoot/config.yaml'); + if (serviceConfig == null || serviceConfig['jars'] == null) + continue; + components.services.addAll(serviceConfig['services']); + for (String jar in serviceConfig['jars']) { + // Jar might refer to an android SDK jar, or URL to download. + if (jar.startsWith("android-sdk:")) { + jar = jar.replaceAll('android-sdk:', '${components.androidSdk.path}/'); + components.jars.add(new File(jar)); + } else if (jar.startsWith("http")) { + String cachePath = await ArtifactStore.getThirdPartyFile(jar, service); + components.jars.add(new File(cachePath)); + } else { + logging.severe('Service depends on a jar in an unrecognized format: $jar'); + throw new ProcessExit(2); + } + } + } + } + Future<_ApkComponents> _findApkComponents(BuildConfiguration config) async { String androidSdkPath; List artifactPaths; @@ -158,7 +216,7 @@ class ApkCommand extends FlutterCommand { androidSdkPath = '${runner.enginePath}/third_party/android_tools/sdk'; artifactPaths = [ '${runner.enginePath}/third_party/icu/android/icudtl.dat', - '${config.buildDir}/gen/sky/shell/shell/classes.dex', + '${config.buildDir}/gen/sky/shell/shell/classes.dex.jar', '${config.buildDir}/gen/sky/shell/shell/shell/libs/armeabi-v7a/libsky_shell.so', '${runner.enginePath}/build/android/ant/chromium-debug.keystore', ]; @@ -169,7 +227,7 @@ class ApkCommand extends FlutterCommand { } List artifactTypes = [ ArtifactType.androidIcuData, - ArtifactType.androidClassesDex, + ArtifactType.androidClassesJar, ArtifactType.androidLibSkyShell, ArtifactType.androidKeystore, ]; @@ -183,11 +241,13 @@ class ApkCommand extends FlutterCommand { components.androidSdk = new Directory(androidSdkPath); components.manifest = new File(argResults['manifest']); components.icuData = new File(artifactPaths[0]); - components.classesDex = new File(artifactPaths[1]); + components.jars = [new File(artifactPaths[1])]; components.libSkyShell = new File(artifactPaths[2]); components.debugKeystore = new File(artifactPaths[3]); components.resources = new Directory(argResults['resources']); + await _findServices(components); + if (!components.resources.existsSync()) { // TODO(eseidel): This level should be higher when path is manually set. logging.info('Can not locate Resources: ${components.resources}, ignoring.'); @@ -204,8 +264,9 @@ class ApkCommand extends FlutterCommand { logging.severe('and version $_kBuildToolsVersion of the build tools.'); return null; } - for (File f in [components.manifest, components.icuData, components.classesDex, - components.libSkyShell, components.debugKeystore]) { + for (File f in [components.manifest, components.icuData, + components.libSkyShell, components.debugKeystore] + ..addAll(components.jars)) { if (!f.existsSync()) { logging.severe('Can not locate file: ${f.path}'); return null; @@ -215,18 +276,44 @@ class ApkCommand extends FlutterCommand { return components; } + // Outputs a services.json file for the flutter engine to read. Format: + // { + // services: [ + // { name: string, class: string }, + // ... + // ] + // } + void _generateServicesConfig(File servicesConfig, List> servicesIn) { + List> services = + servicesIn.map((Map service) => { + 'name': service['name'], + 'class': service['registration-class'] + }).toList(); + + Map json = { 'services': services }; + servicesConfig.writeAsStringSync(JSON.encode(json), mode: FileMode.WRITE, flush: true); + } + int _buildApk(_ApkComponents components, String flxPath) { Directory tempDir = Directory.systemTemp.createTempSync('flutter_tools'); try { + _ApkBuilder builder = new _ApkBuilder(components.androidSdk.path); + + File classesDex = new File('${tempDir.path}/classes.dex'); + builder.compileClassesDex(classesDex, components.jars); + + File servicesConfig = new File('${tempDir.path}/services.json'); + _generateServicesConfig(servicesConfig, components.services); + _AssetBuilder assetBuilder = new _AssetBuilder(tempDir, 'assets'); assetBuilder.add(components.icuData, 'icudtl.dat'); assetBuilder.add(new File(flxPath), 'app.flx'); + assetBuilder.add(servicesConfig, 'services.json'); _AssetBuilder artifactBuilder = new _AssetBuilder(tempDir, 'artifacts'); - artifactBuilder.add(components.classesDex, 'classes.dex'); + artifactBuilder.add(classesDex, 'classes.dex'); artifactBuilder.add(components.libSkyShell, 'lib/armeabi-v7a/libsky_shell.so'); - _ApkBuilder builder = new _ApkBuilder(components.androidSdk.path); File unalignedApk = new File('${tempDir.path}/app.apk.unaligned'); builder.package(unalignedApk, components.manifest, assetBuilder.directory, artifactBuilder.directory, components.resources); diff --git a/packages/gcm/lib/apk/config.yaml b/packages/gcm/lib/apk/config.yaml new file mode 100644 index 0000000000..cefe41ddcb --- /dev/null +++ b/packages/gcm/lib/apk/config.yaml @@ -0,0 +1,13 @@ +services: + - name: gcm::GcmService + registration-class: org.domokit.gcm.RegistrationIntentService$MojoService +jars: + - android-sdk:extras/google/google_play_services/libproject/google-play-services_lib/libs/google-play-services.jar + - android-sdk:extras/android/support/v13/android-support-v13.jar + - android-sdk:extras/android/support/v7/appcompat/libs/android-support-v7-appcompat.jar + - android-sdk:extras/android/support/v7/mediarouter/libs/android-support-v7-mediarouter.jar + - https://storage.googleapis.com/mojo_infra/flutter/c03a5dda7e78c50d74fe5e5ed72eed1de6ae993d/android-arm/gcm/gcm_lib.dex.jar + - https://storage.googleapis.com/mojo_infra/flutter/c03a5dda7e78c50d74fe5e5ed72eed1de6ae993d/android-arm/gcm/interfaces_java.dex.jar +todo: > + Maybe we should link to a URL to download the jars. Might need different + versions per platform, etc.