Search code examples
androidandroid-studiojava-native-interfacefopenaar

How to use fopen for JNI native functions when building .aar?


I’m trying to build a .aar library which contains native/C++ codes, so there is a jni folder at src\main\jni in the structure. One of the C++ function uses fopen to read a text file. The text file to be read is at src\main\jni along with other .cpp/.h as well. I’ve changed the file path for fopen to "/data/data/[package name]/files/XXX.txt" but it still fails. I’ve searched for some related posts but so far there’s no clear solution... thanks for any suggestions! (I'm using Android Studio 2.1.1)


Solution

  • You should put the text file in the assets folder instead and use the AAssetManager API to read it at runtime. This is summarised in this post: Android read text file from asset folder using C (ndk)