Skip to content

Fix Android build failure caused by Kotlin 2.x null-safety in MusicModule #2579

@imniloy

Description

@imniloy

Context

With the release of newer Android toolchains (Kotlin 2.x) and modern React Native stacks
(e.g. React Native 0.81.x / Expo SDK 54), Kotlin’s null-safety rules are now enforced
more strictly at compile time.

While upgrading an existing project to this stack, I encountered a reproducible
Android build failure originating from MusicModule.kt.

This PR addresses that issue in a minimal, safe, and backward-compatible way.


The Problem

In MusicModule.kt, the following methods:

  • getTrack
  • getActiveTrack

call:

Arguments.fromBundle(track.originalItem)

However, track.originalItem is defined as a nullable Bundle?,
while Arguments.fromBundle() expects a non-null Bundle.

With Kotlin 2.x, this results in a compile-time error similar to:

Argument type mismatch: actual type is 'Bundle?', but 'Bundle' was expected

Earlier Kotlin versions were more permissive, which allowed this code to compile.
With stricter null-safety enforcement, the build now fails consistently.

Why This Happens

originalItem may legitimately be null in certain runtime scenarios (e.g. defensive clearing, edge cases during queue updates, or internal state transitions).

From a Kotlin perspective, passing a nullable value into a non-null API is no longer allowed and must be handled explicitly.

The Fix

This PR introduces explicit null handling before calling Arguments.fromBundle():

  • If originalItem is null, null is returned to JS
  • If non-null, the bundle is safely converted using Arguments.fromBundle()

This keeps behavior predictable, avoids runtime crashes, and satisfies Kotlin’s stricter type system.

Safety & Backward Compatibility

  • No API changes
  • No behavior changes for valid/non-null tracks
  • Only adds explicit null handling where it was previously implicit
  • Fully backward-compatible with existing JS usage

Testing

Tested on Android with:

  • React Native 0.81.x
  • Expo SDK 54
  • Kotlin 2.x
  • Gradle 8.x

The Android build succeeds after this change,
and existing Track Player functionality remains unaffected.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions