Skip to content

Commit 43f43b2

Browse files
authored
fix(android): prevent crash in getHasGms() when play-services-base is missing (#42)
* fix(android): prevent crash in getHasGms() when play-services-base is missing * refactor(android): catch NoClassDefFoundError explicitly instead of Throwable Address PR review feedback: catching Throwable is too broad and can mask serious JVM errors like OutOfMemoryError or StackOverflowError. Now catches Exception and NoClassDefFoundError separately for more explicit error handling.
1 parent adf3115 commit 43f43b2

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [1.4.1]
9+
10+
### Fixed
11+
12+
- **iOS**: `getIpAddress()` and `getIpAddressSync()` now consistently return IPv4 addresses when available ([#39](https://github.com/l2hyunwoo/react-native-nitro-device-info/issues/39))
13+
- Previously, the functions could return IPv6 addresses inconsistently due to network interface enumeration order
14+
- IPv4 addresses are now prioritized, with IPv6 used only as a fallback when no IPv4 is available
15+
16+
- **Android**: `getHasGms()` no longer crashes when `play-services-base` dependency is not included ([#40](https://github.com/l2hyunwoo/react-native-nitro-device-info/issues/40))
17+
- Previously, calling `getHasGms()` without the GMS dependency would throw a JNI exception (`NoClassDefFoundError`)
18+
- The function now catches `NoClassDefFoundError` explicitly (not `Throwable`) to avoid masking serious JVM errors
19+
- This allows apps distributed on non-GMS stores (Amazon, Huawei AppGallery) to safely use this API
20+
821
## [1.4.0] - 2025-12-01
922

1023
### Added
@@ -161,13 +174,15 @@ const charging = DeviceInfoModule.getIsBatteryCharging();
161174
All getter functions have been converted to readonly properties for cleaner, more intuitive access ([#20](https://github.com/l2hyunwoo/react-native-nitro-device-info/pull/20)):
162175

163176
**Before (v1.1.x)**:
177+
164178
```typescript
165179
const brand = DeviceInfoModule.getBrand();
166180
const model = DeviceInfoModule.getModel();
167181
const isTablet = DeviceInfoModule.isTablet();
168182
```
169183

170184
**After (v1.2.0)**:
185+
171186
```typescript
172187
const brand = DeviceInfoModule.brand;
173188
const model = DeviceInfoModule.model;

android/src/main/java/com/margelo/nitro/nitrodeviceinfo/DeviceInfo.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,14 +486,23 @@ class DeviceInfo : HybridDeviceInfoSpec() {
486486
override val supportedAbis: Array<String>
487487
get() = Build.SUPPORTED_ABIS
488488

489-
/** Check if Google Mobile Services (GMS) is available */
489+
/**
490+
* Check if Google Mobile Services (GMS) is available
491+
* Catches both Exception and NoClassDefFoundError to handle cases where
492+
* play-services-base dependency is not included in the app.
493+
*/
490494
override fun getHasGms(): Boolean {
491495
return try {
492496
val result =
493497
GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context)
494498
result == ConnectionResult.SUCCESS
495499
} catch (e: Exception) {
496-
Log.w(NAME, "GMS not available or GMS library not found", e)
500+
Log.d(NAME, "GMS not available", e)
501+
false
502+
} catch (e: NoClassDefFoundError) {
503+
// NoClassDefFoundError is thrown when GMS library is not included
504+
// This is expected in apps distributed on non-GMS stores (Amazon, Huawei AppGallery)
505+
Log.d(NAME, "GMS library not found", e)
497506
false
498507
}
499508
}

0 commit comments

Comments
 (0)