ibaiGorordo / depthai-android-jni-example

Android example to get the rgb and disparity images from the OAK-D device connected to a phone.
44 stars 8 forks source link

Running with Android 12 #3

Closed iwatake2222 closed 2 years ago

iwatake2222 commented 2 years ago

Thank you for sharing the great project ! I tried the project with Pixel 4a + Android 12 and found there are some issues. So I'd like to share my solutions (I'm not sure if the solution is the best and it may cause another problem in another environment. So I just post an issue here rather than sending a pull request). Hope it helps someone.

Issue 1

Detail

When I ran the project with Android 12, the following error happens: Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.

Log cat
``` 2022-02-27 15:54:08.023 23733-23733/? I/oid_jni_exampl: Late-enabling -Xcheck:jni 2022-02-27 15:54:08.051 23733-23733/? D/ProcessState: Binder ioctl to enable oneway spam detection failed: Invalid argument 2022-02-27 15:54:08.103 23733-23733/? D/CompatibilityChangeReporter: Compat change id reported: 171979766; UID 10424; state: ENABLED 2022-02-27 15:54:08.169 23733-23733/com.example.depthai_android_jni_example V/GraphicsEnvironment: ANGLE Developer option for 'com.example.depthai_android_jni_example' set to: 'default' 2022-02-27 15:54:08.169 23733-23733/com.example.depthai_android_jni_example V/GraphicsEnvironment: ANGLE GameManagerService for com.example.depthai_android_jni_example: false 2022-02-27 15:54:08.169 23733-23733/com.example.depthai_android_jni_example V/GraphicsEnvironment: Updatable production driver is not supported on the device. 2022-02-27 15:54:08.173 23733-23733/com.example.depthai_android_jni_example D/NetworkSecurityConfig: No Network Security Config specified, using platform default 2022-02-27 15:54:08.174 23733-23733/com.example.depthai_android_jni_example D/NetworkSecurityConfig: No Network Security Config specified, using platform default 2022-02-27 15:54:08.180 23733-23733/com.example.depthai_android_jni_example D/WM-WrkMgrInitializer: Initializing WorkManager with default configuration. 2022-02-27 15:54:08.383 23733-23775/com.example.depthai_android_jni_example D/CompatibilityChangeReporter: Compat change id reported: 160794467; UID 10424; state: ENABLED 2022-02-27 15:54:08.443 23733-23733/com.example.depthai_android_jni_example W/oid_jni_exampl: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (unsupported, reflection, allowed) 2022-02-27 15:54:08.444 23733-23733/com.example.depthai_android_jni_example W/oid_jni_exampl: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (unsupported, reflection, allowed) 2022-02-27 15:54:08.450 23733-23733/com.example.depthai_android_jni_example I/depthaiAndroid: libusb_set_option ANDROID_JAVAVM: Success 2022-02-27 15:54:08.446 23733-23733/com.example.depthai_android_jni_example W/oid_jni_example: type=1400 audit(0.0:35729): avc: denied { read } for name="usb" dev="tmpfs" ino=9016767 scontext=u:r:untrusted_app:s0:c168,c257,c512,c768 tcontext=u:object_r:usb_device:s0 tclass=dir permissive=0 app=com.example.depthai_android_jni_example 2022-02-27 15:54:08.446 23733-23733/com.example.depthai_android_jni_example W/oid_jni_example: type=1400 audit(0.0:35730): avc: denied { read } for name="/" dev="tmpfs" ino=25575 scontext=u:r:untrusted_app:s0:c168,c257,c512,c768 tcontext=u:object_r:device:s0 tclass=dir permissive=0 app=com.example.depthai_android_jni_example 2022-02-27 15:54:08.460 23733-23733/com.example.depthai_android_jni_example W/oid_jni_example: type=1400 audit(0.0:35731): avc: denied { search } for name="usb" dev="tmpfs" ino=9016767 scontext=u:r:untrusted_app:s0:c168,c257,c512,c768 tcontext=u:object_r:usb_device:s0 tclass=dir permissive=0 app=com.example.depthai_android_jni_example 2022-02-27 15:54:08.466 23733-23733/com.example.depthai_android_jni_example A/oid_jni_exampl: java_vm_ext.cc:579] JNI DETECTED ERROR IN APPLICATION: JNI CallVoidMethod called with pending exception java.lang.IllegalArgumentException: com.example.depthai_android_jni_example: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent. java_vm_ext.cc:579] Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles. java_vm_ext.cc:579] at void android.app.PendingIntent.checkFlags(int, java.lang.String) (PendingIntent.java:375) java_vm_ext.cc:579] at android.app.PendingIntent android.app.PendingIntent.getBroadcastAsUser(android.content.Context, int, android.content.Intent, int, android.os.UserHandle) (PendingIntent.java:645) java_vm_ext.cc:579] at android.app.PendingIntent android.app.PendingIntent.getBroadcast(android.content.Context, int, android.content.Intent, int) (PendingIntent.java:632) java_vm_ext.cc:579] at void com.example.depthai_android_jni_example.MainActivity.startDevice(java.lang.String, int, int) (MainActivity.java:-2) java_vm_ext.cc:579] at void com.example.depthai_android_jni_example.MainActivity$1.run() (MainActivity.java:75) java_vm_ext.cc:579] at void com.example.depthai_android_jni_example.MainActivity.onCreate(android.os.Bundle) (MainActivity.java:64) java_vm_ext.cc:579] at void android.app.Activity.performCreate(android.os.Bundle, android.os.PersistableBundle) (Activity.java:8051) java_vm_ext.cc:579] at void android.app.Activity.performCreate(android.os.Bundle) (Activity.java:8031) java_vm_ext.cc:579] at void android.app.Instrumentation.callActivityOnCreate(android.app.Activity, android.os.Bundle) (Instrumentation.java:1329) java_vm_ext.cc:579] at android.app.Activity android.app.ActivityThread.performLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent) (ActivityThread.java:3608) java_vm_ext.cc:579] at android.app.Activity android.app.ActivityThread.handleLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.app.servertransaction.PendingTransactionActions, android.content.Intent) (ActivityThread.java:3792) java_vm_ext.cc:579] at void android.app.servertransaction.LaunchActivityItem.execute(android.app.ClientTransactionHandler, android.os.IBinder, android.app.servertransaction.PendingTransactionActions) (LaunchActivityItem.java:103) java_vm_ext.cc:579] at void android.app.servertransaction.TransactionExecutor.executeCallbacks(android.app.servertransaction.ClientTransaction) (TransactionExecutor.java:135) java_vm_ext.cc:579] at void android.app.servertransaction.TransactionExecutor.execute(android.app.servertransaction.ClientTransaction) omitted runtime.cc:669] native: #111 pc 00000000002cdfe8 /apex/com.android.art/lib64/libart.so (art_quick_invoke_static_stub+568) 2022-02-27 15:54:08.753 23733-23733/com.example.depthai_android_jni_example A/oid_jni_exampl: runtime.cc:669] native: #112 pc 00000000004550b4 /apex/com.android.art/lib64/libart.so (art::JValue art::InvokeWithVarArgs<_jmethodID*>(art::ScopedObjectAccessAlreadyRunnable const&, _jobject*, _jmethodID*, std::__va_list)+464) runtime.cc:669] native: #113 pc 0000000000633790 /apex/com.android.art/lib64/libart.so (art::JNI::CallStaticVoidMethodV(_JNIEnv*, _jclass*, _jmethodID*, std::__va_list)+268) runtime.cc:669] native: #114 pc 00000000000aeac4 /system/lib64/libandroid_runtime.so (_JNIEnv::CallStaticVoidMethod(_jclass*, _jmethodID*, ...)+120) runtime.cc:669] native: #115 pc 00000000000ba05c /system/lib64/libandroid_runtime.so (android::AndroidRuntime::start(char const*, android::Vector const&, bool)+836) runtime.cc:669] native: #116 pc 000000000000258c /system/bin/app_process64 (main+1336) runtime.cc:669] native: #117 pc 00000000000488c8 /apex/com.android.runtime/lib64/bionic/libc.so (__libc_init+96) runtime.cc:669] at com.example.depthai_android_jni_example.MainActivity.startDevice(Native method) runtime.cc:669] at com.example.depthai_android_jni_example.MainActivity$1.run(MainActivity.java:75) runtime.cc:669] at com.example.depthai_android_jni_example.MainActivity.onCreate(MainActivity.java:64) runtime.cc:669] at android.app.Activity.performCreate(Activity.java:8051) runtime.cc:669] at android.app.Activity.performCreate(Activity.java:8031) runtime.cc:669] at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1329) runtime.cc:669] at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3608) runtime.cc:669] at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3792) runtime.cc:669] at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103) runtime.cc:669] at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) runtime.cc:669] at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) runtime.cc:669] at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2210) runtime.cc:669] at android.os.Handler.dispatchMessage(Handler.java:106) runtime.cc:669] at android.os.Looper.loopOnce(Looper.java:201) runtime.cc:669] at android.os.Looper.loop(Looper.java:288) runtime.cc:669] at android.app.ActivityThread.main(ActivityThread.java:7839) runtime.cc:669] at java.lang.reflect.Method.invoke(Native method) runtime.cc:669] at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) runtime.cc:669] at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003) runtime.cc:669] Pending exception java.lang.IllegalArgumentException: com.example.depthai_android_jni_example: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent. runtime.cc:669] Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles. runtime.cc:669] at void android.app.PendingIntent.checkFlags(int, java.lang.String) (PendingIntent.java:375) runtime.cc:669] at android.app.PendingIntent android.app.PendingIntent.getBroadcastAsUser(android.content.Context, int, android.content.Intent, int, android.os.UserHandle) (PendingIntent.java:645) runtime.cc:669] at android.app.PendingIntent android.app.PendingIntent.getBroadcast(android.content.Context, int, android.content.Intent, int) (PendingIntent.java:632) runtime.cc:669] at void com.example.depthai_android_jni_example.MainActivity.startDevice(java.lang.String, int, int) (MainActivity.java:-2) runtime.cc:669] at void com.example.depthai_android_jni_example.MainActivity$1.run() (MainActivity.java:75) runtime.cc:669] at void com.example.depthai_android_jni_example.MainActivity.onCreate(android.os.Bundle) (MainActivity.java:64) 2022-02-27 15:54:08.753 23733-23733/com.example.depthai_android_jni_example A/oid_jni_exampl: runtime.cc:669] at void android.app.Activity.performCreate(android.os.Bundle, android.os.PersistableBundle) (Activity.java:8051) runtime.cc:669] at void android.app.Activity.performCreate(android.os.Bundle) (Activity.java:8031) runtime.cc:669] at void android.app.Instrumentation.callActivityOnCreate(android.app.Activity, android.os.Bundle) (Instrumentation.java:1329) runtime.cc:669] at android.app.Activity android.app.ActivityThread.performLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent) (ActivityThread.java:3608) runtime.cc:669] at android.app.Activity android.app.ActivityThread.handleLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.app.servertransaction.PendingTransactionActions, android.content.Intent) (ActivityThread.java:3792) runtime.cc:669] at void android.app.servertransaction.LaunchActivityItem.execute(android.app.ClientTransactionHandler, android.os.IBinder, android.app.servertransaction.PendingTransactionActions) (LaunchActivityItem.java:103) runtime.cc:669] at void android.app.servertransaction.TransactionExecutor.executeCallbacks(android.app.servertransaction.ClientTransaction) (TransactionExecutor.java:135) runtime.cc:669] at void android.app.servertransaction.TransactionExecutor.execute(android.app.servertransaction.ClientTransaction) (TransactionExecutor.java:95) runtime.cc:669] at void android.app.ActivityThread$H.handleMessage(android.os.Message) (ActivityThread.java:2210) runtime.cc:669] at void android.os.Handler.dispatchMessage(android.os.Message) (Handler.java:106) runtime.cc:669] at boolean android.os.Looper.loopOnce(android.os.Looper, long, int) (Looper.java:201) runtime.cc:669] at void android.os.Looper.loop() (Looper.java:288) runtime.cc:669] at void android.app.ActivityThread.main(java.lang.String[]) (ActivityThread.java:7839) runtime.cc:669] at java.lang.Object java.lang.reflect.Method.invoke(java.lang.Object, java.lang.Object[]) (Method.java:-2) runtime.cc:669] at void com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run() (RuntimeInit.java:548) runtime.cc:669] at void com.android.internal.os.ZygoteInit.main(java.lang.String[]) (ZygoteInit.java:1003) runtime.cc:669] 2022-02-27 15:54:08.753 23733-23733/com.example.depthai_android_jni_example A/oid_jni_exampl: runtime.cc:677] JNI DETECTED ERROR IN APPLICATION: JNI CallVoidMethod called with pending exception java.lang.IllegalArgumentException: com.example.depthai_android_jni_example: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent. runtime.cc:677] Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles. runtime.cc:677] at void android.app.PendingIntent.checkFlags(int, java.lang.String) (PendingIntent.java:375) runtime.cc:677] at android.app.PendingIntent android.app.PendingIntent.getBroadcastAsUser(android.content.Context, int, android.content.Intent, int, android.os.UserHandle) (PendingIntent.java:645) runtime.cc:677] at android.app.PendingIntent android.app.PendingIntent.getBroadcast(android.content.Context, int, android.content.Intent, int) (PendingIntent.java:632) runtime.cc:677] at void com.example.depthai_android_jni_example.MainActivity.startDevice(java.lang.String, int, int) (MainActivity.java:-2) runtime.cc:677] at void com.example.depthai_android_jni_example.MainActivity$1.run() (MainActivity.java:75) runtime.cc:677] at void com.example.depthai_android_jni_example.MainActivity.onCreate(android.os.Bundle) (MainActivity.java:64) runtime.cc:677] at void android.app.Activity.performCreate(android.os.Bundle, android.os.PersistableBundle) (Activity.java:8051) runtime.cc:677] at void android.app.Activity.performCreate(android.os.Bundle) (Activity.java:8031) runtime.cc:677] at void android.app.Instrumentation.callActivityOnCreate(android.app.Activity, android.os.Bundle) (Instrumentation.java:1329) runtime.cc:677] at android.app.Activity android.app.ActivityThread.performLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent) (ActivityThread.java:3608) runtime.cc:677] at android.app.Activity android.app.ActivityThread.handleLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.app.servertransaction.PendingTransactionActions, android.content.Intent) (ActivityThread.java:3792) runtime.cc:677] at void android.app.servertransaction.LaunchActivityItem.execute(android.app.ClientTransactionHandler, android.os.IBinder, android.app.servertransaction.PendingTransactionActions) (LaunchActivityItem.java:103) runtime.cc:677] at void android.app.servertransaction.TransactionExecutor.executeCallbacks(android.app.servertransaction.ClientTransaction) (TransactionExecutor.java:135) runtime.cc:677] at void android.app.servertransaction.TransactionExecutor.execute(android.app.servertransaction.ClientTransaction) (TransactionExecutor.java:95) runtime.cc:677] at void android.app.ActivityThread$H.handleMessage(android.os.Message) (ActivityThread.java:2210) runtime.cc:677] at void android.os.Handler.dispatchMessage(android.os.Message) (Handler.java:106) runtime.cc:677] at boolean android.os.Looper.loopOnce(android.os.Looper, long, int) (Looper.java:201) runtime.cc:677] at void android.os.Looper.loop() (Looper.java:288) runtime.cc:677] at void android.app.ActivityThread.main(java.lang.String[]) (ActivityThread.java:7839) runtime.cc:677] at java.lang.Object java.lang.reflect.Method.invoke(java.lang.Object, java.lang.Object[]) (Method.java:-2) runtime.cc:677] at void com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run() (RuntimeInit.java:548) runtime.cc:677] at void com.android.internal.os.ZygoteInit.main(java.lang.String[]) (ZygoteInit.java:1003) runtime.cc:677] runtime.cc:677] in call to CallVoidMethod runtime.cc:677] from void com.example.depthai_android_jni_example.MainActivity.startDevice(java.lang.String, int, int) --------- beginning of crash 2022-02-27 15:54:08.754 23733-23733/com.example.depthai_android_jni_example A/libc: Fatal signal 6 (SIGABRT), code -1 (SI_QUEUE) in tid 23733 (oid_jni_example), pid 23733 (oid_jni_example) ```

