Is it possible to integrate dependencies from Gradle inside Xamarin project?
A have some libraries, which I need to install, they look like this
dependencies {
compile (name: '<aar fileName>', ext:'aar')
compile 'com.koushikdutta.async:androidasync:2.1.6'
compile 'com.android.support:appcompat-v7:23.1.0'
compile 'com.android.support:design:23.1.0' compile
}
Can I expose them to Xamarin somehow?
There is no gradle in Xamarin.Android.
To get external libraries or Android libraries you could use:
NuGet
Xamarin Components
Do your own Java Library binding
Convert code directly from Java to C#.
There is a plugin for Visual Studio (tested it a few months ago but didn't worked for me) that in theory grabs dependencies from gradle and tries to convert them to C# Bindings.
Here you have a very good github page with awesome Xamarin Plugins.
Build your Android applications in Visual Studio using Gradle.
With the latest Visual Studio release, if you take a look at the cross-platform C++ section in the File->New project template section, you will see a variety of templates as shown in the figure below. Notice two new Gradle templates that have been introduced.
Source: https://blogs.msdn.microsoft.com/vcblog/2016/06/12/build-your-android-applications-in-visual-studio-using-gradle/
You can try the Java library binding, it's basically creating a C# binding Java library to consume your library from your Xamarin project.
Typically you won't use gradle to build your Xamarin projects. Thus you need to integrate/ reference those libraries through NuGet packages. AppCompat and Support Design are both available as NuGet packages. The otherone, if it does what I think it does, won't be needed because c# as build in async-support.
Related
Suppose I am building two class libraries in a single solution in visual stuido. The projects are called "Core" and "Extensions".
Extensions depends upon Core.
I would like to publish these as separate nuget packages for other developers to consume, because while the Core package would always be used, the extensions package just provides some optional functionality.
When I am developing the solution in visual studio, however, it is much simpler for me to set a project dependency from Extension to Core. Otherwise (in a naive approach), building the whole solution would require the following steps:
In visual studio, commit and push Core.
Wait for the CI server to build Core and publish the nuget package to our internal feed.
In visual studio, manage nuget packages for the Extensions project.
Connect to the internal nuget feed
Get the updated Core nuget package.
This approach would work, but is obviously very tedious.
The main difficulty I have been encountering while researching how to do this is that the way all of these things interact has been changing quite rapidly over the past few years, with sdk vs non sdk style projects, nuget pack vs dotnet pack vs msbuild pack, and so on and so forth.
Perhaps the most relevant, most recent answer appears in this question How do I create an individual nuget package for each project in a solution, with project references converted to nuget dependencies? however the answer is now quite old, and is for a dot net framework style project (using packages.config), not a .net5 project.
So, can anyone provide up to date advice on how to correctly accomplish the following:
In a visual studio solution with multiple .net5 class library projects, have dependencies set up as project dependencies at dev-time, but convert each project to its own nuget package with the equivalent package dependencies at publish time.
I read this article a while ago:
https://blog.xamarin.com/net-standard-library-support-for-xamarin/
So, I converted all of our libraries from portable libraries to .NET Standard (1.4). I did this because the article says "This PCL now needs to be upgraded to target the .NET Standard Library, which can be found in the projects properties.".
However, I can't figure out how to build a Xamarin Forms project that targets .NET Standard. I cloned the Xamarin Forms samples, and opened up the MasterDetailPage project. I went in to the project properties and switched from portable to .NET Standard as per the instructions. Immediately, I get an error telling me that I need to opt in to NuGet 3.0 support. I'm fine with this, but how do I do it?
I found that if I remove the Xamarin Forms NuGet package, I am able to switch over to .NET Standard. However, once I have done this, I can't add the Xamarin Forms NuGet package back. It just keeps failing. Contrary to what the article says, I cannot add references to .NET Standard libraries. When I try to add a reference to existing .NET Standard libraries in my Solution, Visual Studio just gives me an error saying that the library is not compatible. Which version of .NET Standard should I be targeting for Xamarin Forms?
How do I get a .NET Standard library compiling with Xamarin Forms support?
If you wanna do it with the new Visual Studio 2017 release with csproj instead of project.json you can either use dotnet migrate CLI command or add/edit this code to your csproj file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<PackageTargetFallback>$(PackageTargetFallback);portable-win+net45+wp8+win81+wpa8</PackageTargetFallback>
</PropertyGroup>
</Project>
It turns out that you can get a .NET Standard project to reference the Xamarin Forms NuGet package, and therefore be able to leverage .NET Standard libraries in Xamarin Forms.
This article more or less explains how to do it. The trick is to import the portable framework in the project.json.
https://xamarinhelp.com/dot-net-standard-pcl-xamarin-forms/
If you can't get it working, here is a Git sample that does work.
https://github.com/adamped/XamarinForms.NetStandard.git
I was eventually able to target .NET Standard 1.4 without issues.
I created a Xamarin project MyApp within VS2015. This ends up creating a bunch of projects including MyApp.Droid for Android. I can build and deploy this project just fine on my Android device.
Next, I created a shared C++ Dynamic Shared Library (Android) project MyShared. When I try to add this project as a reference in MyApp.Droid, it reports an error that only .dll or .exe file types can be added as a reference and not .so types.
Wondering if there is a different way to add a native library project as a dependency. Regards.
Xamarin.Android supports the use of native libraries via the standard PInvoke mechanism.
You will need to define the DllImport C# attributes/signatures, there are various tools such as SWIG, PInvoker Visual Studio Addin, Mono's CppSharp, etc.. that can assist in this step.
(Personally I use Mono's CppSharp, but it might be overkill depending upon the number of classes/methods/functions there are within your C++ library.)
Mono Interop with Native Libraries
Xamarin.Android : Using Native Libraries
To interop with native libraries in your Xamarin.Android project, all you need to do is create a DllImport function declaration for the existing code to invoke, and the runtime will handle the rest. Set the EntryPoint to specify the exact function to be called in the native code.
Build and Debug C++ Libraries in Xamarin.Android Apps with Visual Studio 2015
I want to debug code written in C. Currently, Android Studio 1.2.2 does not seem to support JNI debugging.
Does Android Studio 1.3 RC 1 version support step over, step into as Java debugger?
Android Studio supports JNI debugging first time with 1.3 RC 1. See here
To get started using this feature, go to add native code.
Check this repository for Android NDK samples using the new Android Studio 1.3 preview C++ integration.
It is supported, but with limitations.
Limitations
First, the Android NDK support only works with the new experimental Gradle plugin for Android (which in turn requires Gradle 2.5).
While the new gradle plugin delivers some major performance improvements (and Android NDK build support), note that it also requires changes to the build file “DSL” (the way your build is described in your build.gradle files.) Not only will you need to modify your build.gradle files (a process we hope to automate before the plugin graduates from experimental to stable), but we anticipate making some additional incompatible changes along the way.
Second, note also that Android Studio has not yet been updated to fully handle the experimental plugin. This means that for example the Project Structure Dialog, and various quick fixes which automatically update the build data, do not work correctly. You’ll need to edit your build.gradle files manually to configure your projects. As another example, the various templates which update the build files (such as New Module), have not yet been updated.
Finally, there are still some known bugs in this build; when debugging with LLDB breakpoints do not always work on Windows; if you run into this, you can switch to GDB debugging as a temporary workaround.
Source: http://tools.android.com/tech-docs/android-ndk-preview
If your native code is wrapped as a library, you need to add this part to your app's gradle:
model {
android.sources {
main {
jniLibs {
dependencies {
project ":mynativelib" buildType "debug"
}
}
}
}
I'm trying to make a very simple Xamarin.Forms test application to explore some features of the System.Data.SQLite package (namely encryption). However, I can't seem to get Visual Studio 2012 to add a reference to the package to the project.
Here are the steps I am taking:
Created a new Xamarin.Forms Portable blank app.
Right-click on TestApp (core project) and select Manage NuGet Packages
Search for "sqlite"
Install the "System.Data.SQLite (x86/x64)" package. (This package is version 1.0.97.0 and it is described as "the official SQLite database engine for both x86 and x64 along with the ADO.NET provider.")
According to NuGet, the package was successfully installed.
However, I cannot see the reference added under my project's "References" folder, and I also cannot write "using System.Data.SQLite" without errors ("cannot resolve symbol 'Data'"). Does anyone have any idea why this is happening?
On a side note, I can follow the exact same process described above except instead of creating a Xamarin.Forms Portable blank app, I choose a regular C# console app. This allows me to install the NuGet package fine and it shows up in references as expected.
Posting an update to share what I've learned.
So as Jason sort of alluded to in his answer, the reason I was not able to reference the package in my Xamarin.Forms project is because Xamarin.Forms is, by definition, a PCL project. It's meant to work on all platforms. The package I was referring to in my question is not a PCL, which means that anything in the package that is platform dependent is inaccessable (which is basically the entire package). The SQLite.Net-PCL package is basically the same thing as System.Data.SQLite, but in PCL form, which means it is compatible for use in a Xamarin.Forms project.
Following this logic, it makes sense that I was able to access System.Data.SQLite from a console project, since a console project is not a PCL project and it targets a specific platform.
For PCL projects, use SQLite.Net-PCL.