Has anyone of you found a solution? I’m facing the same issue.
I have copied all required .so files such as libcutils.so, libvndksupport.so, libc++.so, etc. to the jniLibs folder from the /system/lib64 path since libOpenCL.so requires all of these. But, after copying all the libaries, the app crashes at the stage where ld-android.so is called. I’m posting the backtrace here for more details:
2024-03-27 21:30:43.446 24682-24682 DEBUG pid-24682 A #00 pc 0000000000001004 /data/app/~~V_u95z6ithwbxixbJ3Hohw==/com.example.opencltest-v7w12XtotUmr12gJzEibDQ==/base.apk!ld-android.so (offset 0x4000) (__loader_android_link_namespaces+4) (BuildId: 61a1d0fafe0d2b0e25353dbbdfe73925)
2024-03-27 21:30:43.446 24682-24682 DEBUG pid-24682 A #01 pc 00000000000010e0 /data/app/~~V_u95z6ithwbxixbJ3Hohw==/com.example.opencltest-v7w12XtotUmr12gJzEibDQ==/base.apk!libdl_android.so (offset 0x28000) (android_get_exported_namespace+12) (BuildId: 9e8f737153187060d8176c2dbe0a2096)
2024-03-27 21:30:43.446 24682-24682 DEBUG pid-24682 A #02 pc 00000000000011a8 /data/app/~~V_u95z6ithwbxixbJ3Hohw==/com.example.opencltest-v7w12XtotUmr12gJzEibDQ==/base.apk!libvndksupport.so (offset 0x8000) (android_load_sphal_library+328) (BuildId: 3e0086769009296ad00fcff0e3d51a9a)
2024-03-27 21:30:43.446 24682-24682 DEBUG pid-24682 A #03 pc 00000000000221c0 /data/app/~~V_u95z6ithwbxixbJ3Hohw==/com.example.opencltest-v7w12XtotUmr12gJzEibDQ==/base.apk!libOpenCL.so (offset 0x620000) (clGetPlatformIDs+48) (BuildId: 94a79fb140b82c30bd6caa4a6114f5cf)
2024-03-27 21:30:43.446 24682-24682 DEBUG pid-24682 A #04 pc 000000000001dd1c /data/app/~~V_u95z6ithwbxixbJ3Hohw==/com.example.opencltest-v7w12XtotUmr12gJzEibDQ==/base.apk!libopencltest.so (offset 0x704000) (Java_com_example_opencltest_MainActivity_stringFromJNI+108) (BuildId: 16d3c8b48f4ad0eb7d0cd9363bd6e5f0725c622f)
Edit: Within some 15min of posting this, I found the working solution.
What worked:
Adding the exclude option in the build.gradle.kts(:app)
+ specifying native lib in the AndroidManifest.xml
worked
If either of this is not done, it fails.
Background:
I was reading this forum thread from yesterday, but couldn’t find the solution. So, I decided to write this and check if someone responds, and just after posting it, I browsed through all the previous posts once again to see if I missed something. I found that one of the posts with the “exclude option” by the OP @terdev was marked as the solution, and another one by @atwork had said specifying “uses native lib” in the manifest helped solve it. Though I had tried both of those solutions individually, the first “exclude option” wasn’t tried properly with the new Kotlin format, and I had never tried using both the solutions together at the same time. On modifying the “exclude option” to comply with the new kotlin style after googling some more stuff, I was able to add this exclude option properly in addition to the “uses native lib” in the manifest file. And it finally worked without errors or crashes.
Summary:
For the build to succeed, we need to include a libOpenCL.so file and link it in the CMakeLists.txt to the target. But this .so shouldn’t be packaged, and it needs to be excluded by specifying it in the gradle file, but once it is excluded, we need to tell android to use the native library in the manifest file. This can be done by including the following 2 sections:
- Add this:
packaging {
jniLibs {
// Exclude a specific lib of all architectures
excludes += "/lib/**/libOpenCL.so"
}
}
under android {} in the build.gradle.kts(:app) file. Directly using the same solution in the post won’t work with Kotlin/the new versions, so we need this. Not sure why we have a lib instead of jniLibs here, but this was tested and found to be working as is.
- Add this:
<uses-native-library android:name="libOpenCL.so"/>
under in the manifest file.
We don’t need to package all the transitive dependencies - such as libcutils, ld-android.so etc. with the app.