From 7ccf9524aa8054182c96ad90d1be4ef481304e4a Mon Sep 17 00:00:00 2001 From: Felix Angelov Date: Tue, 15 Feb 2022 14:05:46 -0600 Subject: [PATCH] feat(android): create my_plugin_android (#4) --- .github/workflows/my_plugin_android.yaml | 21 ++++++++ src/my_plugin_android/CHANGELOG.md | 3 ++ src/my_plugin_android/LICENSE | 21 ++++++++ src/my_plugin_android/README.md | 14 ++++++ src/my_plugin_android/android/.gitignore | 8 +++ src/my_plugin_android/android/build.gradle | 50 +++++++++++++++++++ src/my_plugin_android/android/settings.gradle | 1 + .../android/src/main/AndroidManifest.xml | 3 ++ .../com/example/my_plugin/MyPluginPlugin.kt | 34 +++++++++++++ .../lib/my_plugin_android.dart | 20 ++++++++ src/my_plugin_android/pubspec.yaml | 29 +++++++++++ .../test/my_plugin_android_test.dart | 44 ++++++++++++++++ 12 files changed, 248 insertions(+) create mode 100644 .github/workflows/my_plugin_android.yaml create mode 100644 src/my_plugin_android/CHANGELOG.md create mode 100644 src/my_plugin_android/LICENSE create mode 100644 src/my_plugin_android/README.md create mode 100644 src/my_plugin_android/android/.gitignore create mode 100644 src/my_plugin_android/android/build.gradle create mode 100644 src/my_plugin_android/android/settings.gradle create mode 100644 src/my_plugin_android/android/src/main/AndroidManifest.xml create mode 100644 src/my_plugin_android/android/src/main/kotlin/com/example/my_plugin/MyPluginPlugin.kt create mode 100644 src/my_plugin_android/lib/my_plugin_android.dart create mode 100644 src/my_plugin_android/pubspec.yaml create mode 100644 src/my_plugin_android/test/my_plugin_android_test.dart diff --git a/.github/workflows/my_plugin_android.yaml b/.github/workflows/my_plugin_android.yaml new file mode 100644 index 0000000..bd3df3f --- /dev/null +++ b/.github/workflows/my_plugin_android.yaml @@ -0,0 +1,21 @@ +name: my_plugin_android + +on: + pull_request: + paths: + - ".github/workflows/my_plugin_android.yaml" + - "src/my_plugin_android/**" + push: + branches: + - main + paths: + - ".github/workflows/my_plugin_android.yaml" + - "src/my_plugin_android/**" + +jobs: + build: + uses: VeryGoodOpenSource/very_good_workflows/.github/workflows/flutter_package.yml@v1 + with: + flutter_channel: stable + flutter_version: 2.10.1 + working_directory: src/my_plugin_android diff --git a/src/my_plugin_android/CHANGELOG.md b/src/my_plugin_android/CHANGELOG.md new file mode 100644 index 0000000..1455983 --- /dev/null +++ b/src/my_plugin_android/CHANGELOG.md @@ -0,0 +1,3 @@ +# 0.1.0+1 + +- Initial release of this plugin. \ No newline at end of file diff --git a/src/my_plugin_android/LICENSE b/src/my_plugin_android/LICENSE new file mode 100644 index 0000000..bba8e50 --- /dev/null +++ b/src/my_plugin_android/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022 Very Good Ventures + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/src/my_plugin_android/README.md b/src/my_plugin_android/README.md new file mode 100644 index 0000000..54d824a --- /dev/null +++ b/src/my_plugin_android/README.md @@ -0,0 +1,14 @@ +# my_plugin_android + +[![style: very good analysis][very_good_analysis_badge]][very_good_analysis_link] + +The Android implementation of `my_plugin`. + +## Usage + +This package is [endorsed][endorsed_link], which means you can simply use `my_plugin` +normally. This package will be automatically included in your app when you do. + +[endorsed_link]: https://flutter.dev/docs/development/packages-and-plugins/developing-packages#endorsed-federated-plugin +[very_good_analysis_badge]: https://img.shields.io/badge/style-very_good_analysis-B22C89.svg +[very_good_analysis_link]: https://pub.dev/packages/very_good_analysis \ No newline at end of file diff --git a/src/my_plugin_android/android/.gitignore b/src/my_plugin_android/android/.gitignore new file mode 100644 index 0000000..2665975 --- /dev/null +++ b/src/my_plugin_android/android/.gitignore @@ -0,0 +1,8 @@ +*.iml +.gradle +/local.properties +/.idea/workspace.xml +/.idea/libraries +.DS_Store +/build +/captures \ No newline at end of file diff --git a/src/my_plugin_android/android/build.gradle b/src/my_plugin_android/android/build.gradle new file mode 100644 index 0000000..afe7670 --- /dev/null +++ b/src/my_plugin_android/android/build.gradle @@ -0,0 +1,50 @@ +group 'com.example.my_plugin' +version '1.0-SNAPSHOT' + +buildscript { + ext.kotlin_version = '1.3.50' + repositories { + google() + mavenCentral() + } + + dependencies { + classpath 'com.android.tools.build:gradle:4.1.0' + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" + } +} + +rootProject.allprojects { + repositories { + google() + mavenCentral() + } +} + +apply plugin: 'com.android.library' +apply plugin: 'kotlin-android' + +android { + compileSdkVersion 30 + + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } + + kotlinOptions { + jvmTarget = '1.8' + } + + sourceSets { + main.java.srcDirs += 'src/main/kotlin' + } + + defaultConfig { + minSdkVersion 16 + } +} + +dependencies { + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" +} \ No newline at end of file diff --git a/src/my_plugin_android/android/settings.gradle b/src/my_plugin_android/android/settings.gradle new file mode 100644 index 0000000..cbb9c70 --- /dev/null +++ b/src/my_plugin_android/android/settings.gradle @@ -0,0 +1 @@ +rootProject.name = 'my_plugin_android' diff --git a/src/my_plugin_android/android/src/main/AndroidManifest.xml b/src/my_plugin_android/android/src/main/AndroidManifest.xml new file mode 100644 index 0000000..c17e2b7 --- /dev/null +++ b/src/my_plugin_android/android/src/main/AndroidManifest.xml @@ -0,0 +1,3 @@ + + diff --git a/src/my_plugin_android/android/src/main/kotlin/com/example/my_plugin/MyPluginPlugin.kt b/src/my_plugin_android/android/src/main/kotlin/com/example/my_plugin/MyPluginPlugin.kt new file mode 100644 index 0000000..c0467f5 --- /dev/null +++ b/src/my_plugin_android/android/src/main/kotlin/com/example/my_plugin/MyPluginPlugin.kt @@ -0,0 +1,34 @@ +package com.example.my_plugin + +import android.content.Context +import androidx.annotation.NonNull + +import io.flutter.embedding.engine.plugins.FlutterPlugin +import io.flutter.plugin.common.MethodCall +import io.flutter.plugin.common.MethodChannel +import io.flutter.plugin.common.MethodChannel.MethodCallHandler +import io.flutter.plugin.common.MethodChannel.Result + +class MyPluginPlugin : FlutterPlugin, MethodCallHandler { + private lateinit var channel: MethodChannel + private var context: Context? = null + + override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) { + channel = MethodChannel(flutterPluginBinding.binaryMessenger, "my_plugin_android") + channel.setMethodCallHandler(this) + context = flutterPluginBinding.applicationContext + } + + override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) { + if (call.method == "getPlatformName") { + result.success("Android") + } else { + result.notImplemented() + } + } + + override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) { + channel.setMethodCallHandler(null) + context = null + } +} \ No newline at end of file diff --git a/src/my_plugin_android/lib/my_plugin_android.dart b/src/my_plugin_android/lib/my_plugin_android.dart new file mode 100644 index 0000000..bab3207 --- /dev/null +++ b/src/my_plugin_android/lib/my_plugin_android.dart @@ -0,0 +1,20 @@ +import 'package:flutter/foundation.dart'; +import 'package:flutter/services.dart'; +import 'package:my_plugin_platform_interface/my_plugin_platform_interface.dart'; + +/// The Android implementation of [MyPluginPlatform]. +class MyPluginAndroid extends MyPluginPlatform { + /// The method channel used to interact with the native platform. + @visibleForTesting + final methodChannel = const MethodChannel('my_plugin_android'); + + /// Registers this class as the default instance of [MyPluginPlatform] + static void registerWith() { + MyPluginPlatform.instance = MyPluginAndroid(); + } + + @override + Future getPlatformName() { + return methodChannel.invokeMethod('getPlatformName'); + } +} diff --git a/src/my_plugin_android/pubspec.yaml b/src/my_plugin_android/pubspec.yaml new file mode 100644 index 0000000..6b15d81 --- /dev/null +++ b/src/my_plugin_android/pubspec.yaml @@ -0,0 +1,29 @@ +name: my_plugin_android +description: Android implementation of the my_plugin plugin +version: 0.1.0+1 +publish_to: none + +environment: + sdk: ">=2.14.0 <3.0.0" + flutter: ">=2.5.0" + +flutter: + plugin: + implements: my_plugin + platforms: + android: + package: com.example.my_plugin + pluginClass: MyPluginPlugin + dartPluginClass: MyPluginAndroid + +dependencies: + flutter: + sdk: flutter + my_plugin_platform_interface: + path: ../my_plugin_platform_interface + +dev_dependencies: + flutter_test: + sdk: flutter + plugin_platform_interface: ^2.0.0 + very_good_analysis: ^2.4.0 diff --git a/src/my_plugin_android/test/my_plugin_android_test.dart b/src/my_plugin_android/test/my_plugin_android_test.dart new file mode 100644 index 0000000..7e6167e --- /dev/null +++ b/src/my_plugin_android/test/my_plugin_android_test.dart @@ -0,0 +1,44 @@ +import 'package:flutter/services.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:my_plugin_android/my_plugin_android.dart'; +import 'package:my_plugin_platform_interface/my_plugin_platform_interface.dart'; + +void main() { + TestWidgetsFlutterBinding.ensureInitialized(); + + group('MyPluginAndroid', () { + const kPlatformName = 'Android'; + late MyPluginAndroid myPlugin; + late List log; + + setUp(() async { + myPlugin = MyPluginAndroid(); + + log = []; + TestDefaultBinaryMessengerBinding.instance!.defaultBinaryMessenger + .setMockMethodCallHandler(myPlugin.methodChannel, (methodCall) async { + log.add(methodCall); + switch (methodCall.method) { + case 'getPlatformName': + return kPlatformName; + default: + return null; + } + }); + }); + + test('can be registered', () { + MyPluginAndroid.registerWith(); + expect(MyPluginPlatform.instance, isA()); + }); + + test('getPlatformName returns correct name', () async { + final name = await myPlugin.getPlatformName(); + expect( + log, + [isMethodCall('getPlatformName', arguments: null)], + ); + expect(name, equals(kPlatformName)); + }); + }); +}