Alexey Romanov
Alexey Romanov

Reputation: 170745

How to correctly find native lib path in Android?

I am trying to follow advice from https://issuetracker.google.com/issues/128554619#comment4 (not on Android 10 yet, but want my code to work there as well):

While exec() no longer works on files within the application home directory, it continues to be supported for files within the read-only /data/app directory. In particular, it should be possible to package the binaries into your application's native libs directory and enable android:extractNativeLibs=true, and then call exec() on the /data/app artifacts. A similar approach is done with the wrap.sh functionality, documented at https://developer.android.com/ndk/guides/wrap-script#packaging_wrapsh .

as suggested in this answer: https://stackoverflow.com/a/58748468/9204

However, in my case the directory to which the native libs are extracted is not

File(filesDir.parentFile!!, "lib")

as in that answer but

File(File(packageCodePath).parentFile!!, "lib/arm64")

This is obviously hacky (especially hardcoding arm64) and may depend on specific Android version and/or device.

Is there a documented way to find the native libs directory?

Upvotes: 2

Views: 6164

Answers (1)

Alexey Romanov
Alexey Romanov

Reputation: 170745

It's applicationInfo.nativeLibraryDir (on a Context).

Also even though https://developer.android.com/ndk/guides/wrap-script#packaging_wrapsh says

Android Studio only packages .so files from the lib/ directories, so if you're an Android Studio user, you'll need to place your wrap.sh files in the src/main/resources/lib/* directories instead, so that they'll be packaged correctly.

at least on one device files placed src/main/resources/lib/* were packaged in the APK but not extracted afterwards if it wasn't named lib*.so. Fixed by renaming the executable file, happily nothing depended on the name itself.

Upvotes: 4

Related Questions