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

JNI thorws StateError when execute CameraX initializationFuture.await #1849

Open
yanshouwang opened this issue Dec 31, 2024 · 1 comment
Open

Comments

@yanshouwang
Copy link

The CameraX library's CameraController class has a getInitializationFuture() method.

I wrote a wrapper of this method and generate binding method with jnigen.

# Run with `dart run jnigen --config jnigen.yaml`.
android_sdk_config:
  add_gradle_deps: true
  android_example: example/
suspend_fun_to_async: true
log_level: all
output:
  dart:
    path: lib/src/jni/
class_path:
  - classes/jetified-listenablefuture-1.0.jar
classes:
  - android.app.Activity
  - android.content.Context
  - androidx.lifecycle.LifecycleOwner
  - androidx.lifecycle.LiveData
  - androidx.lifecycle.Observer
  - dev.hebei.camerax_android.core.CameraSelector
  - dev.hebei.camerax_android.core.PermissionManager
  - dev.hebei.camerax_android.core.ZoomState
  - dev.hebei.camerax_android.view.CameraController
  - dev.hebei.camerax_android.view.LifecycleCameraController
  - dev.hebei.camerax_android.view.PreviewView
abstract class CameraController {
    internal abstract val obj: androidx.camera.view.CameraController

    suspend fun initialize() {
        obj.initializationFuture.await()
    }
...
}
final class CameraControllerImpl {
  final jni.LifecycleCameraController obj;
...
  @override
  Future<void> initialize() async {
    await obj.initialize();
  }
...
}

The app throws StateError when calling this method.

    try {
      await controller.initialize();
      await controller.setCameraSelector(CameraSelector.back);
      await bind();
    } catch (e, stack) {
      logger.severe(e, stack);
    }
[HomeViewModel] Bad state: The reference was null
[HomeViewModel] #0      new JObject.fromReference (package:jni/src/jobject.dart:105:7)
                #1      JObjectType.fromReference (package:jni/src/jobject.dart:61:15)
jobject.dart:61
                #2      CameraController.initialize (package:camerax_android/src/jni/dev/hebei/camerax_android/view/CameraController.dart:96:38)
CameraController.dart:96
                <asynchronous suspension>
                #3      CameraControllerImpl.initialize (package:camerax_android/src/view/camera_controller_impl.dart:41:5)
camera_controller_impl.dart:41
                <asynchronous suspension>
                #4      HomeViewModel._initialize (package:camerax_android_example/view_models/home_view_model.dart:216:7)
home_view_model.dart:216
                <asynchronous suspension>
@yanshouwang
Copy link
Author

yanshouwang commented Dec 31, 2024

I think jnigen has a bug when generating return value of Void type.

public final class Void {
    public static final Class<Void> TYPE = null;

    Void() {
        throw new RuntimeException("Stub!");
    }
}

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

No branches or pull requests

1 participant