android_plugin_compilesdkversion_mismatch_test.dart 3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
// 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_testing/file_testing.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/io.dart';
import 'package:flutter_tools/src/cache.dart';

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

void main() {

  late Directory tempDir;

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

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

26
  test('error logged when plugin Android compileSdk version higher than project', () async {
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
    final String flutterBin = fileSystem.path.join(
      getFlutterRoot(),
      'bin',
      'flutter',
    );

    // Create dummy plugin
    processManager.runSync(<String>[
      flutterBin,
      ...getLocalEngineArguments(),
      'create',
      '--template=plugin',
      '--platforms=android',
      'test_plugin',
    ], workingDirectory: tempDir.path);

    final Directory pluginAppDir = tempDir.childDirectory('test_plugin');
    final File pluginGradleFile = pluginAppDir.childDirectory('android').childFile('build.gradle');
    expect(pluginGradleFile, exists);

    final String pluginBuildGradle = pluginGradleFile.readAsStringSync();

49 50
    // Bump up plugin compileSdk version to 31
    final RegExp androidCompileSdkVersionRegExp = RegExp(r'compileSdk ([0-9]+|flutter.compileSdkVersion)');
51
    final String newPluginGradleFile = pluginBuildGradle.replaceAll(
52
      androidCompileSdkVersionRegExp, 'compileSdk 31');
53 54 55 56 57 58 59 60 61
    pluginGradleFile.writeAsStringSync(newPluginGradleFile);

    final Directory pluginExampleAppDir = pluginAppDir.childDirectory('example');

    final File projectGradleFile = pluginExampleAppDir.childDirectory('android').childDirectory('app').childFile('build.gradle');
    expect(projectGradleFile, exists);

    final String projectBuildGradle = projectGradleFile.readAsStringSync();

62
    // Bump down plugin example app compileSdk version to 30
63
    final String newProjectGradleFile = projectBuildGradle.replaceAll(
64
      androidCompileSdkVersionRegExp, 'compileSdk 30');
65 66 67 68 69 70 71 72 73 74 75 76
    projectGradleFile.writeAsStringSync(newProjectGradleFile);

    // Run flutter build apk to build plugin example project
    final ProcessResult result = processManager.runSync(<String>[
      flutterBin,
      ...getLocalEngineArguments(),
      'build',
      'apk',
      '--target-platform=android-arm',
    ], workingDirectory: pluginExampleAppDir.path);

    // Check error message is thrown
77 78 79
    expect(
        result.stdout,
        contains(
80
            'Warning: The plugin test_plugin requires Android SDK version 31 or higher.'));
81 82 83 84 85 86 87 88 89
    expect(
      result.stderr,
      contains('One or more plugins require a higher Android SDK version.'),
    );
    expect(
        result.stderr,
        contains(
            'Fix this issue by adding the following to ${projectGradleFile.path}'));
  });
90
}