Hey Guys!
Recently, I have received a few messages from the community asking me mostly 2 questions: “How were you able to create 2 Xamarin Plugins in the last few months” and “What was your experience while creating them, what tools did you use?”, so this week’s post is inspired by those two questions and I will do my best to answer them fully, hoping that by the end of this post you can make your own plugin too and contribute something new for all of us in the community! 😊
But wait. . . What is a Plugin?
Before we get started with the code, lets figure out what is a plugin and why we actually need and use them on Xamarin (Hahaha yes, yes, I know most of us
A Xamarin Plugin is basically a common use case (Feature) that we used to implement natively on our projects. So for example in my personal experience I noticed that each time I wanted to develop an App and allow my users to authenticate with their social media accounts (Facebook, Google, and LinkedIn), I was able to give them this feature on my Xamarin Apps but each time I wanted to do so, I had to use the native binding libraries on each platform (iOS, Android) to enable these features. Shortly after doing it on about 2 or 3 app’s I noticed that I was literally implementing the same code over and over on each of my apps to enable essentially the same set of features.
So it clicked really quick on my head, that there must be a better way to abstract my code and save time for me, my teammates and anyone on the community that had the same use case on the future.
And that’s when I started doing some research and Plugins came into the picture and I learned that Xamarin
Plugins are an abstract cross-platform interface, that is implemented in various mobile platforms and each of these can be accessed by Xamarin Forms by Dependecy Service. [More Info]
Aha!!! Exactly what we need, but how can we make them??
Well, Let’s Get Started!
So once we have identified our use case (Feature), the next problem we face is how can we actually make our Plugin
Prerequisites:
-
- Cross-Platform .NET Standard Plugin Templates for Xamarin – By James Montemagno
This is a great Plugin Template and project structure created by James, that allows us to create a single library using platform multi-targeting hence allowing us to easily target Android, iOS and any other platform we wish to support on our Plugin. But still keeping our project structure clean and simplified.
Project Structure:
As you can see the project structure for our plugin is pretty simple we have the following folders:
Platforms:
Inside this folder, is where you will have each platform you wish to support, and inside each of these subfolders, you will add the native implementations for your feature. (We will go more in detail, later on, the post)
Shared:
The shared folder is where we are going to put all of our abstractions and reusable code between our implementations, basically our Interfaces, Exceptions, Models, and any code you wish to share that is not platform dependent. (We will go more in detail on these files, later on, the post).
Lastly but not least we should check out the best goodie this template offers us and it’s the .csproj file and how it auto generates our Nuget Package on build.
Once you open this file, you will see something similar to our GoogleClient.csproj file which contains the base configuration for our Nuget, and plugin:
- Base Configurations for our Nuget Specs File
- Platforms supported by our plugin
- Dependencies for each platform
If you wish to learn more about the structure of this project James Montemagno also has a great video to walk through it.
- Nuget Package Explorer – By Oren Novotny, LLC
This is basically the tool we use to publish our plugin once, we have built our project and generated our Nuget file for publishing on release mode.
Now, we are ready to go!🎊
Once we have our user case (Feature), project template and Nuget Package Explorer. Creating our plugin is as simple as copying our native code of our use case from our Xamarin App and adding it to our new Plugin Project, and publishing it on the Nuget Gallery to use on our Xamarin Forms project directly!
I will show you how to do it, using my Google Plugin code as an example, remember you can always go to the repo of the project and any of the open source plugins published by the community look at the code directly and learn more about how they implemented it.
STEP 1: Adding our Plugin Interface in our Shared Folder
STEP 2: Implementing our Interface on each platform
Android
Full code of implementation can be found here
iOS
Full code of implementation can be found here
STEP 3: Modify our csproj and prepare for publishing
Before publishing our Nuget we need to be sure and double check that there is not an existing project on the same feature you’re trying to develop, if there is join in and help by collaborating to the existing project, if it’s not the case we also we need to make sure our project meets the minimum requirements to be supported as a Plugin:
- Open source on GitHub
- Documentation on GitHub’s README file
- Name: “FEATURE_NAME Plugin for Xamarin”
- Namespace: Plugin.FEATURE_NAME
- App-store friendly OSS license (we like MIT)
- No dependency on Xamarin.Forms in our platform projects
- Have a list of supported and unsupported OSs in its GitHub wiki
[List of Popular Existing Xamarin Plugins]
Adding Our Project Info to our csproj properties
BUILD PROJECT⚙️
Once our Project has build successfully we can now find our Nuget ready to publish on our Project Folder > Bin > Release Folder.
Now we just need to open our Nuget File, Verify all the data is correct, and go to File > Publish, add our Nuget Gallery Publishing Key and Publish our new Plugin!
CE FINITO! 🥳
And that’s it, Guys now you have successfully published your new plugin to the community so that everyone can enjoy your collaboration and use the implementation you shared on their projects, once Nuget Gallery has published your Package successfully it should look like this. (Nuget Gallery takes some time to index your package into their repo, around 10 – 15 mins average after the Indexing notification is removed from your page, you will be able to download your plugin directly from the visual studio Nugget Package Manager).
Troubleshooting 🛠
You might run into some issues trying to build your plugin project, this is probably caused because you have not connected to your Mac client on Visual Studio (Remember that to compile iOS projects we need a Mac client, and that’s why our project can’t compile completely).
You can learn how to connect to your Mac Client here.
Full Source Code for the Google Plugin for Xamarin can be found on the GitHub Repo for the project.
Made with ❤ by Pujols.
Good References to learn How to make your own Plugins on Xamarin
- Join James Montemagno at his guest lecture: Using and Developing Plugins for Xamarin at Xamarin University
- Consuming and Creating Xamarin.Forms Plugins – Microsoft Docs
- Creating Reusable Plugins for Xamarin and Xamarin Forms – James Montemagno
Credits for artwork components:
Mobile Icons for the banner of the post made by Freepik
Thank!
It’s my pleasure! 😀