build_aar_module_test.dart 6.72 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:io';

7
import 'package:flutter_devicelab/framework/apk_utils.dart';
8
import 'package:flutter_devicelab/framework/framework.dart';
9
import 'package:flutter_devicelab/framework/task_result.dart';
10 11 12
import 'package:flutter_devicelab/framework/utils.dart';
import 'package:path/path.dart' as path;

13 14
final String platformLineSep = Platform.isWindows ? '\r\n': '\n';

15 16 17 18 19 20
/// Tests that AARs can be built on module projects.
Future<void> main() async {
  await task(() async {

    section('Find Java');

21
    final String? javaHome = await findJavaHome();
22
    if (javaHome == null) {
23
      return TaskResult.failure('Could not find Java');
24
    }
25 26 27 28 29
    print('\nUsing JAVA_HOME=$javaHome');

    final Directory tempDir = Directory.systemTemp.createTempSync('flutter_module_test.');
    final Directory projectDir = Directory(path.join(tempDir.path, 'hello'));
    try {
30 31
      section('Create module project');

32 33 34 35 36 37 38
      await inDirectory(tempDir, () async {
        await flutter(
          'create',
          options: <String>['--org', 'io.flutter.devicelab', '--template', 'module', 'hello'],
        );
      });

39 40 41 42 43
      section('Create plugin that supports android platform');

      await inDirectory(tempDir, () async {
        await flutter(
          'create',
44
          options: <String>['--org', 'io.flutter.devicelab', '--template', 'plugin', '--platforms=android', 'plugin_with_android'],
45 46 47
        );
      });

48
      section("Create plugin that doesn't support android project");
49 50 51 52

      await inDirectory(tempDir, () async {
        await flutter(
          'create',
53
          options: <String>['--org', 'io.flutter.devicelab', '--template', 'plugin', '--platforms=ios', 'plugin_without_android'],
54 55
        );
      });
56

57 58 59 60
      section('Add plugins to pubspec.yaml');

      final File modulePubspec = File(path.join(projectDir.path, 'pubspec.yaml'));
      String content = modulePubspec.readAsStringSync();
61
      content = content.replaceFirst(
62 63 64 65 66
        '${platformLineSep}dependencies:$platformLineSep',
        '${platformLineSep}dependencies:$platformLineSep'
          '  plugin_with_android:$platformLineSep'
          '    path: ../plugin_with_android$platformLineSep'
          '  plugin_without_android:$platformLineSep'
67 68
          '    path: ../plugin_without_android$platformLineSep'
          '  webcrypto: 0.5.2$platformLineSep', // Plugin that uses NDK.
69
      );
70 71 72 73
      modulePubspec.writeAsStringSync(content, flush: true);

      section('Run packages get in module project');

74 75 76 77 78 79 80 81 82 83 84 85
      await inDirectory(projectDir, () async {
        await flutter(
          'packages',
          options: <String>['get'],
        );
      });

      section('Build release AAR');

      await inDirectory(projectDir, () async {
        await flutter(
          'build',
86
          options: <String>['aar', '--verbose'],
87 88 89 90 91 92 93 94 95 96 97
        );
      });

      final String repoPath = path.join(
        projectDir.path,
        'build',
        'host',
        'outputs',
        'repo',
      );

98
      section('Check release Maven artifacts');
99

100 101 102 103 104 105 106 107 108 109 110
      checkFileExists(path.join(
        repoPath,
        'io',
        'flutter',
        'devicelab',
        'hello',
        'flutter_release',
        '1.0',
        'flutter_release-1.0.aar',
      ));

111
      final String releasePom = path.join(
112 113 114 115 116 117 118 119
        repoPath,
        'io',
        'flutter',
        'devicelab',
        'hello',
        'flutter_release',
        '1.0',
        'flutter_release-1.0.pom',
120
      );
121

122
      checkFileExists(releasePom);
123

124 125 126 127
      checkFileExists(path.join(
        repoPath,
        'io',
        'flutter',
128 129 130
        'devicelab',
        'plugin_with_android',
        'plugin_with_android_release',
131
        '1.0',
132
        'plugin_with_android_release-1.0.aar',
133 134 135 136 137 138
      ));

      checkFileExists(path.join(
        repoPath,
        'io',
        'flutter',
139 140 141
        'devicelab',
        'plugin_with_android',
        'plugin_with_android_release',
142
        '1.0',
143
        'plugin_with_android_release-1.0.pom',
144 145
      ));

146
      section('Check AOT blobs in release POM');
147

148 149 150 151
      checkFileContains(<String>[
        'flutter_embedding_release',
        'armeabi_v7a_release',
        'arm64_v8a_release',
152
        'x86_64_release',
153 154
        'plugin_with_android_release',
      ], releasePom);
155

156 157
      section('Check assets in release AAR');

158
      checkCollectionContains<String>(
159 160 161 162 163
        <String>[
          ...flutterAssets,
          // AOT snapshots
          'jni/arm64-v8a/libapp.so',
          'jni/armeabi-v7a/libapp.so',
164
          'jni/x86_64/libapp.so',
165 166 167 168 169 170 171 172 173 174 175 176 177 178
        ],
        await getFilesInAar(
          path.join(
            repoPath,
            'io',
            'flutter',
            'devicelab',
            'hello',
            'flutter_release',
            '1.0',
            'flutter_release-1.0.aar',
          )
        )
      );
179

180
      section('Check debug Maven artifacts');
181

182 183 184 185 186 187
      checkFileExists(path.join(
        repoPath,
        'io',
        'flutter',
        'devicelab',
        'hello',
188
        'flutter_debug',
189
        '1.0',
190
        'flutter_debug-1.0.aar',
191 192
      ));

193
      final String debugPom = path.join(
194 195 196 197 198 199 200 201
        repoPath,
        'io',
        'flutter',
        'devicelab',
        'hello',
        'flutter_debug',
        '1.0',
        'flutter_debug-1.0.pom',
202
      );
203

204
      checkFileExists(debugPom);
205

206 207 208 209
      checkFileExists(path.join(
        repoPath,
        'io',
        'flutter',
210 211 212
        'devicelab',
        'plugin_with_android',
        'plugin_with_android_debug',
213
        '1.0',
214
        'plugin_with_android_debug-1.0.aar',
215 216 217 218 219 220
      ));

      checkFileExists(path.join(
        repoPath,
        'io',
        'flutter',
221 222 223
        'devicelab',
        'plugin_with_android',
        'plugin_with_android_debug',
224
        '1.0',
225
        'plugin_with_android_debug-1.0.pom',
226 227
      ));

228
      section('Check AOT blobs in debug POM');
229

230 231 232 233 234 235
      checkFileContains(<String>[
        'flutter_embedding_debug',
        'x86_debug',
        'x86_64_debug',
        'armeabi_v7a_debug',
        'arm64_v8a_debug',
236 237
        'plugin_with_android_debug',
      ], debugPom);
238 239 240 241 242 243 244 245 246 247 248 249 250 251

      section('Check assets in debug AAR');

      final Iterable<String> debugAar = await getFilesInAar(path.join(
        repoPath,
        'io',
        'flutter',
        'devicelab',
        'hello',
        'flutter_debug',
        '1.0',
        'flutter_debug-1.0.aar',
      ));

252
      checkCollectionContains<String>(<String>[
253
        ...flutterAssets,
254
        ...debugAssets,
255 256
      ], debugAar);

257
      return TaskResult.success(null);
258 259
    } on TaskResult catch (taskResult) {
      return taskResult;
260 261 262
    } catch (e) {
      return TaskResult.failure(e.toString());
    } finally {
263
      rmTree(tempDir);
264 265 266
    }
  });
}