From 61bc4cce8209b698da182504cc0a293142afac43 Mon Sep 17 00:00:00 2001 From: rxlabz Date: Fri, 3 Apr 2020 23:03:18 +0200 Subject: [PATCH] Android : new FlutterPlugin API & AndroidX migration --- README.md | 4 +- audioplayer/CHANGELOG.md | 3 + audioplayer/android/.gitignore | 4 -- audioplayer/android/build.gradle | 17 +++-- audioplayer/android/gradle.properties | 3 + .../gradle/wrapper/gradle-wrapper.properties | 5 ++ .../android/src/main/AndroidManifest.xml | 4 +- .../rxla/audioplayer/AudioplayerPlugin.java | 72 +++++++++++-------- example/android/app/build.gradle | 13 +--- .../android/app/src/debug/AndroidManifest.xml | 2 +- .../android/app/src/main/AndroidManifest.xml | 38 ++++++---- .../audioplayer_example/MainActivity.java | 6 ++ .../rxla/audioplayerexample/MainActivity.java | 13 ---- .../kotlin/bz/rxla/example/MainActivity.kt | 6 -- .../app/src/main/res/values/styles.xml | 10 +++ .../app/src/profile/AndroidManifest.xml | 2 +- example/android/build.gradle | 2 +- example/android/gradle.properties | 3 + .../gradle/wrapper/gradle-wrapper.properties | 2 +- 19 files changed, 117 insertions(+), 92 deletions(-) create mode 100644 audioplayer/android/gradle/wrapper/gradle-wrapper.properties create mode 100644 example/android/app/src/main/java/bz/rxla/audioplayer_example/MainActivity.java delete mode 100644 example/android/app/src/main/java/bz/rxla/audioplayerexample/MainActivity.java delete mode 100644 example/android/app/src/main/kotlin/bz/rxla/example/MainActivity.kt diff --git a/README.md b/README.md index 0b78ee3..8964d30 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # AudioPlayer -A Flutter audio plugin (Swift/Java) to play remote or local audio files on iOS / Android / MacOS / Web. +A Flutter audio plugin (Swift/Java) to play remote or local audio files on iOS / Android / MacOS and Web. [Online demo](https://rxlabz.github.io/audioplayer/) @@ -30,7 +30,7 @@ To use this plugin : dependencies: flutter: sdk: flutter - audioplayer: 0.7.0 + audioplayer: 0.8.0 audioplayer_web: 0.7.0 ``` diff --git a/audioplayer/CHANGELOG.md b/audioplayer/CHANGELOG.md index 06df4f9..08b2af1 100644 --- a/audioplayer/CHANGELOG.md +++ b/audioplayer/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +## 0.8.0 +- Android : new FlutterPlugin API & AndroidX migration + ## 0.7.0 - add Web support diff --git a/audioplayer/android/.gitignore b/audioplayer/android/.gitignore index 5c4ef82..c6cbe56 100644 --- a/audioplayer/android/.gitignore +++ b/audioplayer/android/.gitignore @@ -6,7 +6,3 @@ .DS_Store /build /captures - -/gradle -/gradlew -/gradlew.bat diff --git a/audioplayer/android/build.gradle b/audioplayer/android/build.gradle index 2595bdc..173306b 100644 --- a/audioplayer/android/build.gradle +++ b/audioplayer/android/build.gradle @@ -1,18 +1,20 @@ group 'bz.rxla.audioplayer' -version '1.0-SNAPSHOT' +version '1.0' buildscript { repositories { + google() jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:2.3.0' + classpath 'com.android.tools.build:gradle:3.5.0' } } -allprojects { +rootProject.allprojects { repositories { + google() jcenter() } } @@ -20,15 +22,16 @@ allprojects { apply plugin: 'com.android.library' android { - compileSdkVersion 25 - buildToolsVersion '25.0.3' + compileSdkVersion 28 defaultConfig { minSdkVersion 16 - targetSdkVersion 25 - testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } lintOptions { disable 'InvalidPackage' } } + +dependencies { + implementation 'com.android.support:support-annotations:28.0.0' +} diff --git a/audioplayer/android/gradle.properties b/audioplayer/android/gradle.properties index 8bd86f6..38c8d45 100644 --- a/audioplayer/android/gradle.properties +++ b/audioplayer/android/gradle.properties @@ -1 +1,4 @@ org.gradle.jvmargs=-Xmx1536M +android.enableR8=true +android.useAndroidX=true +android.enableJetifier=true diff --git a/audioplayer/android/gradle/wrapper/gradle-wrapper.properties b/audioplayer/android/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..01a286e --- /dev/null +++ b/audioplayer/android/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,5 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-all.zip diff --git a/audioplayer/android/src/main/AndroidManifest.xml b/audioplayer/android/src/main/AndroidManifest.xml index 039c85a..8fcadb2 100644 --- a/audioplayer/android/src/main/AndroidManifest.xml +++ b/audioplayer/android/src/main/AndroidManifest.xml @@ -1,5 +1,3 @@ + package="bz.rxla.audioplayer"> diff --git a/audioplayer/android/src/main/java/bz/rxla/audioplayer/AudioplayerPlugin.java b/audioplayer/android/src/main/java/bz/rxla/audioplayer/AudioplayerPlugin.java index 5f47e51..dfde358 100644 --- a/audioplayer/android/src/main/java/bz/rxla/audioplayer/AudioplayerPlugin.java +++ b/audioplayer/android/src/main/java/bz/rxla/audioplayer/AudioplayerPlugin.java @@ -1,71 +1,82 @@ package bz.rxla.audioplayer; +import android.content.Context; import android.media.AudioManager; import android.media.MediaPlayer; +import android.os.Build; import android.os.Handler; import android.util.Log; + +import androidx.annotation.NonNull; + +import java.io.IOException; + +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; -import io.flutter.plugin.common.MethodCall; import io.flutter.plugin.common.PluginRegistry.Registrar; -import java.io.IOException; -import java.util.HashMap; - -import android.content.Context; -import android.os.Build; - -/** - * Android implementation for AudioPlayerPlugin. - */ -public class AudioplayerPlugin implements MethodCallHandler { +/** AudioplayerPlugin */ +public class AudioplayerPlugin implements FlutterPlugin, MethodCallHandler { private static final String ID = "bz.rxla.flutter/audio"; - private final MethodChannel channel; - private final AudioManager am; + private MethodChannel channel; + private AudioManager am; private final Handler handler = new Handler(); private MediaPlayer mediaPlayer; - public static void registerWith(Registrar registrar) { - final MethodChannel channel = new MethodChannel(registrar.messenger(), ID); - channel.setMethodCallHandler(new AudioplayerPlugin(registrar, channel)); - } - - private AudioplayerPlugin(Registrar registrar, MethodChannel channel) { - this.channel = channel; + @Override + public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) { + channel = new MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), ID); channel.setMethodCallHandler(this); - Context context = registrar.context().getApplicationContext(); + + Context context = flutterPluginBinding.getApplicationContext(); this.am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); } + // This static function is optional and equivalent to onAttachedToEngine. It supports the old + // pre-Flutter-1.12 Android projects. You are encouraged to continue supporting + // plugin registration via this function while apps migrate to use the new Android APIs + // post-flutter-1.12 via https://flutter.dev/go/android-project-migration. + // + // It is encouraged to share logic between onAttachedToEngine and registerWith to keep + // them functionally equivalent. Only one of onAttachedToEngine or registerWith will be called + // depending on the user's project. onAttachedToEngine or registerWith must both be defined + // in the same class. + public static void registerWith(Registrar registrar) { + final MethodChannel channel = new MethodChannel(registrar.messenger(), "audioplayer"); + channel.setMethodCallHandler(new AudioplayerPlugin()); + } + @Override - public void onMethodCall(MethodCall call, MethodChannel.Result response) { + public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) { switch (call.method) { case "play": play(call.argument("url").toString()); - response.success(null); + result.success(null); break; case "pause": pause(); - response.success(null); + result.success(null); break; case "stop": stop(); - response.success(null); + result.success(null); break; case "seek": double position = call.arguments(); seek(position); - response.success(null); + result.success(null); break; case "mute": Boolean muted = call.arguments(); mute(muted); - response.success(null); + result.success(null); break; default: - response.notImplemented(); + result.notImplemented(); } } @@ -144,6 +155,11 @@ public boolean onError(MediaPlayer mp, int what, int extra) { handler.post(sendData); } + @Override + public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) { + channel.setMethodCallHandler(null); + } + private final Runnable sendData = new Runnable(){ public void run(){ try { diff --git a/example/android/app/build.gradle b/example/android/app/build.gradle index 15c153b..4d5db69 100644 --- a/example/android/app/build.gradle +++ b/example/android/app/build.gradle @@ -25,7 +25,7 @@ apply plugin: 'com.android.application' apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" android { - compileSdkVersion 27 + compileSdkVersion 28 lintOptions { disable 'InvalidPackage' @@ -33,12 +33,11 @@ android { defaultConfig { // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). - applicationId "bz.rxla.audioplayerexample" + applicationId "bz.rxla.audioplayer_example" minSdkVersion 16 - targetSdkVersion 27 + targetSdkVersion 28 versionCode flutterVersionCode.toInteger() versionName flutterVersionName - testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { @@ -53,9 +52,3 @@ android { flutter { source '../..' } - -dependencies { - testImplementation 'junit:junit:4.12' - androidTestImplementation 'com.android.support.test:runner:1.0.2' - androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' -} diff --git a/example/android/app/src/debug/AndroidManifest.xml b/example/android/app/src/debug/AndroidManifest.xml index 1c3e314..63dd508 100644 --- a/example/android/app/src/debug/AndroidManifest.xml +++ b/example/android/app/src/debug/AndroidManifest.xml @@ -1,5 +1,5 @@ + package="bz.rxla.audioplayer_example"> diff --git a/example/android/app/src/main/AndroidManifest.xml b/example/android/app/src/main/AndroidManifest.xml index 8d127cb..17c00d6 100644 --- a/example/android/app/src/main/AndroidManifest.xml +++ b/example/android/app/src/main/AndroidManifest.xml @@ -1,12 +1,5 @@ - - - - + package="bz.rxla.audioplayer_example"> + + android:name="io.flutter.embedding.android.NormalTheme" + android:resource="@style/NormalTheme" + /> + + + + diff --git a/example/android/app/src/main/java/bz/rxla/audioplayer_example/MainActivity.java b/example/android/app/src/main/java/bz/rxla/audioplayer_example/MainActivity.java new file mode 100644 index 0000000..02797e5 --- /dev/null +++ b/example/android/app/src/main/java/bz/rxla/audioplayer_example/MainActivity.java @@ -0,0 +1,6 @@ +package bz.rxla.audioplayer_example; + +import io.flutter.embedding.android.FlutterActivity; + +public class MainActivity extends FlutterActivity { +} diff --git a/example/android/app/src/main/java/bz/rxla/audioplayerexample/MainActivity.java b/example/android/app/src/main/java/bz/rxla/audioplayerexample/MainActivity.java deleted file mode 100644 index e4e5180..0000000 --- a/example/android/app/src/main/java/bz/rxla/audioplayerexample/MainActivity.java +++ /dev/null @@ -1,13 +0,0 @@ -package bz.rxla.audioplayerexample; - -import android.os.Bundle; -import io.flutter.app.FlutterActivity; -import io.flutter.plugins.GeneratedPluginRegistrant; - -public class MainActivity extends FlutterActivity { - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - GeneratedPluginRegistrant.registerWith(this); - } -} diff --git a/example/android/app/src/main/kotlin/bz/rxla/example/MainActivity.kt b/example/android/app/src/main/kotlin/bz/rxla/example/MainActivity.kt deleted file mode 100644 index 2e15e14..0000000 --- a/example/android/app/src/main/kotlin/bz/rxla/example/MainActivity.kt +++ /dev/null @@ -1,6 +0,0 @@ -package bz.rxla.example - -import io.flutter.embedding.android.FlutterActivity - -class MainActivity: FlutterActivity() { -} diff --git a/example/android/app/src/main/res/values/styles.xml b/example/android/app/src/main/res/values/styles.xml index 00fa441..1f83a33 100644 --- a/example/android/app/src/main/res/values/styles.xml +++ b/example/android/app/src/main/res/values/styles.xml @@ -1,8 +1,18 @@ + + + diff --git a/example/android/app/src/profile/AndroidManifest.xml b/example/android/app/src/profile/AndroidManifest.xml index 1c3e314..63dd508 100644 --- a/example/android/app/src/profile/AndroidManifest.xml +++ b/example/android/app/src/profile/AndroidManifest.xml @@ -1,5 +1,5 @@ + package="bz.rxla.audioplayer_example"> diff --git a/example/android/build.gradle b/example/android/build.gradle index bb8a303..e0d7ae2 100644 --- a/example/android/build.gradle +++ b/example/android/build.gradle @@ -5,7 +5,7 @@ buildscript { } dependencies { - classpath 'com.android.tools.build:gradle:3.2.1' + classpath 'com.android.tools.build:gradle:3.5.0' } } diff --git a/example/android/gradle.properties b/example/android/gradle.properties index 8bd86f6..38c8d45 100644 --- a/example/android/gradle.properties +++ b/example/android/gradle.properties @@ -1 +1,4 @@ org.gradle.jvmargs=-Xmx1536M +android.enableR8=true +android.useAndroidX=true +android.enableJetifier=true diff --git a/example/android/gradle/wrapper/gradle-wrapper.properties b/example/android/gradle/wrapper/gradle-wrapper.properties index 2819f02..296b146 100644 --- a/example/android/gradle/wrapper/gradle-wrapper.properties +++ b/example/android/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-all.zip