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

Getting "android context not initialized" error after upgrade to v2.0.0-dev.33 #1946

Closed
tmpfs opened this issue May 16, 2024 · 6 comments
Closed
Labels
awaiting Waiting for responses, PR, further discussions, upstream release, etc bug Something isn't working

Comments

@tmpfs
Copy link
Contributor

tmpfs commented May 16, 2024

Describe the bug

After upgrading to v2 I get this error reported in my crashlog from automated execution against several devices in the Play Store, here is an example crash report:

{
  "kind": "Application",
  "message": "android context was not initialized",
  "app": {
    "version": "1.0.0-beta",
    "buildNumber": "102",
    "isReleaseBuild": true,
    "isTestBuild": false
  },
  "device": {
    "board": "cutf",
    "bootloader": "unknown",
    "brand": "OnePlus",
    "device": "OnePlus8Pro",
    "hardware": "sunfish",
    "host": "ubuntu-10",
    "isLowRamDevice": false,
    "isPhysicalDevice": true,
    "manufacturer": "OnePlus",
    "model": "OnePlus8Pro",
    "platform": "android",
    "product": "IN2023",
    "supported32BitAbis": [
      "x86",
      "armeabi-v7a",
      "armeabi"
    ],
    "supported64BitAbis": [
      "x86_64",
      "arm64-v8a"
    ],
    "supportedAbis": [
      "x86_64",
      "arm64-v8a",
      "x86",
      "armeabi-v7a",
      "armeabi"
    ],
    "tags": "release-keys",
    "type": "user",
    "version": {
      "baseOS": "",
      "codename": "REL",
      "incremental": "2006701342",
      "previewSdkInt": 0,
      "release": "11",
      "sdkInt": 30,
      "securityPatch": "2021-07-16"
    }
  },
  "stack": "#0      SimpleDecoder.decode (package:flutter_rust_bridge/src/codec/base.dart:35)\n#1      SseCodec._decode (package:flutter_rust_bridge/src/codec/sse.dart:42)\n#2      SseCodec.decodeWireSyncType (package:flutter_rust_bridge/src/codec/sse.dart:37)\n#3      BaseHandler.executeSync (package:flutter_rust_bridge/src/main_components/handler.dart:34)\n#4      RustLibApiImpl.applicationNew (package:saveoursecrets/ffi/frb_generated.dart:734)\n#5      new Application (package:saveoursecrets/ffi/api/application.dart:55)\n#6      new AppModel (package:saveoursecrets/model/app.dart:250)\n#7      prepareAppProvider (package:saveoursecrets/main.dart:42)\n<asynchronous suspension>\n#8      main (package:saveoursecrets/main.dart:10)\n<asynchronous suspension>\n",
  "logs": []
}

This is the device info:

{
  "board": "cutf",
  "bootloader": "unknown",
  "brand": "OnePlus",
  "device": "OnePlus8Pro",
  "hardware": "sunfish",
  "host": "ubuntu-10",
  "isLowRamDevice": false,
  "isPhysicalDevice": true,
  "manufacturer": "OnePlus",
  "model": "OnePlus8Pro",
  "platform": "android",
  "product": "IN2023",
  "supported32BitAbis": [
    "x86",
    "armeabi-v7a",
    "armeabi"
  ],
  "supported64BitAbis": [
    "x86_64",
    "arm64-v8a"
  ],
  "supportedAbis": [
    "x86_64",
    "arm64-v8a",
    "x86",
    "armeabi-v7a",
    "armeabi"
  ],
  "tags": "release-keys",
  "type": "user",
  "version": {
    "baseOS": "",
    "codename": "REL",
    "incremental": "2006701342",
    "previewSdkInt": 0,
    "release": "11",
    "sdkInt": 30,
    "securityPatch": "2021-07-16"
  }
}

Steps to reproduce

Not sure how to reproduce this as it worked fine when I tested on my local device.

Logs

N/A

Expected behavior

I expected the app to run as it did with v1.82.6.

Generated binding code

No response

OS

No response

Version of flutter_rust_bridge_codegen

v2.0.0-dev.33

Flutter info

No response

Version of clang++

No response

Additional context

I have looked at the information in #1323 and the linked PR but not sure I need to follow these steps as a vanilla app generated with v2 flutter_rust_bridge_codegen does not seem to include that code.

Any pointers much appreciated 🙏

@tmpfs tmpfs added the bug Something isn't working label May 16, 2024
@fzyzcjy
Copy link
Owner

fzyzcjy commented May 16, 2024

