Skip to content
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

Crash on Android #3

Closed
unsuitable001 opened this issue Apr 18, 2021 · 13 comments · Fixed by #9
Closed

Crash on Android #3

unsuitable001 opened this issue Apr 18, 2021 · 13 comments · Fixed by #9

Comments

@unsuitable001
Copy link
Owner

unsuitable001 commented Apr 18, 2021

I get a crash whenever I try to start the cronet engine. I can get the version string without any issues though.
The log says:

F/chromium(17045): [0418/210141.455792:FATAL:jni_android.cc(96)] Check failed: g_jvm. 
F/chromium(17045): #00 pc 0x00000000004a95a7 /data/app/com.example.cronet_flutter-cwon0ynfTWJ2X0VskS1H_A==/lib/arm64/libcronet.91.0.4456.0.so
F/chromium(17045): #01 pc 0x00000000004b6a3b /data/app/com.example.cronet_flutter-cwon0ynfTWJ2X0VskS1H_A==/lib/arm64/libcronet.91.0.4456.0.so
F/chromium(17045): #02 pc 0x00000000004b6fbf /data/app/com.example.cronet_flutter-cwon0ynfTWJ2X0VskS1H_A==/lib/arm64/libcronet.91.0.4456.0.so
F/chromium(17045): #03 pc 0x0000000000530acf /data/app/com.example.cronet_flutter-cwon0ynfTWJ2X0VskS1H_A==/lib/arm64/libcronet.91.0.4456.0.so
F/chromium(17045): #04 pc 0x0000000000306eeb /data/app/com.example.cronet_flutter-cwon0ynfTWJ2X0VskS1H_A==/lib/arm64/libcronet.91.0.4456.0.so
F/chromium(17045): #05 pc 0x000000000047b3eb /data/app/com.example.cronet_flutter-cwon0ynfTWJ2X0VskS1H_A==/lib/arm64/libcronet.91.0.4456.0.so
F/chromium(17045): #06 pc 0x0000000000483e63 /data/app/com.example.cronet_flutter-cwon0ynfTWJ2X0VskS1H_A==/lib/arm64/libcronet.91.0.4456.0.so
F/chromium(17045): #07 pc 0x0000000000062f63 /data/app/com.example.cronet_flutter-cwon0ynfTWJ2X0VskS1H_A==/lib/arm64/libwrapper_android.so
F/chromium(17045): #08 pc 0x0000007de1f06697 <unknown>                  
F/chromium(17045):                                                      
F/libc    (17045): Fatal signal 6 (SIGABRT), code -6 (SI_TKILL) in tid 17071 (1.ui), pid 17045 (.cronet_flutter)
*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***         
Build fingerprint: 'asus/WW_X00TD/ASUS_X00T_2:9/PKQ1/16.2017.2009.087-20200826:user/release-keys'
Revision: '0'                                                           
ABI: 'arm64'                                                            
pid: 17045, tid: 17071, name: 1.ui  >>> com.example.cronet_flutter <<<  
signal 6 (SIGABRT), code -6 (SI_TKILL), fault addr --------             
Abort message: '[0418/210141.455792:FATAL:jni_android.cc(96)] Check failed: g_jvm. '
    x0  0000000000000000  x1  00000000000042af  x2  0000000000000006  x3  0000000000000008
    x4  0000000000000020  x5  0000000000000020  x6  0000000000000020  x7  0000007df877da80
    x8  0000000000000083  x9  fb934cb33f807cfa  x10 0000000000000000  x11 fffffffc7ffffbdf
    x12 0000000000000001  x13 0000000000000009  x14 0000000000000000  x15 00007c270a5489ec
    x16 0000007e80ca42b8  x17 0000007e80bc5a50  x18 0000000000000010  x19 0000000000004295
    x20 00000000000042af  x21 0000000000000043  x22 0000007dda6ccfd8  x23 0000007e80ca63c0
    x24 0000007dd7912bb3  x25 0000007dd7ff1000  x26 0000007de3be0588  x27 0000007dda7d8288
    x28 0000007dda7d8288  x29 0000007de3bde830                          
    sp  0000007de3bde7f0  lr  0000007e80bb7084  pc  0000007e80bb70ac    
