Android使用C(ndk)从资产文件夹中读取文本文件 [英] Android read text file from asset folder using C (ndk)
问题描述
我需要从android中的asset文件夹中读取文本文件,通过互联网搜索我发现从android 2.3开始就有asset_manager api.因为我只针对平板设备,所以这很有用.但由于我不是 C 语言专家,我无法找到任何关于如何使用文件描述符读取/写入文件的示例.我发现了很多使用 FILE*(文件指针)的例子
I need to read text file from asset folder in android, by searching through internet I found that there is asset_manager api available from android 2.3 onwards. As I am targeting only tablet devices this is useful. But as I am not expert in C language I am not able to find any example on how to read/write files using file descriptor. I found many examples using FILE* (file pointers)
我的目标是从加密的资产文件夹中解密一个 js 文件使用 C(用于保护代码),因为如果最终用户可见 js 代码反编译我的apk.因为asset文件夹在zip文件里面,可以吗?
My goal is to decrypt a js file from asset folder which is encrypted using C (for securing the code), as js code is visible if end user decompiled my apk. Because asset folder is inside zip file is it possible to do?
推荐答案
这是我使用asset_manager ndk lib从android assets文件夹中读取文件的代码
Here is the code I used to read file from android assets folder using asset_manager ndk lib
AAssetManager* mgr = AAssetManager_fromJava(env, assetManager);
AAsset* asset = AAssetManager_open(mgr, (const char *) js, AASSET_MODE_UNKNOWN);
if (NULL == asset) {
__android_log_print(ANDROID_LOG_ERROR, NF_LOG_TAG, "_ASSET_NOT_FOUND_");
return JNI_FALSE;
}
long size = AAsset_getLength(asset);
char* buffer = (char*) malloc (sizeof(char)*size);
AAsset_read (asset,buffer,size);
__android_log_print(ANDROID_LOG_ERROR, NF_LOG_TAG, buffer);
AAsset_close(asset);
在我的 Android.mk 中添加了以下行
Added following line to my Android.mk
# for native asset manager
LOCAL_LDLIBS += -landroid
不要忘记源文件中的包含
And don't forget the include in source file
#include <android/asset_manager.h>
这篇关于Android使用C(ndk)从资产文件夹中读取文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!