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

import 'dart:async';
6
import 'dart:io';
7 8 9 10

import 'package:flutter_devicelab/framework/apk_utils.dart';
import 'package:flutter_devicelab/framework/framework.dart';
import 'package:flutter_devicelab/framework/utils.dart';
11
import 'package:path/path.dart' as path;
12 13

Future<void> main() async {
14 15 16 17 18
  final Iterable<String> baseAabFiles = <String>[
    'base/dex/classes.dex',
    'base/manifest/AndroidManifest.xml',
  ];
  final Iterable<String> flutterAabAssets = flutterAssets.map((String file) => 'base/$file');
19 20
  await task(() async {
    try {
21
      await runProjectTest((FlutterProject project) async {
22
        section('App bundle content for task bundleRelease without explicit target platform');
23
        await project.runGradleTask('bundleRelease');
24

25
        final String releaseBundle = path.join(
26 27 28 29 30 31
          project.rootPath,
          'build',
          'app',
          'outputs',
          'bundle',
          'release',
32
          'app-release.aab',
33
        );
34
        checkCollectionContains<String>(<String>[
35 36
          ...baseAabFiles,
          ...flutterAabAssets,
37 38 39 40
          'base/lib/arm64-v8a/libapp.so',
          'base/lib/arm64-v8a/libflutter.so',
          'base/lib/armeabi-v7a/libapp.so',
          'base/lib/armeabi-v7a/libflutter.so',
41
        ], await getFilesInAppBundle(releaseBundle));
42 43
      });

44
      await runProjectTest((FlutterProject project) async {
45 46 47 48
        if (Platform.isWindows) {
          // https://github.com/flutter/flutter/issues/42985
          return;
        }
49 50
        section('App bundle content using flavors without explicit target platform');
        // Add a few flavors.
51 52 53 54 55 56
        await project.addProductFlavors(<String> [
          'production',
          'staging',
          'development',
          'flavor_underscore', // https://github.com/flutter/flutter/issues/36067
        ]);
57 58 59
        // Build the production flavor in release mode.
        await project.runGradleTask('bundleProductionRelease');

60 61 62 63 64 65 66
        final String bundleFromGradlePath = path.join(
          project.rootPath,
          'build',
          'app',
          'outputs',
          'bundle',
          'productionRelease',
67
          'app-production-release.aab',
68
        );
69
        checkCollectionContains<String>(<String>[
70 71
          ...baseAabFiles,
          ...flutterAabAssets,
72 73 74 75 76 77 78 79 80 81 82
          'base/lib/arm64-v8a/libapp.so',
          'base/lib/arm64-v8a/libflutter.so',
          'base/lib/armeabi-v7a/libapp.so',
          'base/lib/armeabi-v7a/libflutter.so',
        ], await getFilesInAppBundle(bundleFromGradlePath));

        section('Build app bundle using the flutter tool - flavor: flavor_underscore');

        int exitCode;
        await inDirectory(project.rootPath, () async {
          exitCode = await flutter(
83
            'build',
84 85 86 87 88
            options: <String>[
              'appbundle',
              '--flavor=flavor_underscore',
              '--verbose',
            ],
89
          );
90
        });
91

92 93 94
        if (exitCode != 0) {
          throw TaskResult.failure('flutter build appbundle command exited with code: $exitCode');
        }
95

96 97 98 99 100 101 102
        final String flavorUnderscoreBundlePath = path.join(
          project.rootPath,
          'build',
          'app',
          'outputs',
          'bundle',
          'flavor_underscoreRelease',
103
          'app-flavor_underscore-release.aab',
104
        );
105
        checkCollectionContains<String>(<String>[
106 107
          ...baseAabFiles,
          ...flutterAabAssets,
108 109 110 111
          'base/lib/arm64-v8a/libapp.so',
          'base/lib/arm64-v8a/libflutter.so',
          'base/lib/armeabi-v7a/libapp.so',
          'base/lib/armeabi-v7a/libflutter.so',
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
        ], await getFilesInAppBundle(flavorUnderscoreBundlePath));

        section('Build app bundle using the flutter tool - flavor: production');

        await inDirectory(project.rootPath, () async {
          exitCode = await flutter(
            'build',
            options: <String>[
              'appbundle',
              '--flavor=production',
              '--verbose',
            ],
          );
        });

        if (exitCode != 0) {
          throw TaskResult.failure('flutter build appbundle command exited with code: $exitCode');
        }

        final String productionBundlePath = path.join(
          project.rootPath,
          'build',
          'app',
          'outputs',
          'bundle',
          'productionRelease',
138
          'app-production-release.aab',
139
        );
140
        checkCollectionContains<String>(<String>[
141 142
          ...baseAabFiles,
          ...flutterAabAssets,
143 144 145 146 147
          'base/lib/arm64-v8a/libapp.so',
          'base/lib/arm64-v8a/libflutter.so',
          'base/lib/armeabi-v7a/libapp.so',
          'base/lib/armeabi-v7a/libflutter.so',
        ], await getFilesInAppBundle(productionBundlePath));
148 149 150
      });

      await runProjectTest((FlutterProject project) async {
151
        section('App bundle content for task bundleRelease with target platform = android-arm');
152
        await project.runGradleTask('bundleRelease',
153 154
            options: <String>['-Ptarget-platform=android-arm']);

155
        final String releaseBundle = path.join(
156 157 158 159 160 161
          project.rootPath,
          'build',
          'app',
          'outputs',
          'bundle',
          'release',
162
          'app-release.aab',
163
        );
164

165
        final Iterable<String> bundleFiles = await getFilesInAppBundle(releaseBundle);
166
        checkCollectionContains<String>(<String>[
167 168
          ...baseAabFiles,
          ...flutterAabAssets,
169 170 171 172
          'base/lib/armeabi-v7a/libapp.so',
          'base/lib/armeabi-v7a/libflutter.so',
        ], bundleFiles);

173
        checkCollectionDoesNotContain<String>(<String>[
174 175 176 177 178 179 180 181 182 183 184 185
          'base/lib/arm64-v8a/libapp.so',
          'base/lib/arm64-v8a/libflutter.so',
        ], bundleFiles);
      });
      return TaskResult.success(null);
    } on TaskResult catch (taskResult) {
      return taskResult;
    } catch (e) {
      return TaskResult.failure(e.toString());
    }
  });
}