Android的使用C从资源文件夹读取文本文件(NDK) [英] Android read text file from asset folder using C (ndk)

查看:1072
本文介绍了Android的使用C从资源文件夹读取文本文件(NDK)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要阅读从资源文件夹中的android文本文件,通过网络搜索,我发现有可用的Andr​​oid 2.3起asset_manager API。由于我只针对平板设备,这是很有用的。但正如我不是专家在C语言中,我无法找到如何读取任何例子/写使用文件描述符文件。我发现使用FILE *许多例子(文件指针)


  

我的目标是从解密文件夹资产JS文件被加密
  使用C(用于固定code),因为JS code是可见的,如果最终用户
  反编译我的APK。由于资产文件夹是zip文件里面是有可能这样做?



解决方案

下面是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>

这篇关于Android的使用C从资源文件夹读取文本文件(NDK)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