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

import 'dart:io';

import 'package:flutter_devicelab/framework/apk_utils.dart';
import 'package:flutter_devicelab/framework/framework.dart';
9
import 'package:flutter_devicelab/framework/task_result.dart';
10
import 'package:flutter_devicelab/framework/utils.dart';
11
import 'package:path/path.dart' as path;
12 13 14 15 16 17

Future<void> main() async {
  await task(() async {
    try {
      await runPluginProjectTest((FlutterPluginProject pluginProject) async {
        section('APK content for task assembleDebug without explicit target platform');
18 19 20 21 22 23
        await inDirectory(pluginProject.exampleAndroidPath, () {
          return flutter(
            'build',
            options: <String>[
              'apk',
              '--debug',
24
              '--verbose',
25 26 27
            ],
          );
        });
28

29
        Iterable<String> apkFiles = await getFilesInApk(pluginProject.debugApkPath);
30

31
        checkCollectionContains<String>(<String>[
32
          ...flutterAssets,
33 34
          ...debugAssets,
          ...baseApkFiles,
35
          'lib/armeabi-v7a/libflutter.so',
36
          'lib/arm64-v8a/libflutter.so',
37 38 39 40 41
          // Debug mode intentionally includes `x86` and `x86_64`.
          'lib/x86/libflutter.so',
          'lib/x86_64/libflutter.so',
        ], apkFiles);

42
        checkCollectionDoesNotContain<String>(<String>[
43 44 45 46 47 48 49
          'lib/arm64-v8a/libapp.so',
          'lib/armeabi-v7a/libapp.so',
          'lib/x86/libapp.so',
          'lib/x86_64/libapp.so',
        ], apkFiles);

        section('APK content for task assembleRelease without explicit target platform');
50 51 52 53 54 55 56

        await inDirectory(pluginProject.exampleAndroidPath, () {
          return flutter(
            'build',
            options: <String>[
              'apk',
              '--release',
57
              '--verbose',
58 59 60
            ],
          );
        });
61

62
        apkFiles = await getFilesInApk(pluginProject.releaseApkPath);
63

64
        checkCollectionContains<String>(<String>[
65
          ...flutterAssets,
66
          ...baseApkFiles,
67 68
          'lib/armeabi-v7a/libflutter.so',
          'lib/armeabi-v7a/libapp.so',
69 70
          'lib/arm64-v8a/libflutter.so',
          'lib/arm64-v8a/libapp.so',
71 72
          'lib/x86_64/libflutter.so',
          'lib/x86_64/libapp.so',
73 74
        ], apkFiles);

75
        checkCollectionDoesNotContain<String>(debugAssets, apkFiles);
76 77

        section('APK content for task assembleRelease with target platform = android-arm, android-arm64');
78 79 80 81 82 83 84

        await inDirectory(pluginProject.exampleAndroidPath, () {
          return flutter(
            'build',
            options: <String>[
              'apk',
              '--release',
85 86
              '--verbose',
              '--target-platform=android-arm,android-arm64',
87 88 89
            ],
          );
        });
90

91
        apkFiles = await getFilesInApk(pluginProject.releaseApkPath);
92

93
        checkCollectionContains<String>(<String>[
94
          ...flutterAssets,
95
          ...baseApkFiles,
96 97 98 99 100 101
          'lib/armeabi-v7a/libflutter.so',
          'lib/armeabi-v7a/libapp.so',
          'lib/arm64-v8a/libflutter.so',
          'lib/arm64-v8a/libapp.so',
        ], apkFiles);

102
        checkCollectionDoesNotContain<String>(debugAssets, apkFiles);
103 104 105

        section('APK content for task assembleRelease with '
                'target platform = android-arm, android-arm64 and split per ABI');
106 107 108 109 110 111 112

        await inDirectory(pluginProject.exampleAndroidPath, () {
          return flutter(
            'build',
            options: <String>[
              'apk',
              '--release',
113
              '--verbose',
114 115 116 117 118
              '--split-per-abi',
              '--target-platform=android-arm,android-arm64',
            ],
          );
        });
119

120
        final Iterable<String> armApkFiles = await getFilesInApk(pluginProject.releaseArmApkPath);
121

122
        checkCollectionContains<String>(<String>[
123
          ...flutterAssets,
124
          ...baseApkFiles,
125 126 127 128
          'lib/armeabi-v7a/libflutter.so',
          'lib/armeabi-v7a/libapp.so',
        ], armApkFiles);

129
        checkCollectionDoesNotContain<String>(debugAssets, armApkFiles);
130

131
        final Iterable<String> arm64ApkFiles = await getFilesInApk(pluginProject.releaseArm64ApkPath);
132

133
        checkCollectionContains<String>(<String>[
134
          ...flutterAssets,
135
          ...baseApkFiles,
136 137 138 139
          'lib/arm64-v8a/libflutter.so',
          'lib/arm64-v8a/libapp.so',
        ], arm64ApkFiles);

140
        checkCollectionDoesNotContain<String>(debugAssets, arm64ApkFiles);
141 142 143 144
      });

      await runProjectTest((FlutterProject project) async {
        section('gradlew assembleRelease');
145 146 147 148 149 150 151

        await inDirectory(project.rootPath, () {
          return flutter(
            'build',
            options: <String>[
              'apk',
              '--release',
152
              '--verbose',
153 154 155
            ],
          );
        });
156 157 158 159

        // When the platform-target isn't specified, we generate the snapshots
        // for arm and arm64.
        final List<String> targetPlatforms = <String>[
160 161
          'arm64-v8a',
          'armeabi-v7a',
162 163 164
        ];
        for (final String targetPlatform in targetPlatforms) {
          final String androidArmSnapshotPath = path.join(
165 166 167 168 169 170 171 172
            project.rootPath,
            'build',
            'app',
            'intermediates',
            'flutter',
            'release',
            targetPlatform,
          );
173 174 175

          final String sharedLibrary = path.join(androidArmSnapshotPath, 'app.so');
          if (!File(sharedLibrary).existsSync()) {
176
            throw TaskResult.failure("Shared library doesn't exist");
177 178 179 180 181 182 183 184 185 186 187 188
          }
        }
      });

      return TaskResult.success(null);
    } on TaskResult catch (taskResult) {
      return taskResult;
    } catch (e) {
      return TaskResult.failure(e.toString());
    }
  });
}