Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8259243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:48:49+00:00 2026-06-08T02:48:49+00:00

I’m trying to build an Android app which makes use of the NativeActivity facility

  • 0

I’m trying to build an Android app which makes use of the NativeActivity facility of the NDK.
I’m having the following structure:

  • a bunch of native shared libraries installed in /system/vendor/<company>; I’m working
    with a custom built Android image so there’s no problem having the libraries there with
    proper permissions and everything
  • a couple of applications using the NativeActivity that depend in turn on the libraries
    mentioned above

The libraries installed in the /system/vendor and my applications use a couple of
configuration files. There’s no problem reading them using the standard C API
fopen/fclose. But those libraries and my application also need to store some files
as the result of their operation, like configuration, some run-time parameters, calibration
data, log files etc. With the storing of the files there is a slight issue as I’m not allowed to write into /system/vendor/... (as the file system under “/system/…” is mounted read-only and I do not want to hack on that).

So what would be the best way to create and store those files and where would be the
best “conforming with Android” storage area ?

I’ve been reading a couple of threads in the android-ndk Google group and here on SO that mention either the internal application private storage or the external SD card, but as I do not have extended experience with Android I’m not sure what would be the proper approach. If the approach involves some specific Android API a small code example in C++ would be very helpful; I’ve seen a couple of examples involving Java and JNI (e.g. in this SO question) but I would like to stay away from that right now.
Also there seems to be a problem with using from C++ the native activity’s
internalDataPath/externalDataPath pair (a bug that makes them be always NULL).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T02:48:50+00:00Added an answer on June 8, 2026 at 2:48 am

    For relatively small files(application config files, parameter files, log files etc.)
    is best to use the internal application private storage, that is /data/data/<package>/files.
    The external storage if it exists at all (being it SD card or not) should be used for large files that do not need frequent access or updates.

    For the external data storage the native application has to “request” the correct permissions in the application’s AndroidManifest.xml:

    <manifest>
        ... 
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">
        </uses-permission>
        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"> 
        </uses-permission>
    </manifest>  
    

    For the internal application private storage fopen/fclose(or C++ stream equivalents if available) API could be used. Following example illustrates using the Android NDK AssetManager to retrieve and read a configuration file. The file must be placed into the assets directory inside the native application’s project folder so that the NDK build could pack them inside the APK. The internalDataPath/externalDataPath bug I was mentioning in the question was fixed for the NDK r8 version.

    ...
    void android_main(struct android_app* state)
    {
        // Make sure glue isn't stripped 
        app_dummy();
    
        ANativeActivity* nativeActivity = state->activity;                              
        const char* internalPath = nativeActivity->internalDataPath;
        std::string dataPath(internalPath);                               
        // internalDataPath points directly to the files/ directory                                  
        std::string configFile = dataPath + "/app_config.xml";
    
        // sometimes if this is the first time we run the app 
        // then we need to create the internal storage "files" directory
        struct stat sb;
        int32_t res = stat(dataPath.c_str(), &sb);
        if (0 == res && sb.st_mode & S_IFDIR)
        {
            LOGD("'files/' dir already in app's internal data storage.");
        }
        else if (ENOENT == errno)
        {
            res = mkdir(dataPath.c_str(), 0770);
        }
    
        if (0 == res)
        {
            // test to see if the config file is already present
            res = stat(configFile.c_str(), &sb);
            if (0 == res && sb.st_mode & S_IFREG)
            {
                LOGI("Application config file already present");
            }
            else
            {
                LOGI("Application config file does not exist. Creating it ...");
                // read our application config file from the assets inside the apk
                // save the config file contents in the application's internal storage
                LOGD("Reading config file using the asset manager.\n");
    
                AAssetManager* assetManager = nativeActivity->assetManager;
                AAsset* configFileAsset = AAssetManager_open(assetManager, "app_config.xml", AASSET_MODE_BUFFER);
                const void* configData = AAsset_getBuffer(configFileAsset);
                const off_t configLen = AAsset_getLength(configFileAsset);
                FILE* appConfigFile = std::fopen(configFile.c_str(), "w+");
                if (NULL == appConfigFile)
                {
                    LOGE("Could not create app configuration file.\n");
                }
                else
                {
                    LOGI("App config file created successfully. Writing config data ...\n");
                    res = std::fwrite(configData, sizeof(char), configLen, appConfigFile);
                    if (configLen != res)
                    {
                        LOGE("Error generating app configuration file.\n");
                    }
                }
                std::fclose(appConfigFile);
                AAsset_close(configFileAsset);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
We're building an app, our first using Rails 3, and we're having to build
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to loop through a bunch of documents I have to put
Basically, what I'm trying to create is a page of div tags, each has
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.