I’m writing an Android app using the NDK and NativeActivity. My app depends on a few bits of third party code that are shipped as assets. Currently I’m working on trying to extract those assets while keeping the folder structure intact.
I’ve tried using the AssetManager, but to keep the folder structure intact it seemed like there would a huge amount of code involved, for a simple task such as what I’ve mentioned. I’ve since switched focus to try to implement treating the APK as a ZIP file and extract its contents that way. But that requires I find the exact path to the APK.
In a normal Android app one would use getPackageCodePath, but this is an abstract method attached to the Context class. My question is how do I obtain the exact path to the APK when not using a normal Activity?
Also I tried calling getPackageCodePath via JNI, but that crashed the app on account of not being able to find the method.
EDIT:
Is this even possible?
I was actually able to call
getPackageCodePathvia JNI and get it to work. The following code put at the top ofandroid_mainin the native-activity sample in NDK r7 logs the correct path and doesn’t crash:I feel like this might not be a great solution, though. There are two things that worry me:
envmember ofANativeActivityin the main Java thread, and if I understand things correctly, this code is going to get run in the native activity’s thread.ANativeActivity‘sclazzmember appears to be misnamed and is actually the instance of the JavaNativeActivityinstead of the class object. Otherwise this code wouldn’t work. I really hate relying on something that is obviously misnamed like this.Aside from that, it works, and I’m actually about to use it myself to try to extract the assets out of the .apk using libzip and into the data directory.