gradle_non_android_plugin_test.dart 2.68 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:file/file.dart';
import 'package:file_testing/file_testing.dart';
7
import 'package:flutter_tools/src/base/io.dart';
8 9 10 11 12

import '../src/common.dart';
import 'test_utils.dart';

void main() {
13
  late Directory tempDir;
14 15 16 17 18 19 20 21 22

  setUp(() {
    tempDir = createResolvedTempDirectorySync('flutter_plugin_test.');
  });

  tearDown(() {
    tryToDelete(tempDir);
  });

23
  testWithoutContext('Flutter app that depends on a non-Android plugin can still build for Android', () {
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
    final String flutterRoot = getFlutterRoot();
    final String flutterBin = fileSystem.path.join(
      flutterRoot,
      'bin',
      'flutter',
    );
    processManager.runSync(<String>[
      flutterBin,
      ...getLocalEngineArguments(),
      'create',
      '-t',
      'plugin',
      '--platforms=ios,android',
      'ios_only',
    ], workingDirectory: tempDir.path);

    // Delete plugin's Android folder
    final Directory projectRoot = tempDir.childDirectory('ios_only');
    projectRoot.childDirectory('android').deleteSync(recursive: true);

    // Update pubspec.yaml to iOS only plugin
    final File pubspecFile = projectRoot.childFile('pubspec.yaml');
    final String pubspecString = pubspecFile.readAsStringSync();

    final StringBuffer iosOnlyPubspec = StringBuffer();
    bool inAndroidSection = false;
    const String pluginPlatformIndentation = '      ';
    for (final String line in pubspecString.split('\n')) {
      // Skip everything in the Android section of the plugin platforms list.
      if (line.startsWith('${pluginPlatformIndentation}android:')) {
        inAndroidSection = true;
        continue;
      }
      if (inAndroidSection) {
        if (line.startsWith('$pluginPlatformIndentation  ')) {
          continue;
        } else {
          inAndroidSection = false;
        }
      }
      iosOnlyPubspec.write('$line\n');
    }

    pubspecFile.writeAsStringSync(iosOnlyPubspec.toString());

    // Build example APK
    final Directory exampleDir = projectRoot.childDirectory('example');

72
    final ProcessResult result = processManager.runSync(<String>[
73 74 75 76 77 78 79 80 81
      flutterBin,
      ...getLocalEngineArguments(),
      'build',
      'apk',
      '--target-platform',
      'android-arm',
      '--verbose',
    ], workingDirectory: exampleDir.path);

82
    expect(result, const ProcessResultMatcher());
83

84 85 86 87 88 89 90 91 92 93
    final String exampleAppApk = fileSystem.path.join(
      exampleDir.path,
      'build',
      'app',
      'outputs',
      'apk',
      'release',
      'app-release.apk',
    );
    expect(fileSystem.file(exampleAppApk), exists);
94
  });
95
}