Solution

  1. Build libusb instead of using pre-built libusb-1.0.so
  2. Add PendingIntent.FLAG_IMMUTABLE flag to permission_intent in linux_android_jni.c

Issue 2

Detail

It may be a minor issue but I faced build error missing depthai/build/config.hpp in ImgFrame.hpp

Solution

  1. Delete #include "depthai/build/config.hpp" in ImgFrame.hpp

Overall

I modified like the followings and it works well with Android 12

diff --git a/app/src/CMakeLists.txt b/app/src/CMakeLists.txt
index 572d56e..48bc6f5 100644
--- a/app/src/CMakeLists.txt
+++ b/app/src/CMakeLists.txt
@@ -24,10 +24,11 @@ include_directories( ${INCLUDE_DIR})
 include_directories( ${SRC_DIR})

 ######         libusb         ######
-add_library( libusb-lib SHARED IMPORTED )
-set_target_properties(libusb-lib
-        PROPERTIES IMPORTED_LOCATION
-        ${LIB_DIR}/libusb/${ANDROID_ABI}/libusb-1.0.so)
+add_subdirectory("main/include/libusb")
+# add_library( libusb-lib SHARED IMPORTED )
+# set_target_properties(libusb-lib
+#         PROPERTIES IMPORTED_LOCATION
+#         ${LIB_DIR}/libusb/${ANDROID_ABI}/libusb-1.0.so)

 ######         opencv         ######
 add_library( opencv-lib SHARED IMPORTED )