backtrace:                                                              
    #00 pc 00000000000220ac  /system/lib64/libc.so (abort+116)          
    #01 pc 000000000052310c  /data/app/com.example.cronet_flutter-cwon0ynfTWJ2X0VskS1H_A==/lib/arm64/libcronet.91.0.4456.0.so (base::debug::BreakDebugger()+40)
    #02 pc 00000000004b6cf0  /data/app/com.example.cronet_flutter-cwon0ynfTWJ2X0VskS1H_A==/lib/arm64/libcronet.91.0.4456.0.so (logging::LogMessage::~LogMessage()+796)
    #03 pc 00000000004b6fbc  /data/app/com.example.cronet_flutter-cwon0ynfTWJ2X0VskS1H_A==/lib/arm64/libcronet.91.0.4456.0.so (logging::LogMessage::~LogMessage()+16)
    #04 pc 0000000000530acc  /data/app/com.example.cronet_flutter-cwon0ynfTWJ2X0VskS1H_A==/lib/arm64/libcronet.91.0.4456.0.so (base::android::AttachCurrentThread()+84)
    #05 pc 0000000000306ee8  /data/app/com.example.cronet_flutter-cwon0ynfTWJ2X0VskS1H_A==/lib/arm64/libcronet.91.0.4456.0.so (cronet::EnsureInitialized()+108)
    #06 pc 000000000047b3e8  /data/app/com.example.cronet_flutter-cwon0ynfTWJ2X0VskS1H_A==/lib/arm64/libcronet.91.0.4456.0.so (cronet::Cronet_EngineImpl::StartWithParams(Cronet_EngineParams*)+52)
    #07 pc 0000000000483e60  /data/app/com.example.cronet_flutter-cwon0ynfTWJ2X0VskS1H_A==/lib/arm64/libcronet.91.0.4456.0.so (Cronet_Engine_StartWithParams+88)
    #08 pc 0000000000062f60  /data/app/com.example.cronet_flutter-cwon0ynfTWJ2X0VskS1H_A==/lib/arm64/libwrapper_android.so (Cronet_Engine_StartWithParams+40)
    #09 pc 0000000000006694  <anonymous:0000007de1f00000> 
@mannprerak2
Copy link

Is this code to reproduce this available on github?

@unsuitable001
Copy link
Owner Author

Is this code to reproduce this available on github?

@mannprerak2 Yes. The main code is the same. But, there are some more work to do to reproduce it properly.

Steps to reproduce

Build cronet for android

Compile the wrapper for android

  1. Create CMakeLists.txt file
cmake_minimum_required(VERSION 3.16)  # for example
project(cronet_flutter)
add_library( wrapper_android

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             "../wrapper.cc"
             "../wrapper.h"
             "../sample_executor.cc"
             "../sample_executor.h"
             "/usr/lib/dart/include/dart_api_dl.c"
)

include_directories(/usr/lib/dart/include/)

add_compile_options(-fPIC -ldl -rdynamic -DDART_SHARED_LIB -fpermissive)

Adjust the paths accordingly

  1. Generate ninja build files
cmake -H. -Bgenerated/ninja/project/debug/arm-64-v8a -DANDROID_ABI=arm64-v8a -DANDROID_PLATFORM=android-26 -DANDROID_NDK=~/Android/Sdk/ndk/22.1.7171670 -DCMAKE_TOOLCHAIN_FILE=~/Android/Sdk/ndk/22.1.7171670/build/cmake/android.toolchain.cmake -DANDROID_TOOLCHAIN=clang3.6 -G Ninja
  1. Build using ninja
cd generated/ninja/project/debug/arm-64-v8a
ninja

There you will get the libwrapper_android.so file.
Note: Either change the .so file names to the same as in dart files. Or, change the name in the dart file

