The right way to provide Android resources in Unity.

The right way to provide Android resources in Unity.

Back in the good old days in order to provide Android resources in your Unity game you would have to only place them inside Plugins/Android/res folder.

I am not really sure when Unity decided to deprecate this, but with the latest Unity LTS version (the latest version at the moment is 2021.3.25f) you will have to find another way to provide resources.

The right way to do this is to create an Android library which contains the resource files we want to use in our project.

For this sample we will create a simple project where we will post local notifications from our game and customize them with different notification icons.

First step

At first we just created a new Unity project with an already selected Android platform. Using the package manager import Mobile Notifications (com.unity.mobile.notifications) package. Once that’s done we are ready to create our first script which will post notifications.

Let’s call it NotificationsManager:

public class NotificationsManager : MonoBehaviour
{
    // TO BE DONE!
}

In order to create and post a notification in Android we will have to create a Channel first. Let’s do that in Start():

private const string ChannelId = "generic_channel_id";

private void Start()
{
    var channel = new AndroidNotificationChannel()
    {
        Id = ChannelId,
        Name = "Generic Channel",
        Importance = Importance.Default,
        Description = "Generic notifications",
    };
    AndroidNotificationCenter.RegisterNotificationChannel(channel);
}

Create a few buttons inside our main layout which will be used in order to post the notifications.

[SerializeField]
private Button BasicNotification;
[SerializeField]
private Button DailyRewardlNotification;
[SerializeField]
private Button UpdateNotification;

Starting with Android 13.0 (API level 33) notifications can not be posted without users permission. They can still be scheduled, but will work silently with no UI shown to the user. So let’s request the permission inside a coroutine and if the user allows it, enable the buttons.

private IEnumerator RequestNotificationPermission()
{
    var request = new PermissionRequest();
    while (request.Status == PermissionStatus.RequestPending)
        yield return null;

    if (request.Status == PermissionStatus.Allowed)
    {
        BasicNotification.enabled = true;
        DailyRewardlNotification.enabled = true;
        UpdateNotification.enabled = true;
    }
}

Now comes the part of writing the code which will actually post the notifications and it’s simple as that:

private void PostBasicNotification()
{
    var notification = new AndroidNotification
    {
        Title = "Simple Notification",
        Text = "This is a test with a simple notification",
        FireTime = System.DateTime.Now.AddSeconds(10)
    };
    AndroidNotificationCenter.SendNotification(notification, ChannelId);
}

Second step

Now let’s start with creating the Android library which will have our notification icons. In order to do so we will have to download and install Android Studio in case we don’t have it.

Once that’s done we create a new project : Create new project

Since we will create a library which will only contain icon resources we don’t really need to create an Activity. Add your library’s name and package name (and other settings) and Finish the process. Your project should look something similar to this:

Main Project

res folder

You are free to delete everything inside res folder ( all files & folders ) because we don’t really need them otherwise all those resources will be added inside the apk file generated from Unity. Once that’s done you are ready to place your icons. I use a pretty useful online tool to generate Android resources, you are free to use it if you like: Android Icon Generator

Your res folder should look something like this:

Res Folder

Before building our library file we have two files to update. Android Studio generates a basic AndroidManifest.xml with all kinds of information here which we don’t really need for a library which has only resources.

Manifest file generated by Android Studio:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Notificationicons"
        tools:targetApi="31" />

</manifest>

A simple one:

<?xml version="1.0" encoding="utf-8"?>
<manifest />

One last thing to update is build.gradle file. It comes with dependencies which we don’t need too. Update the file to this:

plugins {
    id 'com.android.library'
}

android {
    namespace 'com.hardartcore.notificationicons'
    compileSdk 33

    defaultConfig {
        targetSdk 33
    }

    buildTypes {
        release {
            minifyEnabled false
        }
    }
}

Third step

Generating the library file is as easy as running a gradle command. Open Gradle menu in Android Studio and open module name -> Tasks -> other and run assembleRelease. This will generate the aar file inside build/outputs/aar/ folder.

That’s the file which we will place inside the Unity Project inside Plugins/Android/ folder.

And VOILA! That’s it, we can use the icons.

private void PostDailyRewardNotification()
{
    var notification = new AndroidNotification();
    notification.Title = "Hey master!";
    notification.Text = "Come and collect your daily reward before it expires!";
    notification.FireTime = System.DateTime.Now.AddSeconds(10);
    notification.SmallIcon = "ic_stat_card_giftcard";

    AndroidNotificationCenter.SendNotification(notification, ChannelId);
}

private void PostUpdateNotification()
{
    var notification = new AndroidNotification();
    notification.Title = "IMPORTANT MESSAGE!";
    notification.Text = "A new update has just arrived, go and download it from Play Store!";
    notification.FireTime = System.DateTime.Now.AddSeconds(10);
    notification.SmallIcon = "ic_stat_system_update";

    AndroidNotificationCenter.SendNotification(notification, ChannelId);
}

In case you want to try it yourself here is a link to the Unity project: Download

If you want me to continue writing interesting stuff you can support me via:

hardartcore
hardartcore
Fulltime Android developer!