Writing

expo-notifications custom sound plays the default tone in release builds: R8 deleted it

By Mohamed Gado · · 3 min read

Rafiq, my prayer times app, plays the adhan as a notification sound. A user wrote that the notification arrived but the adhan did not play. Debug builds were fine, the emulator was fine and 1,814 tests were green. On every release build, the phone played its ordinary notification tone.

The symptom#

  • The notification arrives on time.
  • The sound is the system default instead of your file.
  • It only happens in release builds with resource shrinking on.
  • No error is logged anywhere.

Read the channel before the code#

My first move was to reread the scheduler, which was a waste. Android owns the channel, so ask Android:

adb shell dumpsys notification

Find your channel in the output and read mSound. Mine said:

mSound=content://settings/system/notification_sound

The channel had been created with the wrong sound. Nothing about scheduling could cause or fix that. Then check whether the file is in the APK:

aapt2 dump resources app-release.apk

There was no raw/adhan_nafees in the list. The merged resources folder of the same build had it, so something removed it after the merge.

Check Do Not Disturb before you trust a listening test. It was on during my first one, and it silences a working sound just as well.

Why R8 removes it#

expo-notifications finds your sound by its name at runtime. This is SoundResolver.kt:

val resourceId = context.resources.getIdentifier(resourceName, "raw", packageName)
// If resourceId is 0, then the resource does not exist.
// Returning null falls back to using a default sound.
if (resourceId != 0) {

There is no R.raw.adhan_nafees anywhere in the compiled code. The resource shrinker keeps what it can see being used, and a name built at runtime is invisible to it. So it deletes the file, getIdentifier returns 0, and the resolver quietly returns the default tone.

The confusing part is that another copy survived: raw/assets_sounds_adhan_nafees, the one Metro bundles for the in-app preview. Metro writes a keep.xml for its own assets. Nothing wrote one for the notification sounds.

Fix 1: give the shrinker a reference#

The manifest is a root for the shrinker, so anything it names with android:resource is kept. A small config plugin adds one meta-data entry per sound:

const { withAndroidManifest } = require('expo/config-plugins');
 
const RAW_SOUNDS = ['adhan_nafees', 'takbir'];
 
module.exports = (config) =>
  withAndroidManifest(config, (cfg) => {
    const app = cfg.modResults.manifest.application[0];
    const meta = (app['meta-data'] ??= []);
    for (const name of RAW_SOUNDS) {
      const key = `rafiq.sound.${name}`;
      if (meta.some((m) => m.$?.['android:name'] === key)) continue;
      meta.push({ $: { 'android:name': key, 'android:resource': `@raw/${name}` } });
    }
    return cfg;
  });

Use your own prefix in place of rafiq. The names are the file names without the extension, and the list must match the sounds array you pass to expo-notifications.

The other known route is your own res/raw/keep.xml. I avoided it because Metro already generates a file with that exact name for the release variant, and two resources called raw/keep mean one of them can lose its entries.

Fix 2: the channels that already exist#

Shipping the file does not repair anyone who installed the broken build. A notification channel's sound cannot be changed by the app after the channel is created, so those users would keep the default tone.

The only way out is a channel with a new id. I put a generation in it:

const CHANNEL_GENERATION = 'v2';
 
return sound.file ? `${CHANNEL_ID}-${sound.id}-${CHANNEL_GENERATION}` : CHANNEL_ID;

Bump it when a release created channels with the wrong sound. Two things to handle with it:

  1. Notifications that are already scheduled still name the old channel, so schedule them again after the update.
  2. Delete the old channel only once nothing uses it. In the same dump I found other notifications arriving on expo's fallback channel, because a cleanup had deleted theirs.

Check that it worked#

  1. Run aapt2 dump resources on the new APK and look for raw/<your sound>.
  2. Install over the old version, not on a clean phone. A clean install hides the channel problem.
  3. Read mSound for the new channel id in dumpsys notification.
  4. Fire a real scheduled notification on the release build and listen. adb shell dumpsys audio also shows the player: mine was a 17-second one, the length of the adhan file.

Make step 1 part of your release script. A green build says nothing about which resources survived it.

What I have not tested#

I confirmed the getIdentifier call in expo-notifications 58.1.8. I did not test every version of R8 or the Android Gradle Plugin. If you don't use resource shrinking and still hear the default tone, read mSound first: a channel created before you added the sound gives the same result.

Related: Android 16 muting the second notification, and expo-audio stopping in the background.