Skip to content

Better error reporting when an expected bridged method is not found #85

@marcprux

Description

@marcprux

The R8/proguard process for Android release builds will often minify (i.e., mangle) Java names. When the Swift side of the bridge attempts to look up a method name that cannot be found like so:

let Java_set_count_methodID = Java_class.getMethodID(name: "setCount", sig: "(I)V")!

the program will simply crash with the unhelpful error:

Fatal error: Unexpectedly found nil while unwrapping an Optional value

What we might instead do is have the method lookup happen in a forced try! with a throwable function, so the error log will at least contain the name of the method that was sought:

let Java_set_count_methodID = try! Java_class.getMethodIDAndThrowErrorIfMissing(name: "setCount", sig: "(I)V")

That way, we can get an error like:

Fatal error: Could not find Java method setCount on the class APIClient bridged from APIClient.swift:1234 (release mode; did you add it to your proguard-rules.pro file?)

This will at least provide some guidance to the user at development time (and in Crashlytics reports if it happens in a deployed app) about the potential cause of the method not being found.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions