How can I access .so file in apk at runtime? - android

I'd like to access(read) my .so file built in NDK at runtime in order to
check up on its hash value.
To do that, I used AAssetManager_open() function but always failed.
const char* filename = "/data/data/com.mycompany.app/lib/libmyndk.so"; // failed with this.
const char* filename = "assets/libmyndk.so"; // Also failed with this.
AAsset* asset = AAssetManager_open(mgr, (const char*) filename, AASSET_MODE_UNKNOWN);
Please let me know why it persist to fail.

I solved out this issue. We don't need to use AssetManager for just simply accessing.
just use fopen() in NDK(c/c++ files).
for example,
void initNDK()
{
const char* filename = "/data/data/com.company.app/lib/libmyndk.so";
FILE* fp = fopen(filename, "rb");
if(fp)
TRACE("Success");
else
TRACE("Failure");
}
Thank you All.

you have load .so file during application start.
By
static {
System.loadLibrary("libpacman");
}
Than you can access file .
i hope this is help you.

Have you tried:
const char* filename = "libmyndk.so";

Above all, I succeeded in reading/accessing some file in assets folder with AAssetManager_open().
But this AAssetManager_xxxx() functions seem to be only available to access files in assets folder.
What I want to do is to access my .so file built in NDK. Still looking for answer.

If you want to use native files you need to use android NDK.
This link will help you if you use Eclipce: How do I import a native library (.so file) into Eclipse?
And this is for Android Studio: http://www.kylethielk.com/blog/include-native-so-library-in-apk-with-android-studio/
I hope it helps to resolve your problem.

Related

How can i reach files from Android NDK?

I never make a android project and i don't know java. I am trying to port my c++ sdl opengl project to android. I am using android studio. I opened sdl sample android project and now i can manage project from main.cpp . How do i know this because I changed the color of the window and it worked. But when i try to load shader.text file with ifstream program crashed. I understand that this will not happen. After this i searched but I couldn't find a complete code or an example.
When i put files in assets folder and build apk. apk size grows as files. Therefore I need to kepth files in assets folder ?
I think i need to use asset manager but of course i don't know.
What files should I write what code? I mean what i need to add main.cpp and I need to add code java side also ?
main.cpp :
int main()
{
//lets say i have filePath like these
char file_vert[100];
snprintf(file_vert,100,"shaders/vertexShader.text");
char file_Texture[100];
snprintf(file_Texture,100,"textures/image.jpg");
//i am using them in cpp project
//shader text file like this
std::ifstream vShaderFile;
vShaderFile.open(file_vert);
//and i am loading texture file with "stbi" library
unsigned char *data = stbi_load(file_Texture, &width, &height, &nrComponents, 0);
}

Assimp with android port. Error import .obj file

I am built assimp for android with android port (AndroidJNIIOSystem). When I import a file from assets, I get message: Assimp: Asset exists and next get error:
Error::Assimp::No suitable reader found for the file format of file "model/nanosuit.obj".
I have assets hierarchy:
assets/model/nanosuit.obj
My code:
importer.SetIOHandler(ioSystem);
auto modelPath = "model/nanosuit.obj";
const aiScene *scene =
importer.ReadFile(modelPath, aiProcess_Triangulate | aiProcess_FlipUVs);
if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode) {
LOGI("%s::%s", "Error::Assimp", importer.GetErrorString());
}
But then I use this code with assimp built for Linux, it work without any errors. Please, help me find solution. Thanks!
P.S. importer.IsDefaultIOHandler() returns false.
When I build lib, I am use -DASSIMP_BUILD_ALL_IMPORTERS_BY_DEFAULT=OFF in order to reduce lib file size. If remove this flag, all works fine. That use .obj importer you need use -DASSIMP_BUILD_OBJ_IMPORTER=ON flag

File hierarchy with Android native AAssetManager

Issue
I wonder how to make file hierarchy of assets folder in Android from native code. I'm using AAssetManager_openDir but AAssetDir_getNextFileName doesn't return any directory names so basically I have no way to dive deeper into hierarchy because I can't obtain names of subfolders. Is there any way to resolve this issue with AAssetManager or should I implement this in Java?
Example
Assets looks like
a/
file1
file2
b/
file3
c/
file4
C++ code
AAssetDir* assetDir = AAssetManager_openDir(env, "");
// filename is NULL since there is only folders at the root folder
const char* filename = AAssetDir_getNextFileName(assetDir);
AAssetDir_close(assetDir);
AAssetDir* assetDir = AAssetManager_openDir(env, "a");
// filename is "file1"
const char* filename = AAssetDir_getNextFileName(assetDir);
// filename is "file2"
filename = AAssetDir_getNextFileName(assetDir);
AAssetDir_close(assetDir);
EDIT:
I found out that AAssetDir_getNextFileName implementation literally filters directories from output.
Link to source code
You can achieve your goal in three steps, each requires some programming. The bottom line is, your assets all live in the APK file, and your app can find and read the APK file, and its format is a ZIP archive.
Find your APK file name.
Open this file for read as zip archive.
Iterate the zip contents for assets/<whatever>.
For 1), you can use Java or if you must stay in c++, filter /proc/self/fd. You may find some system APK files (framework), but your APK will be the only one that is not system.
For 2), see use zlib to list directory structure of apk file in android. Note that zlib is part of public NDK libraries.

c++ copy file for ios and android

I am familiar with objective-c to copy file using cocoatouch sdk.
But I hope add copy file function to my game (cocos2dx c++).
Is there a common way for ios and android to copy file using c++?
Your comment welcome
int main()
{
std::ifstream src("from.ogv", std::ios::binary);
std::ofstream dst("to.ogv", std::ios::binary);
dst << src.rdbuf();
}
Taken from here.
Of course in Android you need NDK for that.

Writing to internal storage in c++ from jni

I am trying to write a file in the internal storage directory of my application with the following step :
1) Initializing my "jni library" in Acivity Class :
MyLib mylib = new MyLib();
2) Give the internal storage path by calling getFilesDir in my Activity Class:
mylib.setSavePath(getFilesDir());
3) Call a method mylib.save() from my library which is doing the following in c++:
Open the file which i want to write with :
fp = fopen(pathtotheinternalstorage+filename,"w");
if (!fp) {
SetError(XML_ERROR_FILE_NOT_FOUND, filename, 0);
return _errorID;
}
The file path is correct : /data/data/com.myapp/files/myfile.xml
But fopen fails, i dont know what i am doing wrong.
If i write with some java code (openFileOutput), it is working well.
Thanks for your help.
More information would be helpful. Based on the path, I'm assuming you're on a Linux box. I would first check the permissions of the path. A few suggestions below
1) Try opening a file in your home directory and see if you have the same issue.
2) Try setting the file world writable using chmod 777 and see if that fixes the problem. Remember setting world writable is really INSECURE and you should change it to something more restrictive once you've found the problem.
I found the answer, i was using a library that was redefinig fopen to read inside asset folder.
#define fopen(name, mode) android_fopen(name, mode)
now it s working

Categories

Resources