|
| 1 | +// Copyright 2014 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:io'; |
| 6 | + |
| 7 | +import 'package:flutter_devicelab/framework/apk_utils.dart'; |
| 8 | +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 | +Future<void> main() async { |
| 14 | + await task(() async { |
| 15 | + try { |
| 16 | + await runPluginProjectTest((FlutterPluginProject pluginProject) async { |
| 17 | + |
| 18 | + section('check main plugin file exists'); |
| 19 | + final File pluginMainKotlinFile = File( |
| 20 | + path.join( |
| 21 | + pluginProject.rootPath, |
| 22 | + 'android', |
| 23 | + 'src', |
| 24 | + 'main', |
| 25 | + 'kotlin', |
| 26 | + path.join( |
| 27 | + 'com', |
| 28 | + 'example', |
| 29 | + 'aaa', |
| 30 | + 'AaaPlugin.kt', |
| 31 | + ), |
| 32 | + ), |
| 33 | + ); |
| 34 | + |
| 35 | + if (!pluginMainKotlinFile.existsSync()) { |
| 36 | + throw TaskResult.failure('Expected ${pluginMainKotlinFile.path} to exist, but it doesn\'t'); |
| 37 | + } |
| 38 | + |
| 39 | + section('add java 8 feature'); |
| 40 | + pluginMainKotlinFile.writeAsStringSync(r''' |
| 41 | +package com.example.aaa |
| 42 | +
|
| 43 | +import android.util.Log |
| 44 | +import androidx.annotation.NonNull |
| 45 | +
|
| 46 | +import io.flutter.embedding.engine.plugins.FlutterPlugin |
| 47 | +import io.flutter.plugin.common.MethodCall |
| 48 | +import io.flutter.plugin.common.MethodChannel |
| 49 | +import io.flutter.plugin.common.MethodChannel.MethodCallHandler |
| 50 | +import io.flutter.plugin.common.MethodChannel.Result |
| 51 | +
|
| 52 | +import java.util.HashMap |
| 53 | +
|
| 54 | +/** AaaPlugin */ |
| 55 | +class AaaPlugin: FlutterPlugin, MethodCallHandler { |
| 56 | + init { |
| 57 | + val map: HashMap<String, String> = HashMap<String, String>() |
| 58 | + // getOrDefault is a JAVA8 feature. |
| 59 | + Log.d("AaaPlugin", map.getOrDefault("foo", "baz")) |
| 60 | + } |
| 61 | + /// The MethodChannel that will the communication between Flutter and native Android |
| 62 | + /// |
| 63 | + /// This local reference serves to register the plugin with the Flutter Engine and unregister it |
| 64 | + /// when the Flutter Engine is detached from the Activity |
| 65 | + private lateinit var channel : MethodChannel |
| 66 | +
|
| 67 | + override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) { |
| 68 | + channel = MethodChannel(flutterPluginBinding.binaryMessenger, "aaa") |
| 69 | + channel.setMethodCallHandler(this) |
| 70 | + } |
| 71 | +
|
| 72 | + override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) { |
| 73 | + if (call.method == "getPlatformVersion") { |
| 74 | + result.success("Android ${android.os.Build.VERSION.RELEASE}") |
| 75 | + } else { |
| 76 | + result.notImplemented() |
| 77 | + } |
| 78 | + } |
| 79 | +
|
| 80 | + override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) { |
| 81 | + channel.setMethodCallHandler(null) |
| 82 | + } |
| 83 | +} |
| 84 | +'''); |
| 85 | + |
| 86 | + section('Compiles'); |
| 87 | + await inDirectory(pluginProject.exampleAndroidPath, () { |
| 88 | + return flutter( |
| 89 | + 'build', |
| 90 | + options: <String>[ |
| 91 | + 'apk', |
| 92 | + '--debug', |
| 93 | + '--target-platform=android-arm' |
| 94 | + ], |
| 95 | + ); |
| 96 | + }); |
| 97 | + |
| 98 | + }); |
| 99 | + return TaskResult.success(null); |
| 100 | + } on TaskResult catch (taskResult) { |
| 101 | + return taskResult; |
| 102 | + } catch (e) { |
| 103 | + return TaskResult.failure(e.toString()); |
| 104 | + } |
| 105 | + }); |
| 106 | +} |
0 commit comments