Recursive AI

Add a custom Android activity to a C# game

onegame is a free, open-source game engine for .NET from Recursive AI. MIT or Apache-2.0, no editor required.

Use a Java NativeActivity for launch and keep the game in C#. The exporter does not compile Kotlin.

Terminal
dotnet tool install --global RecursiveAI.OneGame.Cli

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

JAVA
package demo;
 
public class GameActivity
    extends android.app.NativeActivity {
    @Override
    protected void onCreate(android.os.Bundle state) {
        super.onCreate(state);
        android.util.Log.i("GameActivity", "Ready");
    }
}

Save the class as android/GameActivity.java beside the project. Set the .csproj property <AndroidActivity>demo.GameActivity</AndroidActivity> to select it at export.

Unzip the example, then dotnet run.

The full program

The project uses RecursiveAI.OneGame.Sdk/2.5.3; the SDK is declared on <Project>, not added as a package reference. Here are all the inputs alongside Program.cs and android/GameActivity.java:

XML
<Project Sdk="RecursiveAI.OneGame.Sdk/2.5.3">
  <PropertyGroup>
    <TargetFramework>net10.0</TargetFramework>
    <AssemblyName>Example</AssemblyName>
    <RootNamespace>Example</RootNamespace>
    <AndroidJavaDir>android</AndroidJavaDir>
    <AndroidActivity>demo.GameActivity</AndroidActivity>
    <AndroidManifestFragment>android/manifest.xml</AndroidManifestFragment>
    <AndroidIcon>android/icon.svg</AndroidIcon>
    <AndroidIconBackground>#152133</AndroidIconBackground>
  </PropertyGroup>
</Project>

The fragment adds android:immersive to the same activity rather than declaring a second launcher. Save it as android/manifest.xml:

XML
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
  <application>
    <activity android:name="demo.GameActivity"
              android:immersive="true" />
  </application>
</manifest>

The foreground art is android/icon.svg:

XML
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 108 108" width="108" height="108">
  <circle cx="54" cy="54" r="28" fill="#61a9f8"/>
</svg>
C#
using System;
using OneGame.Core;
using OneGame.Host;
using OneGame.UI;
 
internal static class Program
{
    internal static int Main(string[] args)
    {
        if (!OperatingSystem.IsAndroid() && args.Length == 0)
            args = ["--headless", "--gpu", "off", "--frames", "1"];
        return GameApp.Run(new Demo(), args);
    }
}
 
sealed class Demo : GameDefinition
{
    public override string Name => "ActivityDemo";
    public override void RegisterEntities(EntityRegistry registry) { }
    public override void Setup(World world) => Console.WriteLine("C# game world ready.");
    public override void DefineScreens(ScreenSet screens) =>
        screens.Add("main", _ => Ui.Label("Ready"));
}

On desktop, dotnet run starts the C# game headlessly and prints:

Text
C# game world ready.
ActivityDemo: 1 frames, tick 1, hash 9eb3622df396ae9d

The Java class compiled against Android API 36 and d8 produced classes.dex. The desktop run doesn’t start the activity. No APK or bundle was built or run for this example.

On a machine with the Android SDK, NDK, and a JDK, set up Android export and run:

Terminal
onegame export --target android

If export reports activity_not_found, check the fully qualified Java class name and that its file is under AndroidJavaDir.

Variations

To customize the launcher icon without changing the activity, keep just these properties in an existing game project and copy in the SVG above:

XML
<AndroidIcon>android/icon.svg</AndroidIcon>
<AndroidIconBackground>#152133</AndroidIconBackground>

The short answer

Put your Java subclass of android.app.NativeActivity under android/, name it with AndroidActivity in your OneGame project, and export for Android. Keep super.onCreate(state): it lets the base activity load the native game library. In C#, keep a named Program class with int Main(string[] args) so the generated Android entry point can call it.

How does Android launch a custom activity for a C# game?

The Android manifest declares the activity the system creates. In OneGame the exporter makes the selected class the launcher, preserves the android.app.lib_name metadata, and includes compiled Java in the package. The native entry point then starts the C# game. Use the Java subclass for Android lifecycle work and leave gameplay and rendering in C#.

If there’s only one top-level NativeActivity subclass, the exporter can select it without AndroidActivity. If it finds several candidates, it reports activity_ambiguous; set the project property to pick one. Java sources compile against android.jar, so the export does not supply a Kotlin compiler or arbitrary Java libraries on the classpath.

How do you change the Android manifest and launcher icon for a game?

Use AndroidManifestFragment to add manifest entries rather than replacing the generated file. Name the main activity exactly as it appears in AndroidActivity to add attributes or children to it. Put permissions and features directly under the fragment’s manifest element. If the fragment contradicts a generated setting, export stops with manifest_conflict instead of silently replacing that setting. For example, use the export’s orientation option rather than overriding the generated android:screenOrientation in the fragment.

AndroidIcon sets the square foreground image and AndroidIconBackground supplies its background. The icon here uses a 108-unit square with a centered circle inside the middle 72 units, the portion the OneGame docs recommend for important art. Export generates density-specific icons as well as the adaptive launcher resources.

All onegame recipes