How can I reference Mono.Android in a NetStandard Library?

Good morning! I have a Build DAG that looks kinda like the diagram below (sorry for my crude MSPaint Drawing).

My setup

I have a platform-independent Engine.csproj and Game.csproj where most of the core code lives. The leaf nodes (the things I actually build) are GameAndroid and GameDesktopGL which are platform specific obviously.

Since Engine.csproj is totally platform agnostic, I need a really thin layer that handles some Desktop specific stuff when I build desktop (stuff like GameWindow.TextInput that only exists on DesktopGL). To accomplish this I created a NetStandard Library that depends on MonoGame.Framework.DesktopGL (EngineDesktopGL.csproj). So it knows about DesktopGL functionality despite being a NetStandard library. It might be hard to see in the diagram but this means that EngineDesktopGL.csproj has a blue and an orange box around it.

I want to have a similar middle-layer for Android-specific stuff. But this isn’t as simple as swapping DesktopGL for MonoGame.Framework.Android. I also need real “Android” platform stuff (particularly stuff from Mono.Android).

My first solution (worked but I don’t like it)

So I have a thing that… works. But it’s super hacky and gross and I was hoping for something better. What I could do is rebuild EngineAndroid.csproj as a MonoGame Android Template (so rather than being a NetStandard library it’s a whole-ass game that I just never build on its own). This appears to work but it does mean that EngineAndroid is way bigger and has more dependencies than it needs. It also means that EngineAndroid can be run on its own which is undesirable.

My second solution (didn’t work)

It feels like I should be able to copy the <Reference Include="Mono.Android" /> line from GameAndroid.csproj and use it in EngineAndroid.csproj but that doesn’t seem to work. It seems Mono.Android doesn’t work like a normal package.

This seems like it should be an uncomplicated problem. But it’s feeling complicated. What am I missing?

I feel so silly. The answer is so obvious.

What I wanted was a minimal project that was:

  • A Library (aka NetStandard, not runnable on its own)
  • Included Mono.Android.

So what I wanted was… a completely standard Android Class Library.

1 Like

Not sure if this is what you’re looking for, but I have a similar setup:
An engine (shared project), the game code (netstandard) and 3 separate run configs: desktop, Android and iOS.
You can check my project setup/configuration here:

1 Like

Thanks! This is pretty much what I already have.

In addition to your setup in the blog post I want an additional layer of “Android-specific engine code” and “DesktopGL-specific engine code”. The DesktopGL part is easy-- just use a netstandard library template, just like the engine itself. The android half is what had me stumped before.

1 Like