Fix NullPointerException in checkPermissions() caused by ProGuard obfuscation #3
+21
−3
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The Android plugin crashes with
NullPointerExceptionwhen callingcheckPermissions()in production builds. The root cause was ProGuard obfuscating the Capacitor plugin methods, preventing proper plugin initialization via reflection.Root Cause
ProGuard was obfuscating methods annotated with
@PluginMethodand@PermissionCallback. Since Capacitor uses reflection to invoke these methods, obfuscation caused the plugin to fail initialization, resulting ingetPermissionState()returning null and triggering NullPointerException.Changes
ProGuard Rules (Primary Fix)
Added ProGuard rules to prevent plugin method obfuscation:
Null Safety Checks (Defensive Programming)
Added null safety checks in four methods as a safety net:
toPermissionString(): Return"prompt"for null state (aligns with default case)requestPermissions(): Check state is non-null before comparisonensurePermission(): Check state is non-null before comparisonmicrophonePermissionStartCallback(): Check state is non-null before comparisonSolution Approach
This two-pronged approach ensures robustness:
The fix maintains backward compatibility while preventing crashes in production builds with ProGuard enabled.
Original prompt
This section details on the original issue you should resolve
<issue_title>bug: The app craches when calling checkPermissions() function with "java.lang.NullPointerException"</issue_title>
<issue_description>## Bug Report
Capacitor Version
Capacitor Doctor
Latest Dependencies:
@capacitor/cli: 8.0.0
@capacitor/core: 8.0.0
@capacitor/android: 8.0.0
@capacitor/ios: 8.0.0
Installed Dependencies:
@capacitor/ios: not installed
@capacitor/cli: 8.0.0
@capacitor/android: 8.0.0
@capacitor/core: 8.0.0
[success] Android looking great! 👌
Plugin Version
@capgo/[email protected]
Ok to proceed? (y) y
│
▲ 💊 Capgo Doctor 💊
│
● App Name: Taqvaylit
│
● App ID: io.ionic.taqvaylit
│
● App Version: 4.5
│
● Web Dir: ./dist
│
● OS: win32 Windows 10 Pro
│
● Node: v24.8.0
│
● Installed Dependencies:
│
● @capgo/cli: 7.68.1
│
● @capawesome/capacitor-android-edge-to-edge-support: 7.2.3
│
● @capgo/capacitor-audio-recorder: 8.0.4
│
◇ Latest Dependencies:
│
● @capgo/cli: 7.68.1
│
● @capawesome/capacitor-android-edge-to-edge-support: 8.0.2
│
● @capgo/capacitor-audio-recorder: 8.0.5
│
▲ 🚨 Some dependencies are not up to date
Error:
message: Some dependencies are not up to date
stack:
context(s)
Platform(s)
Android
Current Behavior
The app craches in production after requesting a record.
The app works well in dev
Expected Behavior
The app shouldn't crache when we start recording
Code Reproduction
here's the code that causes the issue:
here's the traceStack:
--------- beginning of crash
01-11 23:42:21.786 23661 23725 E AndroidRuntime: FATAL EXCEPTION: CapacitorPlugins
01-11 23:42:21.786 23661 23725 E AndroidRuntime: Process: io.ionic.taqvaylit, PID: 23661
01-11 23:42:21.786 23661 23725 E AndroidRuntime: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
01-11 23:42:21.786 23661 23725 E AndroidRuntime: at com.getcapacitor.a.run(r8-map-id-3fe3359b78ef5b550c9f1d72a51fef057b0db10015f59f0d32d3352e1af5a31e:62)
01-11 23:42:21.786 23661 23725 E AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:938)
01-11 23:42:21.786 23661 23725 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
01-11 23:42:21.786 23661 23725 E AndroidRuntime: at android.os.Looper.loopOnce(Looper.java:226)
01-11 23:42:21.786 23661 23725 E AndroidRuntime: at android.os.Looper.loop(Looper.java:313)
01-11 23:42:21.786 23661 23725 E AndroidRuntime: at android.os.HandlerThread.run(HandlerThread.java:67)
01-11 23:42:21.786 23661 23725 E AndroidRuntime: Caused by: java.lang.reflect.InvocationTargetException
01-11 23:42:21.786 23661 23725 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
01-11 23:42:21.786 23661 23725 E AndroidRuntime: at com.getcapacitor.F.a(r8-map-id-3fe3359b78ef5b550c9f1d72a51fef057b0db10015f59f0d32d3352e1af5a31e:26)
01-11 23:42:21.786 23661 23725 E AndroidRuntime: at com.getcapacitor.a.run(r8-map-id-3fe3359b78ef5b550c9f1d72a51fef057b0db1...
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.