How mono runtime is embbedded into Android application - xamarin

As far as I understand, applications built with Xamarin deploy in every application a mono runtime that works beside the ART, communicating between each other.
Knowing the current android limitations that starting API-28 you cannot start binaries, I was wondering how actually the runtime gets started?

This is briefly described in the Xamarin docs here: https://learn.microsoft.com/en-us/xamarin/android/internals/architecture#application-startup
If you take a look at the build output of your app in obj/Debug/android/src/mono you will notice a couple of java files.
MonoRuntimeProvider.java
This creates a ContentProvider, which is responsible for launching the mono runtime and starting your App.
MonoPackageManager_Resources.java
This class is called by the MonoRuntimeProvider, which is a helper class to find the Assembly containing your App.
The MonoRuntimeProvider is registered in the AndroidManifest.xml in the Application tag. Which means that when your App starts, the Content provider will also start, and in turn starts the Mono Runtime.
At build time Android Callable Wrappers (ACW) are generated for your Activities (and many other types as well). This is a way to invoke managed (C# code) from the Android world.
If you try to decompile your App, you will see that in the constructor of such ACW's there is a call to the mono runtime register and activate the managed type.
As for the limitations in API-28, I don't see anywhere in the docs where they mention anything about not being able to start binaries.
There are some Restrictions on non-SDK interfaces. If it was the case that you were not able to consume binaries in your Android App it would mean that all Apps consuming C/C++ NDK libraries would be broken.
There has been some limitations to where you can access such libraries. In the past you would be able to use some binaries outside of your Applications scope, which were bundled with Android, this is not allowed anymore and you need to bundle this with your App yourself.

Related

What the word “shared” really means in Xamarin Shared Mono Runtime?

When you install debug builds on android Xamarin doesn't include Mono runtime in the app instead of this it uses the thing called Shared mono runtime ? I'm curious what the word shared really means? Does is mean that the single instance of the runtime will be lunched and all debug builds(of different apps) will run in this single instance of Mono runtime, or this means that Mono will be delivered to the device only once and all debug builds will run its own instance of that shared runtime(So it will just not be delivered with every app install)
P.S. Although this post looks similar its accepted answer just copy-pastes some ambiguous info from Xamarin docs which is not very informative and doesn't really answers the question

What is your C# code doing within a Xamrin build?

I've read through a lot of documentation on Xamarin and my understanding is that it exposes the native APIs on each platform via C# and .NET, and this way, offers a sort of one-to-one binding. The native build produced contains some of the applicable .NET libraries (based on linker optimisation).
However, I can't seem to find any information about what actually happens within the native build itself.
Assuming my understanding is correct, my question is:
Within the native build, does the code you've written call the native API functions via a call to native .NET functions included in the build?
The short answer is: yes, it calls native API's through .NET code that runs on Mono, via the binding concept you already seem to be familiar with.
Xamarin uses these bindings to call the native code from your C# code. There are a few differences depending on the platform you are running on.
iOS
Your IPA-file will contain 2 things:
The executable of your .NET code (AOT-compiled), which is run through the Mono runtime
Native libraries, which use the objective-C runtime. The .NET code calls these libraries/API's via bindings.
Both the Mono runtime and the Objective-C runtime are running on top of a UNIX-like kernel.
Take a look at this Xamarin iOS architecture documentation for a more extensive explanation.
Android
Android is a little more complex to understand and explain. This is because some of the API's are only exposed through the Android Runtime API.
The bindings in Android are working through concepts such as:
"Android Callable Wrappers" (Android Runtime to Mono)
"Managed Callable Wrappers" (Bindings to Android Runtime API's)
The Xamarin Android architecture documentation explains these concepts and inner workings in great detail.

Custom profiler for UWP apps

I'm trying to rewrite IL code dynamically at runtime. While this is quite well documented for .NET framework applications by using the Profiling API I can't find anything similar for UWP applications.
Because I have a suite of automated function tests, I need to setup the profiling environment and start a sample app programmatically.
Attempt 1
This question explains how to enable a debugger (as far as I understand a profiler is essentially a debugger) and programmatically start a UWP app.
So from my bootstrapper I call IPackageDebugSettings::EnableDebugging followed by IApplicationActivationManager::ActivateApplication.
Anyway I can't figure out how to implement the debugger. The debugger (which is an executable) is called with the following arguments:
-p 3836 -tid 6180
Both numbers change on every run. While p seems to be the process id of the debugged process, I have no idea what tid could be or how I have to use the arguments to control the debugged app (register for module load events, trigger rejits etc.).
Attempt 2
I found this issue on the Core CLR repo which is about enabling the Profiling API for Core CLR apps. The issue contains a link to a sample profiler repo.
So profiling for Core CLR apps seems to basically work the same way as it does in .NET framework apps (with some restrictions). From my bootstrapper I tried to set the appropriate environment variables (i.e. CORECLR_ENABLE_PROFILING, CORECLR_PROFILER and CORECLR_PROFILER_PATH) followed by IApplicationActivationManager::ActivateApplication.
However the profiler doesn't attach.
I'm quite sure that in general it is possible to profile UWP applications because JetBrains' dotTrace can do it.
Any ideas?

xamarin plug and play architecture

I need to create a application which would take the preference of the user when starting the application for the first time and then the appropriate module would be downloaded for the application. Is it possible using xamarin.forms?
Currently we are using xamarin.forms PCL project and creating app for android and iOS using xamarin.forms.
I don't see why not, though you'd likely be tied to Android. Dynamic execution on iOS is not supported, and I'm not sure about Windows. It should be an easy enough test: click a button, download an assembly from somewhere, dynamically load it, then instantiate a class that implements an interface you're looking for. I've been wanting to do a blog post on this very thing, but alas...
Another option which may be iOS compatible is having some code in JavaScript, then download and run it in a WebView. Though that may be more trouble than it's worth.

Does Mac App Store accept application written in Python/Java/Ruby?

Does Mac App Store accept application written in Python/Java/Ruby?
The Good News
Yes, you can submit a Java app to the Mac App Store. Not true in the past, but it is true at least as of now in 2013.
One example is the successful MoneyDance personal finance app currently available for sale in the Mac App Store.
The Bad News
Preparing and submitting your app is neither simple nor easy. You have to jump through some hoops.
Your app cannot depend on Java being installed on the Mac. You must include a JRE (Java Runtime Environment) within your Mac app.
You must write a thin Objective-C wrapper around your Java app. The source code and examples/tutorials are available on the web.
Like any other app, you must go through all hassle of the complicated build and deployment rigmarole with security restrictions Apple requires of any Mac App Store app.
More Info
Two articles, Take your Java application to the Mac App Store and Forking the OpenJDK for fun and profit, by Marco Dinacci. These articles may provide other useful information, but fortunately their discussion of forking the JDK is now outmoded. He and others have provided the needed patches now incorporated in the official releases.
How to Create a Mac OS X Installer for a Java Application (Updated for Mac OS X 10.8 — Mountain Lion) by Dem Pilafian of Center Key Software.
Packaging a Java App for Distribution on a Mac (Oracle)
Submitting to the Mac App Store (Apple)
By the way, you can use both Swing and JavaFX in your app. JavaFX is now bundled with Java, at least as of Java 7 Update 40 according to these Release Notes.
Java 9
The newly released Java 9 should make this process of wrapping an app with a runtime even easier and better.
Modules
Java 9 brings “modularization”, Java Platform Module System (JPMS), where the approximately 5,000 classes bundled with a JVM are grouped into about a hundred chunks known as modules. You can include only the modules containing classes actually used by your particular app rather hauling along every library. If you don't use JSON or CORBA, then you need not include the JSON or CORBA related modules.
The new jlink tool does this work for you, to assemble and optimize a set of modules and their dependencies into a custom run-time image. As a bonus, some link-time optimization work is performed to make your app launch faster.
These features are supported on 64-bit macOS systems, along with other platforms.
Native compiling
Java 9 also brings new support for native compiling, to build an app optimized for specific hardware. But this ability is experimental, and not yet ready for production use.
Update, as of 2018
Oracle has announced the end of web deployment technology, including Java Web Start, the previously recommended avenue for desktop apps. So now, Oracle expects all Java apps to be delivered bundled with a JVM such as is done with the jlink tool mentioned above.
So, the process of wrapping an app with a Java runtime that was something of a rarity just for the Apple App Store is now mainstream. Oracle expects any Java-based desktop app to be installed this way.
Yes and no. Apple doesn't care what language your App is written in, but it cannot depend on or install anything outside of the binary you submit to Apple. The relevant guidelines are below:
2.14
Apps must be packaged and submitted using Apple's packaging technologies included in Xcode - no third party installers allowed
2.15
Apps must be self-contained, single application installation bundles, and cannot install code or resources in shared locations
2.16
Apps that download or install additional code or resources to add functionality or change their primary purpose will be rejected
So, while this means you can't depend on Ruby or Python, you could use a framework like MacRuby to build your app in Ruby and embed the Ruby interpreter. Speaking for MacRuby, apps have been successfully submitted to the Mac App Store this way.
Ruby ? Yes !
Apple already accepted one of my apps written in MacRuby.
AFAIK, Cocoa will allow you include code written in different languages (including Ruby and Python, but not Java I don't think).
Check the Apple SDK's for more info, but I believe (thanks to Cocoa) it's not a problem.
For Python apps, I wrote a comprehensive article explaining how to build and submit such apps to the Mac App Store. It includes source code and build scripts for a barebones example app that I have successfully submitted.
Submitting a Python App to the Mac App Store
Possibly Java, if you bundle the Java application into a mac application.

Resources