How do you register an Android Service in a Flutter Plugin

547

Solution 1

I was unable to determine how to include a service definition in a Flutter plugin that would be added to the plugin consuming app.

What I had to do was add the service definition in the AndroidManifest.xml file of the plugin consuming app.

Solution 2

Haven't tried this with a service, but creating a skeleton manifest in the plugin's folders does seem to merge the skeleton into the app's manifest at build time.

For example, with the following skeleton, the application meta-data definition is merged fine.:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.plugin.restrictions_manager">
    <application>
        <meta-data android:name="android.content.APP_RESTRICTIONS"
                android:resource="@xml/app_restrictions" />
    </application>
</manifest>

[Edit] This approach does indeed work with a service declaration. And to see the merged AndroidManifest.xml, look in my_plugin/build/app/outputs/apk/debug/app-debug.apk, which can be browsed in Android Studio.

Share:
547
dazza5000
Author by

dazza5000

Freelance Android Engineer. Co-organizer of the Austin Android Developer Meetup! Name is Darran Kelinske. love.

Updated on December 20, 2022

Comments

  • dazza5000
    dazza5000 over 1 year

    I am creating a card reader integration that uses and Android Service for part of the integration. The standard Android implementation includes an android service definition in the AndroidManifest.xml

        <service
            android:name="com.anywherecommerce.android.sdk.services.CardReaderConnectionService"
            android:stopWithTask="false" />
    

    https://github.com/dazza5000/any-pay-android-sample/blob/4f7ca7af79f2494e3ec408b025446be6020c0aa4/src/main/AndroidManifest.xml#L43-L45

    How do I define that in a Flutter Plugin so that the service definition will be added to the AndroidManifest of the app that uses it? Do I just include it as part of the library and leave it up to the user to register the service in their app AndroidManifest.xml?