且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android的使用C从资源文件夹读取文本文件(NDK)

更新时间:2023-09-26 08:03:34

下面是code我用来读取Android的资产文件夹使用asset_manager NDK的lib

  AAssetManager *经理= AAssetManager_fromJava(ENV,assetManager);
    AAsset *资产= AAssetManager_open(MGR,(为const char *)JS,AASSET_MODE_UNKNOWN);
    如果(NULL ==资产){
        __android_log_print(ANDROID_LOG_ERROR,NF_LOG_TAG_ASSET_NOT_FOUND_);
        返回JNI_FALSE;
    }
    长尺寸= AAsset_getLength(资产);
    字符*缓冲=(字符*)malloc的(的sizeof(char)的*大小);
    AAsset_read(资产,缓冲液,大小);
    __android_log_print(ANDROID_LOG_ERROR,NF_LOG_TAG,缓冲区);
    AAsset_close(资产);

添加下面一行到我的Andr​​oid.mk

 #为本地资产管理公司
LOCAL_LDLIBS + = -landroid

和不要忘了包括源文件

 的#include<机器人/ asset_manager.h>

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)

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?

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);

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>