-
Notifications
You must be signed in to change notification settings - Fork 2
react-native: android crash handler upgrade #301
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
konraddysput
merged 5 commits into
main
from
improvement/react-native-android-crash-handler-upgrade
Oct 23, 2024
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bbca313
Upgrade crash handler to support the latest AGP versions
konraddysput a23e335
react-native: removed log
konraddysput 92fb752
react-native: Fixed invalid reference to types not available in the p…
konraddysput 467e4ad
react-native: use different method to generate native database path
konraddysput eb370b5
Java Crash handler description
konraddysput File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
packages/react-native/android/src/main/java/backtrace/library/common/AbiHelper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package backtraceio.library.common; | ||
|
||
import android.os.Build; | ||
|
||
public class AbiHelper { | ||
public static String getCurrentAbi() { | ||
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) { | ||
// on newer Android versions, we'll return only the most important Abi version | ||
return Build.SUPPORTED_ABIS[0]; | ||
perf2711 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
// on pre-Lollip versions, we got only one Abi | ||
return Build.CPU_ABI; | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
...droid/src/main/java/backtrace/library/models/nativeHandler/CrashHandlerConfiguration.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package backtraceio.library.models.nativeHandler; | ||
|
||
import android.content.pm.ApplicationInfo; | ||
import android.text.TextUtils; | ||
|
||
import java.io.File; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import backtraceio.library.common.AbiHelper; | ||
import backtraceio.library.services.BacktraceCrashHandlerRunner; | ||
|
||
public class CrashHandlerConfiguration { | ||
|
||
public static final String BACKTRACE_CRASH_HANDLER = "BACKTRACE_CRASH_HANDLER"; | ||
public static final Set<String> UNSUPPORTED_ABIS = new HashSet<String>(Arrays.asList(new String[]{"x86"})); | ||
private static final String CRASHPAD_DIRECTORY_PATH = "/crashpad"; | ||
|
||
private static final String BACKTRACE_NATIVE_LIBRARY_NAME = "libbacktrace-native.so"; | ||
|
||
|
||
public Boolean isSupportedAbi() { | ||
return isSupportedAbi(AbiHelper.getCurrentAbi()); | ||
} | ||
|
||
public Boolean isSupportedAbi(String abi) { | ||
return !this.UNSUPPORTED_ABIS.contains(abi); | ||
} | ||
|
||
public String getClassPath() { | ||
return BacktraceCrashHandlerRunner.class.getCanonicalName(); | ||
} | ||
|
||
public List<String> getCrashHandlerEnvironmentVariables(ApplicationInfo applicationInfo) { | ||
return getCrashHandlerEnvironmentVariables(applicationInfo.sourceDir, applicationInfo.nativeLibraryDir, AbiHelper.getCurrentAbi()); | ||
} | ||
|
||
public List<String> getCrashHandlerEnvironmentVariables(String apkPath, String nativeLibraryDirPath, String arch) { | ||
final List<String> environmentVariables = new ArrayList<>(); | ||
|
||
// convert available in the system environment variables | ||
for (Map.Entry<String, String> variable : | ||
System.getenv().entrySet()) { | ||
environmentVariables.add(String.format("%s=%s", variable.getKey(), variable.getValue())); | ||
} | ||
// extend system-specific environment variables, with variables needed to properly run app_process via crashpad | ||
File nativeLibraryDirectory = new File(nativeLibraryDirPath); | ||
|
||
String backtraceNativeLibraryPath = getBacktraceNativeLibraryPath(nativeLibraryDirPath, apkPath, arch); | ||
File allNativeLibrariesDirectory = nativeLibraryDirectory.getParentFile(); | ||
String allPossibleLibrarySearchPaths = TextUtils.join(File.pathSeparator, new String[]{ | ||
nativeLibraryDirPath, | ||
allNativeLibrariesDirectory.getPath(), | ||
System.getProperty("java.library.path"), | ||
"/data/local"}); | ||
perf2711 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
environmentVariables.add(String.format("CLASSPATH=%s", apkPath)); | ||
environmentVariables.add(String.format("%s=%s", BACKTRACE_CRASH_HANDLER, backtraceNativeLibraryPath)); | ||
environmentVariables.add(String.format("LD_LIBRARY_PATH=%s", allPossibleLibrarySearchPaths)); | ||
environmentVariables.add("ANDROID_DATA=/data"); | ||
|
||
return environmentVariables; | ||
} | ||
|
||
public String useCrashpadDirectory(String databaseDirectory) { | ||
String databasePath = databaseDirectory + CRASHPAD_DIRECTORY_PATH; | ||
File crashHandlerDir = new File(databasePath); | ||
// Create the crashpad directory if it doesn't exist | ||
if (!crashHandlerDir.exists()) { | ||
crashHandlerDir.mkdir(); | ||
} | ||
perf2711 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return databasePath; | ||
} | ||
|
||
private String getBacktraceNativeLibraryPath(String nativeLibraryDirPath, String apkPath, String arch) { | ||
String backtraceNativeLibraryPath = String.format("%s/%s", nativeLibraryDirPath, BACKTRACE_NATIVE_LIBRARY_NAME); | ||
File backtraceNativeLibrary = new File(backtraceNativeLibraryPath); | ||
|
||
// If ndk libraries are already extracted, we shouldn't use libraries from the apk. | ||
// Otherwise. We need to find a path in the apk to use compressed libraries from there. | ||
return backtraceNativeLibrary.exists() | ||
? backtraceNativeLibraryPath | ||
: String.format("%s!/lib/%s/%s", apkPath, arch, BACKTRACE_NATIVE_LIBRARY_NAME); | ||
perf2711 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...act-native/android/src/main/java/backtrace/library/nativeCalls/BacktraceCrashHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package backtraceio.library.nativeCalls; | ||
|
||
public class BacktraceCrashHandler { | ||
public static native boolean handleCrash(String[] args); | ||
|
||
public static native boolean initializeJavaCrashHandler(String url, String databasePath, String classPath, String[] attributeKeys, String[] attributeValues, | ||
String[] attachmentPaths, String[] environmentVariables); | ||
|
||
public static native boolean initializeCrashHandler(String url, String databasePath, String handlerPath, | ||
String[] attributeKeys, String[] attributeValues, | ||
String[] attachmentPaths, boolean enableClientSideUnwinding, | ||
Integer unwindingMode); | ||
} |
19 changes: 19 additions & 0 deletions
19
...ive/android/src/main/java/backtrace/library/nativeCalls/BacktraceCrashHandlerWrapper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package backtraceio.library.nativeCalls; | ||
|
||
public class BacktraceCrashHandlerWrapper{ | ||
public boolean handleCrash(String[] args) { | ||
return BacktraceCrashHandler.handleCrash(args); | ||
} | ||
|
||
public boolean initializeJavaCrashHandler(String url, String databasePath, String classPath, String[] attributeKeys, String[] attributeValues, | ||
String[] attachmentPaths, String[] environmentVariables) { | ||
return BacktraceCrashHandler.initializeJavaCrashHandler(url, databasePath, classPath, attributeKeys, attributeValues, attachmentPaths, environmentVariables); | ||
} | ||
|
||
public boolean initializeCrashHandler(String url, String databasePath, String handlerPath, | ||
String[] attributeKeys, String[] attributeValues, | ||
String[] attachmentPaths, boolean enableClientSideUnwinding, | ||
Integer unwindingMode) { | ||
return BacktraceCrashHandler.initializeCrashHandler(url, databasePath, handlerPath, attributeKeys, attributeValues, attachmentPaths, enableClientSideUnwinding, unwindingMode); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
packages/react-native/android/src/main/java/backtrace/library/nativeCalls/SystemLoader.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package backtraceio.library.nativeCalls; | ||
|
||
public class SystemLoader { | ||
public void loadLibrary(String path) { | ||
System.load(path); | ||
} | ||
|
||
} |
54 changes: 54 additions & 0 deletions
54
...-native/android/src/main/java/backtrace/library/services/BacktraceCrashHandlerRunner.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package backtraceio.library.services; | ||
|
||
import android.util.Log; | ||
|
||
import java.util.Map; | ||
|
||
import backtraceio.library.models.nativeHandler.CrashHandlerConfiguration; | ||
import backtraceio.library.nativeCalls.BacktraceCrashHandlerWrapper; | ||
import backtraceio.library.nativeCalls.SystemLoader; | ||
|
||
public class BacktraceCrashHandlerRunner { | ||
private static final String LOG_TAG = BacktraceCrashHandlerRunner.class.getSimpleName(); | ||
private final BacktraceCrashHandlerWrapper crashHandler; | ||
private final SystemLoader loader; | ||
|
||
public static void main(String[] args) { | ||
BacktraceCrashHandlerRunner runner = new BacktraceCrashHandlerRunner(); | ||
runner.run(args, System.getenv()); | ||
} | ||
|
||
public BacktraceCrashHandlerRunner() { | ||
this(new BacktraceCrashHandlerWrapper(), new SystemLoader()); | ||
} | ||
|
||
public BacktraceCrashHandlerRunner(BacktraceCrashHandlerWrapper crashHandler, SystemLoader loader) { | ||
this.crashHandler = crashHandler; | ||
this.loader = loader; | ||
} | ||
|
||
public boolean run(String[] args, Map<String, String> environmentVariables) { | ||
if (environmentVariables == null) { | ||
Log.e(LOG_TAG, "Cannot capture crash dump. Environment variables are undefined"); | ||
return false; | ||
} | ||
|
||
String crashHandlerLibrary = environmentVariables.get(CrashHandlerConfiguration.BACKTRACE_CRASH_HANDLER); | ||
if (crashHandlerLibrary == null) { | ||
Log.e(LOG_TAG, String.format("Cannot capture crash dump. Cannot find %s environment variable", CrashHandlerConfiguration.BACKTRACE_CRASH_HANDLER)); | ||
return false; | ||
} | ||
|
||
|
||
loader.loadLibrary(crashHandlerLibrary); | ||
|
||
boolean result = crashHandler.handleCrash(args); | ||
if (!result) { | ||
Log.e(LOG_TAG, String.format("Cannot capture crash dump. Invocation parameters: %s", String.join(" ", args))); | ||
return false; | ||
} | ||
|
||
Log.i(LOG_TAG, "Successfully ran crash handler code."); | ||
return true; | ||
} | ||
} |
Binary file modified
BIN
+3.21 MB
(280%)
packages/react-native/android/src/main/jniLibs/arm64-v8a/libbacktrace-native.so
Binary file not shown.
Binary file modified
BIN
+0 Bytes
(100%)
packages/react-native/android/src/main/jniLibs/arm64-v8a/libcrashpad_handler.so
Binary file not shown.
Binary file modified
BIN
-3.02 KB
(99%)
packages/react-native/android/src/main/jniLibs/arm64-v8a/libnative-lib.so
Binary file not shown.
Binary file modified
BIN
+2.77 MB
(330%)
packages/react-native/android/src/main/jniLibs/armeabi-v7a/libbacktrace-native.so
Binary file not shown.
Binary file modified
BIN
-8 Bytes
(100%)
packages/react-native/android/src/main/jniLibs/armeabi-v7a/libcrashpad_handler.so
Binary file not shown.
Binary file modified
BIN
+4.46 KB
(100%)
packages/react-native/android/src/main/jniLibs/armeabi-v7a/libnative-lib.so
Binary file not shown.
Binary file modified
BIN
-16.8 KB
(95%)
packages/react-native/android/src/main/jniLibs/x86/libbacktrace-native.so
Binary file not shown.
Binary file modified
BIN
-18.1 KB
(95%)
packages/react-native/android/src/main/jniLibs/x86/libnative-lib.so
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Uh oh!
There was an error while loading. Please reload this page.