如果寫過JNI就會知道這是一層地獄層,不論在開發或者使用都會有許多問題,需要多透過一層JNI進行溝通,要寫Java的人去寫C++更是一種折磨,當然未來似乎Kotlin可以直接寫Kotlin Native,但個人還沒實際去測試實用性
而寫Native問題應該就是,debug困難,crash後很難去找到問題是什麼,所以常常就只能在有限的資訊中找到有限的資源進行猜測除錯或者開發,非常的麻煩
而Android Studio 4.1之後開始支援可以把native 的debug symbol包含在aab中,所以如果是使用aab的人也不用擔心了,他會自動包裝在aab上傳到google play console ,使用者crash後也可以解譯出正確的crash位置與行數,但? 有兩種人會無法使用這樣的功能:
- 使用APK build的人,由於apk沒有辦法包含非dex等資訊以外的東西,所以自然也無法把混淆或者其他資訊附加在上面
- 如果你是在另一個project build你的native so檔案,然後再複製到你另一個正式的產品app中,那因為你本身不含debug symbol,所以包裝成aab時候自然也無法有debug symobl( 不過個人猜測如果我在build naitve so的時候只要build RelWithDebugSymol的話,結合gradle release remove debug symbol的功能,可以在產出去除symbol檔案,並且讓aab包裝的時候留下debug symbol的功能,但這需要待測試就是了)
那遇到這個問題的人該怎麼辦呢?
還好官方還有提供手動上傳debug symbol的方法
The Google Play Console reports native crashes under Android vitals. With a few steps, you can generate and upload a native debug symbols file for your app. This file enables symbolicated native crash stack traces (that include class and function names) in Android vitals to help you debug your app in production. These steps vary depending on the version of the Android Gradle plugin used in your project and the build output of your project.
不過要手動當然就是麻煩許多了
而如果要自動的話先介紹,可以透過 android studio 4.1的 版本讓他自動包含在aab中
android.buildTypes.release.ndk.debugSymbolLevel = { SYMBOL_TABLE | FULL }
兩個差別在function name 還有line number ,symbol_table是有line number 的
另外要注意的是
Note: There is a 300 MB limit for the native debug symbols file. If your debug symbols footprint is too large, use SYMBOL_TABLE instead of FULL to decrease the file size.
那要怎麼上傳呢?到Google Play Console 的探索工具中可以找到

下面有一個元素,他就會解析你的aab中含有什麼資訊,可以看到我這邊的是沒有原生除錯符號的,並且有一個上傳檔案的符號,只要你拿到你的上傳符號,就可以在這邊進行上傳
如此以後你在jni crash 後就可以看到symbol 或是line number了
官方說明
其他相關
https://source.android.com/devices/tech/debug?hl=en
https://source.android.com/devices/tech/debug/native-crash?hl=en