Flutter

  1. Create a flutter project, add this package in pubspec.
  2. Go to the android/app/src/main/jniLibs/arm64-v8a directory from your flutter project's root directory, and paste libcronet and wrapper shared libs there. (You may have to create jniLibs and subsequent sub-dirs if they do not exist)
  3. Do not forget to add internet permissions in android manifest file
  4. Use the library there. First you can just import the library and just get the version string, and use that in a text widget. It works fine. But, whenever we try to init engine with params, (I did that in a stateful widget's initState) it will crash.

Now all set. You can do flutter run to reproduce the bug

Also attaching my cronet build here. Just in case if you don't want to build yourself.

libcronet.91.0.4456.0.zip

@mannprerak2
Copy link

mannprerak2 commented Apr 20, 2021

That quite a lot :p, I think you can wrap all this in a package.

Take a loot at this -> https://github.com/Sunbreak/cronet_flutter. This works out of the box.

Edit: Maybe you can use this as a starting point, put your wrapper code here and see if a similar error occurs?

@mannprerak2
Copy link

mannprerak2 commented Apr 20, 2021

@unsuitable001
Copy link
Owner Author

It is a lot :/ but, most of the work is building the wrapper actually. As I was testing, I haven't put anything extra in gradle file (I'm yet to be familiar with this, that's why :/ ). If we use gradle build file, most of the task will be automated for us though.

Take a loot at this -> https://github.com/Sunbreak/cronet_flutter. This works out of the box.

Okay. I'll check this out.

@unsuitable001
Copy link
Owner Author

And, I found this: Sunbreak/cronet_flutter#8
The exact same problem I'm facing. And, as per the patch here, I think my speculations are correct. I'll update you after fixing.

@unsuitable001
Copy link
Owner Author

One more question I need to ask. This package is a Dart Package and not a Flutter Package, so that we can use it with Dart CLI and non-flutter scenarios. So, we can't use flutter as a dependency. Without using it, can we integrate platform specific code to our plugin in a well organized manner? (Like Sunbreak did).
If not, then we can rely upon some shell script , though that don't look good.

So, is there any way that we can add platform specific code and also maintain compatibility with Dart CLI? @mannprerak2 @dcharkes

Context: For testing this package on flutter, I'm just importing this library and adding all the platform specific code on the main (example) project directly. In that process, I guess I'm missing something. That causes this error: The plugin cronet_flutter doesn't have a main class defined in <some_long_path_here>/com/example/cronet_flutter/CronetFlutterPlugin.kt

Note: I have MainActivity.kt file in the same location as CronetFlutterPlugin.kt and adding CronetFlutterPlugin as plugin in pubspec. Ideally, CronetFlutterPlugin.kt should reside in the package's platform specific code's dir and not in project's. I'm yet to discover how flutter merges them at the build time. I'll let you know after I figure it out.

@mannprerak2
Copy link

So, is there any way that we can add platform specific code and also maintain compatibility with Dart CLI?

I suppose we can split the package into 2, maybe something like cronet and flutter_cronet.

@unsuitable001
Copy link
Owner Author

So, is there any way that we can add platform specific code and also maintain compatibility with Dart CLI?

I suppose we can split the package into 2, maybe something like cronet and flutter_cronet.

If I'm allowed to do that, then I'll be pushing the changes today (expected). Separating will solve many of the confusions I have now. :p

@dcharkes
Copy link

https://github.com/google/webcrypto.dart is set up as both dart standalone package and flutter plugin.

@unsuitable001
Copy link
Owner Author

https://github.com/google/webcrypto.dart is set up as both dart standalone package and flutter plugin.

Wow! I'll surely look into that.

For testing, I created a separate flutter plugin and used this repo as dependency. I'm getting the version string properly and also NOT getting the error in this issue.

But, sadly, I'm not getting any data also. I'm still missing something. I'll update as I get some more info.

unsuitable001 added a commit that referenced this issue Apr 22, 2021
Added android cronet lib path

Reference: Issue #3
@unsuitable001
Copy link
Owner Author

unsuitable001 commented Apr 22, 2021

Package is working now in android. Repo: https://github.com/unsuitable001/flutter_cronet_sample (only arm64 build is provided. for other devices, have to build cronet from scratch)

But, there are some interesting bugs in the flutter solution. (Though, they aren't impacting the usability. I mean, results produced are error free). You may look into those issues there.

Edit: Issues have been fixed. One interesting thing I encountered while checking webcrypto is, it is using flutter as dependency (and, in environment of course). I can't find any difference between a regular flutter plugin's pubspec file and webcrypto's. That makes me wonder, how it is a standalone package... I can use it without flutter for sure. But, I still have to install flutter, right?

@unsuitable001
Copy link
Owner Author

unsuitable001 commented May 12, 2021

Closing this issue as it has been fixed (but, the patch is in seperate repo as a seperate package). When I merge that repo to this, I'll refer this issue.

Flutter package with Android support: https://github.com/unsuitable001/flutter_cronet_sample

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants