Skip to content

Commit 63a5776

Browse files
Update samples
1 parent 47f68a7 commit 63a5776

18 files changed

Lines changed: 196 additions & 488 deletions

File tree

‎samples/sample-bugsnag/CrashKiOSSampleIOS/CrashKiOSSampleIOS.xcodeproj/project.pbxproj‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@
389389
);
390390
PRODUCT_BUNDLE_IDENTIFIER = co.touchlab.CrashKiOSSampleIOS;
391391
PRODUCT_NAME = "$(TARGET_NAME)";
392+
SWIFT_OBJC_BRIDGING_HEADER = CrashKiOSSampleIOS/BridgingHeader.h;
392393
SWIFT_VERSION = 5.0;
393394
TARGETED_DEVICE_FAMILY = "1,2";
394395
};
@@ -410,6 +411,7 @@
410411
);
411412
PRODUCT_BUNDLE_IDENTIFIER = co.touchlab.CrashKiOSSampleIOS;
412413
PRODUCT_NAME = "$(TARGET_NAME)";
414+
SWIFT_OBJC_BRIDGING_HEADER = CrashKiOSSampleIOS/BridgingHeader.h;
413415
SWIFT_VERSION = 5.0;
414416
TARGETED_DEVICE_FAMILY = "1,2";
415417
};

‎samples/sample-bugsnag/CrashKiOSSampleIOS/CrashKiOSSampleIOS/AppDelegate.swift‎

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ import Bugsnag
1818
class AppDelegate: UIResponder, UIApplicationDelegate {
1919

2020
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
21-
// Override point for customization after application launch.
22-
let config = BugsnagConfiguration.loadConfig()
23-
BugsnagConfigKt.startBugsnag(config: config)
21+
// Configures Bugsnag for Kotlin crash handling, starts it, and registers the
22+
// sink with the Kotlin side (which installs the unhandled-exception hook).
23+
let sink = BugsnagSink.start(BugsnagConfiguration.loadConfig())
24+
BugsnagKt.registerBugsnagSink(sink: sink)
2425

2526
return true
2627
}
@@ -40,3 +41,75 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
4041
}
4142
}
4243

