Linking dependencies between PCL and Xamarin app - android

While working on my Xamarin cross-platform app I am using native apps and a PCL for shared code.
Unfortunately it does not seem possible for me to link the dependencies properly in my PCL.
For example: I use a bouncycastle DLL / dependency in my PCL. When I reference the PCL to my android / iOS app it requires me to link the same dependency / dll again, just inside the native project.
So now I have two dependencies in my native app which seems redundant:
NativeApp:
PCL
BouncyCastle
Is it possible that I only need the BC reference in the PCL?
Thanks.

PCLs use .NET portable (a subset of the whole .NET library) while standard libraries use the full .NET library Becaue of that PCLs cannot reference standard .NET Libraries because they use two different sets of .NET.
On top of that PCLs define the target platforms it is going to support. You can see this in the properties > Library > Targeting of your PCL.
PCLs can only reference libraries that are also PCLs that contain a collection of targets that contain ALL of the targets the PCL has itself.
If you want to make another library for your PCL to reference, make it a PCL itself and make sure it has all the same targets than the PCL that is going to reference it.

Related

How do I include required aar and jar files in a Xamarin Android app? What is the AndroidLibrary build action for?

We use Xamarin Forms and I have been tasked with integrating a 3rd party AAR library from a business partner and I don't have control over the library or its dependencies. This library itself is distributed using Maven, which works great in Android Studio but is a pain in Xamarin, and it has many dependencies on both libraries that are standard in Android as well as other proprietary libraries.
Since I only need to interact with a small portion of the public API of the main library, I've created an Android library (AAR) wrapper project in Android Studio that only exposes the functionality I need and does not use any types that do not already have bindings.
I have created an Android binding project against the AAR wrapper library, and it compiles in Visual Studio without any warnings or errors.
I've created bindings for other libraries in the past and have the Xamarin binding documentation multiple times and searched online, but the part I'm missing is how to include the required/reference JAR/AAR files in the compilation process and the final Android application. Most of the standard libraries that I need already have NuGet packages (Androidx, Google Play Services, etc)., but the binding library compiles without complaint - so how do I include the other required libraries?
Do I really have to create a binding project for each required AAR/JAR and add as a reference? I don't need to interact with the types or resources in these libraries directly from Xamarin since I only interact with the the types/methods exposed in the wrapper AAR (e.g. I don't think I really need a Xamarin binding). Is there a way to simply have Xamarin process the required AARs and JARs without creating a Xamarin binding project for every library that doesn't already have a NuGet package? There are many many dependencies which makes this theoretically possible, but not in practice. There must be an easier way...
I noticed there is an AndroidLibrary build action that the documentation says can be used to directly reference an AAR/JAR file in a Android application, but I can find no examples of how to use this in practice. What does this build action do? How is it supposed to be used? https://learn.microsoft.com/en-us/xamarin/android/deploy-test/building-apps/build-items#androidaarlibrary
Thanks in advance for any help or direction on the best way to do this.

Integration of Android Native module with existing Xamarin Native Project

We have teams which are working on different different technologies. Currently struggling with integration of android native module (Developed in android native language) with existing Xamarin android project.
Actually I got one way that we can convert android native project as a library. This library can integrate to Xamarin project.
I had gone through below links
Convert existing project to library project in Android Studio
https://developer.xamarin.com/guides/android/advanced_topics/using_native_libraries/ ( Integration of android library in xamarin project)
Is this the correct way or do we have any alternate way to achieve it?
Is this the correct way or do we have any alternate way to achieve it?
The document Using Native Libraries is talking about how to use .so file (native libraries) in Xamarin.Android, here is the example.
It is recommended that using Binding Java Library. You could convert your android native project to a .jar or .arr file, then you could refer to Binding a .JAR or Binding an .AAR.
If there is any problem when you trying binding Java Library, you could refer to Troubleshooting Bindings.

Android Studio - Add shared library (.so) file and header (.h) file to project

I am currently working on an android project that requires me to make use of functions included in a shared library (.so). I also only have header (.h) files for the library provided to me.
Is it possible to work with just these two files? Or do I need to create my own implemenations via c++ codes?
I am using Android Studio intend to use CMake.
Regards,
Philip
Most Android apps are written in Java. Google has released the Native Developer Kit (NDK) in order to allow developers to write libraries in C++. However, these libraries are usually very low level and called from the Java code which defines the UI and higher-level app logic. Most likely you will need to write a wrapper for the library so that you can call it from Java code. Looks like this blog is a good place to start.

Adding a Xamarin Component to Shared Code

I am developing a Xamarin project for both Android and iOS. I would like to use a Xamarin Component (from the store) in my shared code base. Currently, I have created a "Shared Project", but it seems like it is not possible to add the component to this project. Is this correctly understood? And if so, is there some kind of workaround?
Thanks.
Add the component to all your platform specific projects and then you can use it on the shared project. If there are any discrepancies in the API's between the platforms you will have to resolve these with compiler flags or partial classes/methods.
http://developer.xamarin.com/guides/cross-platform/application_fundamentals/shared_projects/
A Shared Project does not get compiled on its own, it exists purely as
a grouping of source code files that can be included in other
projects. When referenced by another project, the code is effectively
compiled as part of that project. Shared Projects cannot reference any
other project type (including other Shared Projects).
Components are platform specific. To use platform specific code in a shared or PCL project you can use DI to pass a reference to a common interface into your shared code.
http://developer.xamarin.com/guides/cross-platform/xamarin-forms/dependency-service/

Does Android Library project support native libraries?

I have read the overview for Android Library project. I have in mind to develop an Android project library containing native libraries and JNI wrapper which would be included into the Android project. However, the doc does not explicitly mention if Android library project can include native libraries.
Can anyone confirm/infirm support for native libraries in Android library projects ?
As Android Document said:
An Android library project is a development project that holds shared
Android source code and resources.
An Android Library Project, in fact, isn't so different from normal Android project. You can make a normal android project as library project, except asset folder. They're just different when you declare in Eclipse Buid Path or something similar in other IDEs.
Native support simply, just a call to native layer (as in your post, Native Library) such as C/C++ library file (*.so file). This function is not specific to Android, but in normal Java project also support this feature: Calling functions from another language inside Java.
So, in short answer, yes :)

Categories

Resources