Skip to content

StrictMode violation: system font list loads on main thread #419

@sebastian-gallese-speak

Description

Description

When a RiveAnimationView is attached on Android with a custom FileAssetLoader that calls FontHelper.getSystemFontList(), StrictMode reports an UnbufferedIoViolation. The call performs disk IO (parsing fonts.xml) on the main thread during view attachment, so any app that enables StrictMode and loads a Rive animation with text will hit the violation.

Provide a Repro

  class SampleActivity : ComponentActivity() {

      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)

          StrictMode.setThreadPolicy(
              StrictMode.ThreadPolicy.Builder()
                  .detectAll()
                  .penaltyLog()
                  .build()
          )

          setContentView(
              RiveAnimationView(this).apply {
                  setRiveResource(R.raw.sample_with_text)
                  setAssetLoader(object : FileAssetLoader() {
                      override fun loadContents(asset: FileAsset, inBandBytes: ByteArray): Boolean {
                          if (asset is FontAsset) {
                              if (inBandBytes.isNotEmpty() && asset.decode(inBandBytes)) return true
                              // This triggers FontHelper.getSystemFontList() -> disk IO on main thread.
                              val families = FontHelper.getSystemFontList()
                              val firstFont = families.firstOrNull()?.fonts?.values?.firstOrNull()?.firstOrNull()
                              return firstFont?.let { asset.decode(FontHelper.getFontBytes(it)) } ?: false
                          }
                          return false
                      }
                  })
              }
          )
      }
  }
  1. Enable StrictMode as above.
  2. Launch the activity so RiveAnimationView is attached.
  3. Observe the StrictMode logcat entry: StrictMode policy violation: UnbufferedIoViolation originating from FontHelper.getSystemFontList() during SystemFontsParser.parseFontsXML.

Source .riv/.rev file

Any .riv file

Expected behavior

Loading system fonts (or any font metadata) should not perform disk IO on the main thread. The call should either run asynchronously, cache results before UI usage, or expose an async API so apps can avoid StrictMode violations.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions