build_aar_module_test.dart 10.1 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 (old format)');
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 releasePomOld = 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(releasePomOld);
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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
      section('Check release Maven artifacts (new format)');

      checkFileExists(path.join(
        repoPath,
        'io',
        'flutter',
        'devicelab',
        'hello',
        'flutter',
        '1.0',
        'flutter-1.0-release.aar',
      ));

      final String releasePomNew = path.join(
        repoPath,
        'io',
        'flutter',
        'devicelab',
        'hello',
        'flutter',
        '1.0',
        'flutter-1.0.pom',
      );

      checkFileExists(releasePomNew);

      checkFileExists(path.join(
        repoPath,
        'io',
        'flutter',
        'devicelab',
        'plugin_with_android',
        'plugin_with_android',
        '1.0',
        'plugin_with_android-1.0-release.aar',
      ));

      checkFileExists(path.join(
        repoPath,
        'io',
        'flutter',
        'devicelab',
        'plugin_with_android',
        'plugin_with_android',
        '1.0',
        'plugin_with_android-1.0.pom',
      ));

194
      section('Check AOT blobs in release POM');
195

196 197 198 199
      checkFileContains(<String>[
        'flutter_embedding_release',
        'armeabi_v7a_release',
        'arm64_v8a_release',
200
        'x86_64_release',
201 202 203 204 205 206 207 208 209 210 211
        'plugin_with_android',
        '<relocation>', //make sure the old pom contains the <relocation> tag
      ], releasePomOld);

      checkFileContains(<String>[
        'flutter_embedding_release',
        'armeabi_v7a_release',
        'arm64_v8a_release',
        'x86_64_release',
        'plugin_with_android',
      ], releasePomNew);
212

213 214
      section('Check assets in release AAR');

215
      checkCollectionContains<String>(
216 217 218 219 220
        <String>[
          ...flutterAssets,
          // AOT snapshots
          'jni/arm64-v8a/libapp.so',
          'jni/armeabi-v7a/libapp.so',
221
          'jni/x86_64/libapp.so',
222 223 224 225 226 227 228 229 230 231 232 233 234 235
        ],
        await getFilesInAar(
          path.join(
            repoPath,
            'io',
            'flutter',
            'devicelab',
            'hello',
            'flutter_release',
            '1.0',
            'flutter_release-1.0.aar',
          )
        )
      );
236

237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
      checkCollectionContains<String>(
          <String>[
            ...flutterAssets,
            // AOT snapshots
            'jni/arm64-v8a/libapp.so',
            'jni/armeabi-v7a/libapp.so',
            'jni/x86_64/libapp.so',
          ],
          await getFilesInAar(
              path.join(
                repoPath,
                'io',
                'flutter',
                'devicelab',
                'hello',
                'flutter',
                '1.0',
                'flutter-1.0-release.aar',
              )
          )
      );

      section('Check debug Maven artifacts (old format)');
260

261 262 263 264 265 266
      checkFileExists(path.join(
        repoPath,
        'io',
        'flutter',
        'devicelab',
        'hello',
267
        'flutter_debug',
268
        '1.0',
269
        'flutter_debug-1.0.aar',
270 271
      ));

272
      final String debugPomOld = path.join(
273 274 275 276 277 278 279 280
        repoPath,
        'io',
        'flutter',
        'devicelab',
        'hello',
        'flutter_debug',
        '1.0',
        'flutter_debug-1.0.pom',
281
      );
282

283
      checkFileExists(debugPomOld);
284

285 286 287 288
      checkFileExists(path.join(
        repoPath,
        'io',
        'flutter',
289 290 291
        'devicelab',
        'plugin_with_android',
        'plugin_with_android_debug',
292
        '1.0',
293
        'plugin_with_android_debug-1.0.aar',
294 295 296 297 298 299
      ));

      checkFileExists(path.join(
        repoPath,
        'io',
        'flutter',
300 301 302
        'devicelab',
        'plugin_with_android',
        'plugin_with_android_debug',
303
        '1.0',
304
        'plugin_with_android_debug-1.0.pom',
305 306
      ));

307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
      section('Check debug Maven artifacts (new format)');

      checkFileExists(path.join(
        repoPath,
        'io',
        'flutter',
        'devicelab',
        'hello',
        'flutter',
        '1.0',
        'flutter-1.0-debug.aar',
      ));

      final String debugPomNew = path.join(
        repoPath,
        'io',
        'flutter',
        'devicelab',
        'hello',
        'flutter',
        '1.0',
        'flutter-1.0.pom',
      );

      checkFileExists(debugPomNew);

      checkFileExists(path.join(
        repoPath,
        'io',
        'flutter',
        'devicelab',
        'plugin_with_android',
        'plugin_with_android',
        '1.0',
        'plugin_with_android-1.0-debug.aar',
      ));

      checkFileExists(path.join(
        repoPath,
        'io',
        'flutter',
        'devicelab',
        'plugin_with_android',
        'plugin_with_android',
        '1.0',
        'plugin_with_android-1.0.pom',
      ));

355
      section('Check AOT blobs in debug POM');
356

357 358 359 360 361 362
      checkFileContains(<String>[
        'flutter_embedding_debug',
        'x86_debug',
        'x86_64_debug',
        'armeabi_v7a_debug',
        'arm64_v8a_debug',
363 364 365 366 367 368 369 370 371 372 373 374
        'plugin_with_android',
        '<relocation>', //make sure the old pom contains the <relocation> tag
      ], debugPomOld);

      checkFileContains(<String>[
        'flutter_embedding_debug',
        'x86_debug',
        'x86_64_debug',
        'armeabi_v7a_debug',
        'arm64_v8a_debug',
        'plugin_with_android',
      ], debugPomNew);
375 376 377 378 379 380 381 382 383 384 385 386 387 388

      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',
      ));

389 390 391 392 393 394 395 396 397 398 399
      final Iterable<String> debugAarNew = await getFilesInAar(path.join(
        repoPath,
        'io',
        'flutter',
        'devicelab',
        'hello',
        'flutter',
        '1.0',
        'flutter-1.0-debug.aar',
      ));

400
      checkCollectionContains<String>(<String>[
401
        ...flutterAssets,
402
        ...debugAssets,
403 404
      ], debugAar);

405 406 407 408 409
      checkCollectionContains<String>(<String>[
        ...flutterAssets,
        ...debugAssets,
      ], debugAarNew);

410
      return TaskResult.success(null);
411 412
    } on TaskResult catch (taskResult) {
      return taskResult;
413 414 415
    } catch (e) {
      return TaskResult.failure(e.toString());
    } finally {
416
      rmTree(tempDir);
417 418 419
    }
  });
}