44+
// Copy of the reference sink from the CrashKiOS Swift package
45+
// (Sources/CrashKiOSBugsnag/BugsnagSink.swift), inlined + the protocol declared in
46+
// BridgingHeader.h so the sample doesn't need the package wired into the Xcode
47+
// project. Prefer consuming the CrashKiOSBugsnag package product in a real app.
48+
final class BugsnagSink: NSObject, CrashKiOSBugsnagSink {
49+
50+
/// Configures `config` to suppress the duplicate termination crash that follows a
51+
/// recorded Kotlin fatal, starts Bugsnag, and returns the sink.
52+
static func start(_ config: BugsnagConfiguration) -> BugsnagSink {
53+
overrideOriginalUnhandledValue()
54+
config.addOnSendError { event in
55+
!event.unhandled || !event.featureFlags.contains { $0.name == kotlinCrashedFeatureFlag }
56+
}
57+
config.clearFeatureFlag(name: kotlinCrashedFeatureFlag)
58+
Bugsnag.start(with: config)
59+
return BugsnagSink()
60+
}
61+
62+
func leaveBreadcrumb(_ message: String) {
63+
Bugsnag.leaveBreadcrumb(withMessage: message)
64+
}
65+
66+
func notify(with exceptions: [NSException], handled: Bool) {
67+
guard let exception = exceptions.first else { return }
68+
Bugsnag.notify(exception) { event in
69+
if handled {
70+
event.severity = .warning
71+
} else {
72+
event.unhandled = true
73+
event.severity = .error
74+
}
75+
event.errors += exceptions.dropFirst().map(BugsnagError.init)
76+
return true
77+
}
78+
}
79+
80+
func addMetadata(_ value: Any, key: String, section: String) {
81+
Bugsnag.addMetadata(value, key: key, section: section)
82+
}
83+
84+
func markFatalCrashRecorded() {
85+
// Called only by the Kotlin unhandled-exception hook, right before termination —
86+
// never on direct sendFatalException() calls, which would poison the session and
87+
// make the OnSendError filter discard later genuine crashes.
88+
Bugsnag.addFeatureFlag(name: kotlinCrashedFeatureFlag)
89+
}
90+
}
91+
92+
/// Feature flag used to mark the Kotlin termination crash.
93+
private let kotlinCrashedFeatureFlag = "crashkios.kotlin_crashed"
94+
95+
/// In Bugsnag 6.26.2+ the `originalUnhandledValue` property prevents our synthetic
96+
/// unhandled exceptions from being stored to disk; alias it to `unhandled`.
97+
/// https://github.com/bugsnag/bugsnag-cocoa/pull/1549
98+
private func overrideOriginalUnhandledValue() {
99+
guard let handledStateClass = NSClassFromString("BugsnagHandledState"),
100+
let originalMethod = class_getInstanceMethod(handledStateClass, NSSelectorFromString("originalUnhandledValue")),
101+
let method = class_getInstanceMethod(handledStateClass, NSSelectorFromString("unhandled"))
102+
else { return }
103+
method_setImplementation(originalMethod, method_getImplementation(method))
104+
}
105+
106+
private extension BugsnagError {
107+
/// Creates a BugsnagError from a (Kotlin-synthesized) NSException.
108+
convenience init(_ exception: NSException) {
109+
self.init()
110+
errorClass = exception.name.rawValue
111+
errorMessage = exception.reason
112+
stacktrace = BugsnagStackframe.stackframes(withCallStackReturnAddresses: exception.callStackReturnAddresses)
113+
type = .cocoa
114+
}
115+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef BridgingHeader_h
2+
#define BridgingHeader_h
3+
4+
// Copy of Sources/CrashKiOSBugsnagObjC/include/CrashKiOSBugsnagSink.h.
5+
// The Kotlin framework's umbrella header only FORWARD-declares this protocol
6+
// (`@protocol CrashKiOSBugsnagSink;`), so Swift needs the real declaration to
7+
// conform to it. Apps consuming the CrashKiOSBugsnag Swift package get it from
8+
// the package and don't need this file.
9+
#import <Foundation/Foundation.h>
10+
11+
@protocol CrashKiOSBugsnagSink <NSObject>
12+
- (void)leaveBreadcrumb:(NSString * _Nonnull)message;
13+
- (void)notifyWithExceptions:(NSArray<NSException *> * _Nonnull)exceptions handled:(BOOL)handled;
14+
- (void)addMetadata:(id _Nonnull)value key:(NSString * _Nonnull)key section:(NSString * _Nonnull)section;
15+
- (void)markFatalCrashRecorded;
16+
@end
17+
18+
#endif /* BridgingHeader_h */

‎samples/sample-bugsnag/build.gradle.kts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
plugins {
1111
alias(projectLibs.plugins.kotlin.multiplatform) apply false
1212
alias(projectLibs.plugins.android.library) apply false
13-
alias(projectLibs.plugins.crashkios.bugsnaglink) apply false
1413
alias(libs.plugins.bugsnag.gradle.plugin) apply false
1514
}
1615

‎samples/sample-bugsnag/gradle.properties‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,3 @@ android.useAndroidX=true
1313

1414
org.gradle.jvmargs=-Xmx4g
1515

16-
kotlin.native.cacheKind=none
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.11.1-bin.zip
4+
networkTimeout=10000
5+
validateDistributionUrl=true
46
zipStoreBase=GRADLE_USER_HOME
57
zipStorePath=wrapper/dists

‎samples/sample-bugsnag/shared/build.gradle.kts‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@
77
*
88
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
99
*/
10-
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
10+
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
1111

1212
plugins {
1313
id("com.android.library")
1414
kotlin("multiplatform")
1515
kotlin("native.cocoapods")
16-
id("co.touchlab.crashkios.bugsnaglink")
1716
}
1817

1918
android {
@@ -28,14 +27,14 @@ android {
2827
}
2928
}
3029

31-
tasks.withType<KotlinCompile> {
32-
kotlinOptions.jvmTarget = "1.8"
33-
}
34-
3530
version = "0.0.1"
3631

3732
kotlin {
38-
androidTarget()
33+
androidTarget {
34+
compilerOptions {
35+
jvmTarget.set(JvmTarget.JVM_1_8)
36+
}
37+
}
3938
iosX64()
4039
iosArm64()
4140
// Note: iosSimulatorArm64 target requires that all dependencies have M1 support

‎samples/sample-crashlytics/build.gradle.kts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@ buildscript {
1515
dependencies {
1616
classpath(libs.google.services)
1717
classpath(libs.firebase.crashlytics.gradle)
18-
classpath(libs.crashkios.utils)
1918
}
2019
}
2120
plugins {
2221
alias(projectLibs.plugins.kotlin.multiplatform) apply false
2322
alias(projectLibs.plugins.android.library) apply false
24-
alias(projectLibs.plugins.crashkios.crashlyticslink) apply false
2523
}
2624

2725
allprojects{

‎samples/sample-crashlytics/gradle.properties‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,3 @@ android.useAndroidX=true
1313

1414
org.gradle.jvmargs=-Xmx4g
1515

16-
kotlin.native.cacheKind=none

‎samples/sample-crashlytics/gradle/libs.versions.toml‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22
firebase-bom = "32.2.3"
33
firebase-crashlytics-gradle = "2.9.9"
44
google-services = "4.3.15"
5-
utils = "0.7.1-alpha3"
65

76
[libraries]
87
firebase-bom = { module = "com.google.firebase:firebase-bom", version.ref = "firebase-bom" }
98
firebase-analytics = { module = "com.google.firebase:firebase-analytics-ktx" }
109
firebase-crashlytics = { module = "com.google.firebase:firebase-crashlytics-ktx" }
1110
firebase-crashlytics-gradle = { module = "com.google.firebase:firebase-crashlytics-gradle", version.ref = "firebase-crashlytics-gradle" }
1211
google-services = { module = "com.google.gms:google-services", version.ref = "google-services" }
13-
crashkios-utils = { module = "co.touchlab.crashkios:utils", version.ref = "utils" }

0 commit comments

Comments
 (0)