diff --git a/app/src/main/include/depthai/pipeline/datatype/ImgFrame.hpp b/app/src/main/include/depthai/pipeline/datatype/ImgFrame.hpp
index 20a1462..dfe6ff3 100644
--- a/app/src/main/include/depthai/pipeline/datatype/ImgFrame.hpp
+++ b/app/src/main/include/depthai/pipeline/datatype/ImgFrame.hpp
@@ -5,7 +5,7 @@
 #include <vector>

 // project
-#include "depthai/build/config.hpp"
+// #include "depthai/build/config.hpp"
 #include "depthai/pipeline/datatype/Buffer.hpp"

 // shared
diff --git a/app/src/main/include/libusb/CMakeLists.txt b/app/src/main/include/libusb/CMakeLists.txt
index f0f0821..290e841 100644
--- a/app/src/main/include/libusb/CMakeLists.txt
+++ b/app/src/main/include/libusb/CMakeLists.txt
@@ -26,7 +26,7 @@ project(
 )

 # Creates library target "usb-1.0"
-set(TARGET_NAME usb-1.0)
+set(TARGET_NAME libusb-lib)
 add_library(${TARGET_NAME})

 # Add PUBLIC include directory
diff --git a/app/src/main/include/libusb/libusb/os/linux_android_jni.c b/app/src/main/include/libusb/libusb/os/linux_android_jni.c
index b33535b..7e15772 100644
--- a/app/src/main/include/libusb/libusb/os/linux_android_jni.c
+++ b/app/src/main/include/libusb/libusb/os/linux_android_jni.c
@@ -490,7 +490,7 @@ int android_jni_request_permission(struct android_jni_context *jni,
    permission_intent =
        (*jni_env)->CallStaticObjectMethod(jni_env,
            jni->PendingIntent, jni->PendingIntent__getBroadcast,
-           jni->application_context, 0, intent, 0);
+           jni->application_context, 0, intent, 67108864); /* PendingIntent.FLAG_IMMUTABLE */

    (*jni_env)->DeleteLocalRef(jni_env, intent);
iwatake2222 commented 2 years ago

It's not a question, so I just close this issue now.

ibaiGorordo commented 2 years ago

Thank you for the notice!

I will give a try and see if it also works with my phone. I took the libusb build from the Luxonis fork, but I am glad to hear that it also works building it directly.

ibaiGorordo commented 2 years ago

I have just tested it and was able to reproduce the errors. I have updated the repository with a slight modification of your changes. Thanks!

iwatake2222 commented 2 years ago

Great!!

themarpe commented 2 years ago

Thanks @iwatake2222 for checking this out and proposing the fix. We have our own fork of libusb, which is also going to get used by DepthAI once we mainline Android support.

Do you mind opening a PR with the fix against cmake_android branch of: https://github.com/luxonis/libusb/

themarpe commented 2 years ago

@ibaiGorordo I see you already created a fork & fix - do you mind opening a PR back? Just one regard, use the enum/flag instead of the magic number if possible. If the flag isn't available pre Android12, use ifdef / other mechanism to select this flag value