android_obfuscate_test.dart 5.2 KB
Newer Older
1 2 3 4 5 6
// Copyright 2014 The Flutter 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 'package:flutter_devicelab/framework/apk_utils.dart';
import 'package:flutter_devicelab/framework/framework.dart';
7
import 'package:flutter_devicelab/framework/task_result.dart';
8 9 10 11 12 13
import 'package:flutter_devicelab/framework/utils.dart';
import 'package:path/path.dart' as path;

Future<void> main() async {
  await task(() async {
    try {
14
      bool foundApkProjectName = false;
15 16 17 18 19 20 21 22
      await runProjectTest((FlutterProject flutterProject) async {
        section('APK content for task assembleRelease with --obfuscate');
        await inDirectory(flutterProject.rootPath, () async {
          await flutter('build', options: <String>[
            'apk',
            '--target-platform=android-arm',
            '--obfuscate',
            '--split-debug-info=foo/',
23
            '--verbose',
24 25
          ]);
        });
26
        final String outputApkDirectory = path.join(
27 28 29
          flutterProject.rootPath,
          'build/app/outputs/apk/release/app-release.apk',
        );
30
        final Iterable<String> apkFiles = await getFilesInApk(outputApkDirectory);
31 32 33 34 35 36 37 38 39 40

        checkCollectionContains<String>(<String>[
          ...flutterAssets,
          ...baseApkFiles,
          'lib/armeabi-v7a/libapp.so',
        ], apkFiles);

        // Verify that an identifier from the Dart project code is not present
        // in the compiled binary.
        await inDirectory(flutterProject.rootPath, () async {
41 42
          await exec('unzip', <String>[outputApkDirectory]);
          checkFileExists(path.join(flutterProject.rootPath, 'lib/armeabi-v7a/libapp.so'));
43 44 45 46 47 48
          final String response = await eval(
            'grep',
            <String>[flutterProject.name, 'lib/armeabi-v7a/libapp.so'],
            canFail: true,
          );
          if (response.trim().contains('matches')) {
49
            foundApkProjectName = true;
50 51 52
          }
        });
      });
53

54 55
      bool foundProjectNameInOldAar = false;
      bool foundProjectNameInNewAar = false;
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
      await runModuleProjectTest((FlutterModuleProject flutterProject) async {
        section('AAR content with --obfuscate');

        await inDirectory(flutterProject.rootPath, () async {
          await flutter('build', options: <String>[
            'aar',
            '--target-platform=android-arm',
            '--obfuscate',
            '--split-debug-info=foo/',
            '--no-debug',
            '--no-profile',
            '--verbose',
          ]);
        });

71 72 73
        section('Check old _release AAR files');

        final String oldReleaseAar = path.join(
74 75 76
          flutterProject.rootPath,
          'build/host/outputs/repo/com/example/${flutterProject.name}/flutter_release/1.0/flutter_release-1.0.aar',
        );
77
        final Iterable<String> filesInOldAar = await getFilesInAar(oldReleaseAar);
78 79 80 81

        checkCollectionContains<String>(<String>[
          ...flutterAssets,
          'jni/armeabi-v7a/libapp.so',
82
        ], filesInOldAar);
83 84 85 86

        // Verify that an identifier from the Dart project code is not present
        // in the compiled binary.
        await inDirectory(flutterProject.rootPath, () async {
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
          await exec('unzip', <String>['-o', oldReleaseAar]);
          checkFileExists(path.join(flutterProject.rootPath, 'jni/armeabi-v7a/libapp.so'));
          final String response = await eval(
            'grep',
            <String>[flutterProject.name, 'jni/armeabi-v7a/libapp.so'],
            canFail: true,
          );
          if (response.trim().contains('matches')) {
            foundProjectNameInOldAar = true;
          }
        });

        section('Check new -release AAR files');

        final String newReleaseAar = path.join(
          flutterProject.rootPath,
          'build/host/outputs/repo/com/example/${flutterProject.name}/flutter/1.0/flutter-1.0-release.aar',
        );
        final Iterable<String> filesInNewAar = await getFilesInAar(newReleaseAar);

        checkCollectionContains<String>(<String>[
          ...flutterAssets,
          'jni/armeabi-v7a/libapp.so',
        ], filesInNewAar);

        await inDirectory(flutterProject.rootPath, () async {
          await exec('unzip', <String>['-o', newReleaseAar]);
114 115 116 117 118 119 120
          checkFileExists(path.join(flutterProject.rootPath, 'jni/armeabi-v7a/libapp.so'));
          final String response = await eval(
            'grep',
            <String>[flutterProject.name, 'jni/armeabi-v7a/libapp.so'],
            canFail: true,
          );
          if (response.trim().contains('matches')) {
121
            foundProjectNameInNewAar = true;
122 123 124 125
          }
        });
      });

126

127 128 129
      if (foundApkProjectName) {
        return TaskResult.failure('Found project name in obfuscated APK dart library');
      }
130 131 132 133 134
      if (foundProjectNameInOldAar) {
        return TaskResult.failure('Found project name in obfuscated AAR _release dart library');
      }
      if (foundProjectNameInNewAar) {
        return TaskResult.failure('Found project name in obfuscated AAR -release dart library');
135
      }
136

137 138 139 140 141 142 143 144
      return TaskResult.success(null);
    } on TaskResult catch (taskResult) {
      return taskResult;
    } catch (e) {
      return TaskResult.failure(e.toString());
    }
  });
}