From c469b1fea92dfae21c0d641106220cc4823a16b7 Mon Sep 17 00:00:00 2001 From: Emmanuel Garcia Date: Thu, 25 Jul 2019 08:49:24 -0700 Subject: [PATCH] Add annotation dependency to plugins (#36886) Fixes #36817 --- dev/bots/test.dart | 1 + ...adle_plugins_without_annotations_test.dart | 138 ++++++++++++++++++ .../gradle/aar_init_script.gradle | 14 +- 3 files changed, 143 insertions(+), 10 deletions(-) create mode 100644 dev/devicelab/bin/tasks/gradle_plugins_without_annotations_test.dart diff --git a/dev/bots/test.dart b/dev/bots/test.dart index 4ebcfc32e0..398abf53c6 100644 --- a/dev/bots/test.dart +++ b/dev/bots/test.dart @@ -962,6 +962,7 @@ Future _androidGradleTests(String subShard) async { await _runDevicelabTest('gradle_plugin_light_apk_test', env: env); await _runDevicelabTest('gradle_plugin_fat_apk_test', env: env); await _runDevicelabTest('gradle_jetifier_test', env: env); + await _runDevicelabTest('gradle_plugins_without_annotations_test', env: env); await _runDevicelabTest('gradle_plugin_dependencies_test', env: env); await _runDevicelabTest('gradle_migrate_settings_test', env: env); } diff --git a/dev/devicelab/bin/tasks/gradle_plugins_without_annotations_test.dart b/dev/devicelab/bin/tasks/gradle_plugins_without_annotations_test.dart new file mode 100644 index 0000000000..f357c5ff4d --- /dev/null +++ b/dev/devicelab/bin/tasks/gradle_plugins_without_annotations_test.dart @@ -0,0 +1,138 @@ +// Copyright (c) 2019 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. + +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'; + +/// Tests that plugins that don't define a `androidx.annotation:annotation:+` or +/// `com.android.support:support-annotations:+` dependency can be built as AAR. +/// aar_init_script.gradle adds these dependencies manually. +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 app 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', + 'hello', + ], + ); + }); + + section('Add firebase_auth: 0.11.1+12 since it uses AndroidX annotations'); + // firebase_auth: 0.11.1+12 uses `androidx.annotation.NonNull` without having a + // dependency on `androidx.annotation:annotation:+` + + final File pubspec = File(path.join(projectDir.path, 'pubspec.yaml')); + String content = pubspec.readAsStringSync(); + content = content.replaceFirst( + '\ndependencies:\n', + '\ndependencies:\n firebase_auth: 0.11.1+12\n', + ); + pubspec.writeAsStringSync(content, flush: true); + await inDirectory(projectDir, () async { + await flutter( + 'packages', + options: ['get'], + ); + }); + + section('Build release APK'); + + await inDirectory(projectDir, () async { + await flutter( + 'build', + options: [ + 'apk', + '--target-platform', 'android-arm', + '--verbose', + ], + ); + }); + + final File releaseApk = File(path.join( + projectDir.path, + 'build', + 'app', + 'outputs', + 'apk', + 'release', + 'app-release.apk', + )); + + if (!exists(releaseApk)) { + return TaskResult.failure('Failed to build release APK.'); + } + + checkApkContainsClasses(releaseApk, [ + // The plugin class defined by `firebase_auth`. + 'io.flutter.plugins.firebaseauth.FirebaseAuthPlugin', + // Used by `firebase_auth`. + 'com.google.firebase.FirebaseApp', + ]); + + section('Build debug APK'); + + await inDirectory(projectDir, () async { + await flutter( + 'build', + options: [ + 'apk', + '--target-platform', 'android-arm', + '--debug', '--verbose', + ], + ); + }); + + final File debugApk = File(path.join( + projectDir.path, + 'build', + 'app', + 'outputs', + 'apk', + 'debug', + 'app-debug.apk', + )); + + if (!exists(debugApk)) { + return TaskResult.failure('Failed to build debug APK.'); + } + + checkApkContainsClasses(debugApk, [ + // The plugin class defined by `firebase_auth`. + 'io.flutter.plugins.firebaseauth.FirebaseAuthPlugin', + // Used by `firebase_auth`. + 'com.google.firebase.FirebaseApp', + ]); + + return TaskResult.success(null); + } catch (e) { + return TaskResult.failure(e.toString()); + } finally { + rmTree(tempDir); + } + }); +} diff --git a/packages/flutter_tools/gradle/aar_init_script.gradle b/packages/flutter_tools/gradle/aar_init_script.gradle index 1285c58819..2439da81c9 100644 --- a/packages/flutter_tools/gradle/aar_init_script.gradle +++ b/packages/flutter_tools/gradle/aar_init_script.gradle @@ -42,17 +42,11 @@ void configureProject(Project project, File outputDir) { // Check if the project uses the Flutter plugin (defined in flutter.gradle). Boolean usesFlutterPlugin = project.plugins.find { it.class.name == "FlutterPlugin" } != null if (!usesFlutterPlugin) { - // Plugins don't include their dependencies under the assumption that the parent project adds them. - if (project.properties['android.useAndroidX']) { - project.dependencies { - compileOnly "androidx.annotation:annotation:+" - } - } else { - project.dependencies { - compileOnly "com.android.support:support-annotations:+" - } - } project.dependencies { + // Some plugins don't include `annotations` and they don't set + // `android.useAndroidX=true` in `gradle.properties`. + compileOnly "androidx.annotation:annotation:+" + compileOnly "com.android.support:support-annotations:+" // The Flutter plugin already adds `flutter.jar`. compileOnly project.files("${getFlutterRoot(project)}/bin/cache/artifacts/engine/android-arm-release/flutter.jar") }