Hmm, so firstly, do you use any special things in Rust? e.g. something like android context.

In addition, try to print as many logs as possible for that automated testing, which can be quite helpful.

Also, do they provide some extra info such as full video recordings?

@fzyzcjy fzyzcjy added the awaiting Waiting for responses, PR, further discussions, upstream release, etc label May 16, 2024
@tmpfs
Copy link
Contributor Author

tmpfs commented May 16, 2024

Hi @fzyzcjy ,

I don't load any dynamic libraries any more after the upgrade (with v1 i loaded the bindings .so), just use normal Flutter packages/libraries and nothing particularly fancy.

I just manged to reproduce this on a local device:

I/flutter ( 2345): ══╡ EXCEPTION CAUGHT BY FLUTTER FRAMEWORK ╞═════════════════════════════════════════════════════════
I/flutter ( 2345): The following PanicException was thrown:
I/flutter ( 2345): PanicException(android context was not initialized)
I/flutter ( 2345):
I/flutter ( 2345): When the exception was thrown, this was the stack:
I/flutter ( 2345): #0      SimpleDecoder.decode (package:flutter_rust_bridge/src/codec/base.dart:35:9)
I/flutter ( 2345): #1      SseCodec._decode (package:flutter_rust_bridge/src/codec/sse.dart:42:55)
I/flutter ( 2345): #2      SseCodec.decodeWireSyncType (package:flutter_rust_bridge/src/codec/sse.dart:37:7)
I/flutter ( 2345): #3      BaseHandler.executeSync (package:flutter_rust_bridge/src/main_components/handler.dart:34:25)
I/flutter ( 2345): #4      RustLibApiImpl.applicationNew (package:saveoursecrets/ffi/frb_generated.dart:734:20)
I/flutter ( 2345): #5      new Application (package:saveoursecrets/ffi/api/application.dart:55:28)
I/flutter ( 2345): #6      new AppModel (package:saveoursecrets/model/app.dart:250:27)
I/flutter ( 2345): #7      prepareAppProvider (package:saveoursecrets/main.dart:42:17)
I/flutter ( 2345): <asynchronous suspension>
I/flutter ( 2345): #8      main (package:saveoursecrets/main.dart:10:15)
I/flutter ( 2345): <asynchronous suspension>
I/flutter ( 2345): ════════════════════════════════════════════════════════════════════════════════════════════════════

Video recording won't help as this is when the application first boots. The stack trace is from trying to create an Application
instance on the Rust side when I prepare/boot the app.

Perhaps there is some Android specific configuration lingering from the v1 build process that is causing this, will keep looking into it.

BTW, huge thanks to you and all the other contributors for FRB v2, it is sooooo much better having the build process just be another Flutter package and the generated bindings are much easier to read and reason about. Awesome work!

@fzyzcjy
Copy link
Owner

fzyzcjy commented May 16, 2024

I just manged to reproduce this on a local device

Great!

Btw you can setup stacktrace logging, such that we can know which exact line of Rust code causes it.

I don't load any dynamic libraries any more after the upgrade

I mean, do you use some JNI function or something that indirectly needs android?

Perhaps there is some Android specific configuration lingering from the v1 build process that is causing this, will keep looking into it.

If this may be the problem, one way is to create a brand new project by flutter_rust_bridge_codegen create, and copy-paste your old code to it.

@tmpfs
Copy link
Contributor Author

tmpfs commented May 17, 2024

Huh, so this was a little convoluted to figure out but I got it working.

TL;DR needed to avoid using the app_dirs2 crate functionality on Android (which makes a JNI call) whilst main was executing.

During the upgrade I refactored from a global static Logger to an Application that wrapped a Logger - the logger by default would call out to app_dirs2 to determine the application directory to store log files.

I then instantiated the Application as an instance variable on the model final app = Application() (which would call out over FFI and create the Logger - making the JNI call before the android context was available). The model (and thus Application) instantiated in main were executing before the android context is available as the MainActivity hadn't finished launching at that point.

To work around this issue I deferred instantiating the Application to later and avoided calling app_dirs2 methods whilst the application was booting.

Apologies for the noise @fzyzcjy and thanks again for all your work on the excellent v2 of FRB 👏

@tmpfs tmpfs closed this as completed May 17, 2024
@fzyzcjy
Copy link
Owner

fzyzcjy commented May 17, 2024

It's OK and happy to see it is solved!

Copy link
Contributor

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 31, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
awaiting Waiting for responses, PR, further discussions, upstream release, etc bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants