@replayd/capacitor-android 0.0.0-stage → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,24 @@
1
+ // @replayd/capacitor-android (T-154). Built by the app's Gradle as a Capacitor plugin module (npx cap sync).
2
+ ext {
3
+ capacitorVersion = project.hasProperty('capacitorVersion') ? rootProject.ext.capacitorVersion : '6.0.0'
4
+ }
5
+
6
+ apply plugin: 'com.android.library'
7
+ apply plugin: 'kotlin-android'
8
+
9
+ android {
10
+ namespace 'com.replayd.capacitor'
11
+ compileSdk project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 34
12
+ defaultConfig {
13
+ minSdkVersion project.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 22
14
+ }
15
+ compileOptions {
16
+ sourceCompatibility JavaVersion.VERSION_17
17
+ targetCompatibility JavaVersion.VERSION_17
18
+ }
19
+ kotlinOptions { jvmTarget = '17' }
20
+ }
21
+
22
+ dependencies {
23
+ implementation project(':capacitor-android')
24
+ }
@@ -0,0 +1 @@
1
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android" />
@@ -0,0 +1,142 @@
1
+ package com.replayd.capacitor
2
+
3
+ import android.app.ActivityManager
4
+ import android.app.ApplicationExitInfo
5
+ import android.content.Context
6
+ import android.os.Build
7
+ import com.getcapacitor.JSArray
8
+ import com.getcapacitor.JSObject
9
+ import com.getcapacitor.Plugin
10
+ import com.getcapacitor.PluginCall
11
+ import com.getcapacitor.PluginMethod
12
+ import com.getcapacitor.annotation.CapacitorPlugin
13
+ import org.json.JSONObject
14
+ import java.io.File
15
+
16
+ /**
17
+ * T-154: native crashes and ANRs for replayd. An uncaught Java/Kotlin exception is written to app storage by a handler
18
+ * chained before the previous one (≤ 64 KB each, ≤ 5 kept); on API 30+ the system's ApplicationExitInfo adds ANRs and
19
+ * native crashes. The SDK's Capacitor chunk calls `pending()` on the next launch and sends them as error events,
20
+ * linked to the session id it last reported with `setSession()`. Nothing leaves the device from here.
21
+ */
22
+ @CapacitorPlugin(name = "ReplaydNative")
23
+ class ReplaydNativePlugin : Plugin() {
24
+ override fun load() {
25
+ CrashStore.install(context.applicationContext)
26
+ }
27
+
28
+ @PluginMethod
29
+ fun setSession(call: PluginCall) {
30
+ val sid = call.getString("sid")
31
+ if (sid != null && sid.length <= 64) CrashStore.prefs(context).edit().putString(CrashStore.SID, sid).apply()
32
+ call.resolve()
33
+ }
34
+
35
+ @PluginMethod
36
+ fun pending(call: PluginCall) {
37
+ val out = JSArray()
38
+ try {
39
+ for (r in CrashStore.take(context)) out.put(r)
40
+ for (r in exitReasons(context)) out.put(r)
41
+ } catch (_: Throwable) {
42
+ // never fail the app's launch over a report
43
+ }
44
+ call.resolve(JSObject().put("reports", out))
45
+ }
46
+
47
+ /** ANRs and native crashes since the last call (API 30+), each reported once, ≤ 5. */
48
+ private fun exitReasons(ctx: Context): List<JSONObject> {
49
+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) return emptyList()
50
+ val am = ctx.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
51
+ val prefs = CrashStore.prefs(ctx)
52
+ val seen = prefs.getLong(CrashStore.EXIT_SEEN, 0L)
53
+ val list = am.getHistoricalProcessExitReasons(ctx.packageName, 0, 20)
54
+ .filter { it.timestamp > seen && (it.reason == ApplicationExitInfo.REASON_ANR || it.reason == ApplicationExitInfo.REASON_CRASH_NATIVE) }
55
+ .take(5)
56
+ if (list.isNotEmpty()) prefs.edit().putLong(CrashStore.EXIT_SEEN, list.maxOf { it.timestamp }).apply()
57
+ val sid = prefs.getString(CrashStore.SID, null)
58
+ return list.map { info ->
59
+ val anr = info.reason == ApplicationExitInfo.REASON_ANR
60
+ val trace = try {
61
+ info.traceInputStream?.use { s -> String(s.readBytes().take(CrashStore.MAX_BYTES).toByteArray()) }
62
+ } catch (_: Throwable) {
63
+ null
64
+ }
65
+ JSONObject()
66
+ .put("kind", if (anr) "anr" else "native")
67
+ .put("type", if (anr) "ANR" else "NativeCrash")
68
+ .put("message", info.description ?: if (anr) "Application Not Responding" else "Native crash")
69
+ .put("stack", if (anr) mainThread(trace ?: "") else "")
70
+ .put("at", info.timestamp)
71
+ .put("sid", sid)
72
+ .put("reason", info.description)
73
+ }
74
+ }
75
+
76
+ /** The main thread's section of an ANR trace, in `at …` form for grouping (≤ 64 frames). */
77
+ private fun mainThread(trace: String): String {
78
+ val start = trace.indexOf("\"main\"")
79
+ if (start < 0) return ""
80
+ val section = trace.substring(start).substringBefore("\n\n")
81
+ return section.lines()
82
+ .filter { it.trim().startsWith("at ") }
83
+ .take(64)
84
+ .joinToString("\n") { "\t" + it.trim() }
85
+ }
86
+ }
87
+
88
+ internal object CrashStore {
89
+ const val SID = "sid"
90
+ const val EXIT_SEEN = "exit_seen"
91
+ const val MAX_BYTES = 64 * 1024
92
+ private const val MAX_FILES = 5
93
+ @Volatile private var installed = false
94
+
95
+ fun prefs(ctx: Context) = ctx.getSharedPreferences("replayd", Context.MODE_PRIVATE)
96
+
97
+ private fun dir(ctx: Context) = File(ctx.filesDir, "replayd-crashes").apply { mkdirs() }
98
+
99
+ fun install(ctx: Context) {
100
+ if (installed) return
101
+ installed = true
102
+ val previous = Thread.getDefaultUncaughtExceptionHandler()
103
+ Thread.setDefaultUncaughtExceptionHandler { thread, e ->
104
+ try {
105
+ write(ctx, thread, e)
106
+ } catch (_: Throwable) {
107
+ // the app is crashing anyway: never throw from here
108
+ }
109
+ previous?.uncaughtException(thread, e)
110
+ }
111
+ }
112
+
113
+ private fun write(ctx: Context, thread: Thread, e: Throwable) {
114
+ val d = dir(ctx)
115
+ val files = d.listFiles()?.sortedBy { it.name } ?: emptyList()
116
+ if (files.size >= MAX_FILES) return
117
+ val stack = e.stackTraceToString().take(MAX_BYTES)
118
+ val o = JSONObject()
119
+ .put("kind", "crash")
120
+ .put("type", e.javaClass.name)
121
+ .put("message", (e.message ?: "").take(1000))
122
+ .put("stack", stack)
123
+ .put("at", System.currentTimeMillis())
124
+ .put("sid", prefs(ctx).getString(SID, null))
125
+ .put("thread", thread.name.take(100))
126
+ File(d, "crash-${System.currentTimeMillis()}.json").writeText(o.toString())
127
+ }
128
+
129
+ /** The kept crashes, oldest first; deleted once read. */
130
+ fun take(ctx: Context): List<JSONObject> {
131
+ val files = dir(ctx).listFiles()?.sortedBy { it.name } ?: return emptyList()
132
+ return files.mapNotNull { f ->
133
+ val o = try {
134
+ JSONObject(f.readText())
135
+ } catch (_: Throwable) {
136
+ null
137
+ }
138
+ f.delete()
139
+ o
140
+ }
141
+ }
142
+ }
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ var e=()=>globalThis.Capacitor?.Plugins?.ReplaydNative;export{e as nativePlugin};
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/index.ts"],
4
+ "sourcesContent": ["/**\n * @replayd/capacitor-android (T-154): the native half lives in `android/` (a Capacitor plugin named `ReplaydNative`).\n * Nothing to call from the app: the SDK's Capacitor chunk finds `Capacitor.Plugins.ReplaydNative`, tells it the\n * current session id and, on every launch, sends the crashes and ANRs it kept as error events (`source: 'native'`).\n * This module only types the plugin for apps that want to query it themselves.\n */\nexport interface NativeReport {\n /** `crash` (uncaught Java/Kotlin exception), `anr` or `native` (crash in native code, API 30+) */\n kind: 'crash' | 'anr' | 'native';\n /** exception class, or `ANR` / `NativeCrash` */\n type: string;\n message: string;\n /** `Throwable.stackTraceToString()` (crash) or the main thread of the ANR trace (\u2264 64 KB) */\n stack: string;\n /** ms since epoch */\n at: number;\n /** replayd session id at the time (the last one the SDK reported) */\n sid?: string;\n thread?: string;\n /** ApplicationExitInfo description (API 30+) */\n reason?: string;\n}\n\nexport interface ReplaydNativePlugin {\n setSession(o: { sid: string }): Promise<void>;\n /** the reports kept since the last call (\u2264 5 crashes + \u2264 5 exit reasons); each is returned once */\n pending(): Promise<{ reports: NativeReport[] }>;\n}\n\n/** the registered native plugin, when the app runs under Capacitor with this package installed */\nexport const nativePlugin = (): ReplaydNativePlugin | undefined =>\n (globalThis as { Capacitor?: { Plugins?: Record<string, unknown> } }).Capacitor?.Plugins?.ReplaydNative as ReplaydNativePlugin | undefined;\n"],
5
+ "mappings": "AA8BO,IAAMA,EAAe,IACzB,WAAqE,WAAW,SAAS",
6
+ "names": ["nativePlugin"]
7
+ }
@@ -0,0 +1,33 @@
1
+ /**
2
+ * @replayd/capacitor-android (T-154): the native half lives in `android/` (a Capacitor plugin named `ReplaydNative`).
3
+ * Nothing to call from the app: the SDK's Capacitor chunk finds `Capacitor.Plugins.ReplaydNative`, tells it the
4
+ * current session id and, on every launch, sends the crashes and ANRs it kept as error events (`source: 'native'`).
5
+ * This module only types the plugin for apps that want to query it themselves.
6
+ */
7
+ export interface NativeReport {
8
+ /** `crash` (uncaught Java/Kotlin exception), `anr` or `native` (crash in native code, API 30+) */
9
+ kind: 'crash' | 'anr' | 'native';
10
+ /** exception class, or `ANR` / `NativeCrash` */
11
+ type: string;
12
+ message: string;
13
+ /** `Throwable.stackTraceToString()` (crash) or the main thread of the ANR trace (≤ 64 KB) */
14
+ stack: string;
15
+ /** ms since epoch */
16
+ at: number;
17
+ /** replayd session id at the time (the last one the SDK reported) */
18
+ sid?: string;
19
+ thread?: string;
20
+ /** ApplicationExitInfo description (API 30+) */
21
+ reason?: string;
22
+ }
23
+ export interface ReplaydNativePlugin {
24
+ setSession(o: {
25
+ sid: string;
26
+ }): Promise<void>;
27
+ /** the reports kept since the last call (≤ 5 crashes + ≤ 5 exit reasons); each is returned once */
28
+ pending(): Promise<{
29
+ reports: NativeReport[];
30
+ }>;
31
+ }
32
+ /** the registered native plugin, when the app runs under Capacitor with this package installed */
33
+ export declare const nativePlugin: () => ReplaydNativePlugin | undefined;
package/package.json CHANGED
@@ -1,6 +1,37 @@
1
1
  {
2
2
  "name": "@replayd/capacitor-android",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.4",
4
+ "description": "Capacitor Android plugin for replayd: native (Java/Kotlin) crashes and ANRs, reported on the next launch",
5
+ "type": "module",
6
+ "sideEffects": false,
7
+ "license": "UNLICENSED",
8
+ "files": [
9
+ "dist",
10
+ "android/src",
11
+ "android/build.gradle"
12
+ ],
13
+ "publishConfig": {
14
+ "access": "public"
15
+ },
16
+ "exports": {
17
+ ".": {
18
+ "types": "./dist/types/index.d.ts",
19
+ "import": "./dist/index.js"
20
+ }
21
+ },
22
+ "capacitor": {
23
+ "android": {
24
+ "src": "android"
25
+ }
26
+ },
27
+ "peerDependencies": {
28
+ "@replayd/core": "^0.1.4"
29
+ },
30
+ "devDependencies": {
31
+ "@replayd/core": "0.1.4"
32
+ },
33
+ "gitHead": "4f56f61ca8ca290a1a6e2dc38bbef707b282d4c0",
34
+ "scripts": {
35
+ "build": "node ../../scripts/build.mjs build.json && tsc -p tsconfig.build.json"
36
+ }
6
37
  }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.