Chapter 8. External data files, Android assets

The idea is the same as in previous chapter - to place the data files within the reach of the executable. However, in Android's case we are dealing NOT with the "executable", but with APK. Besides, we have to deliver data files not just to different folder, but to different device. The only "transport" (known to me) is a special "assets" folder that can be embedded into APK, that will be eventually installed on destination device. So, let's create it.

1. Start Visual Studio, open C:\CPP\a999hello\p_android\p_android.sln solution.
Right-click on p_android.Packaging project (NOT p_android.NativeActivity as usual, but p_android.Packaging ). Add -> New Folder, call it assets. It's a special reserved name, p_android.Packaging will know what to do with it.

2. Now, open p_android.NativeActivity project Properties, All Configurations / ARM64, go to Configuration Properties -> Build Events -> Post-Build Event -> Command Line -> Edit, add command

xcopy "..\..\..\engine\dt\*.*" "..\$(RootNamespace).Packaging\assets\dt\" /E /R /D /y

Ok, Apply, Ok


"..\..\..\engine\dt\*.*" - copy from. Unlike in Windows, here we have to go 1 folder level higher.

"..\$(RootNamespace).Packaging\assets\dt\" - copy to where. Since this command will be executed from p_android.NativeActivity folder, we need to step 1 folder level up and proceed to p_android.Packaging folder and then to assets.

The rest is the same as for Windows.

  1. VS top menu -> Build -> Rebuild Solution. Result: “2 succeeded, 0 failed, 0 skipped”.

Now, if we open C:\CPP\a999hello\p_android\p_android.Packaging\assets folder (in Windows File Explorer, not in VS), we'll find there our "dt". Now it's also supposed to be included in the APK as well. But, to my big surprise, it is not. No trace of test0.txt file in the APK (which is located at C:\CPP\a999hello\p_android\p_android.Packaging\ARM64\Debug).

4. It turned out that we had to manually add each file and folder to assets, as we usually do with program files (right-click, add item, and so on). This is a BIG problem. Obviously, we will have a huge number of these files and folders: shaders, models, textures, etc. It is simply impossible to keep track of this. We need to somehow force the assets folder to automatically fetch its contents.

The solution did not come easy, had to dive VERY deep, but NOW I know

How to add files to Android assets in Visual Studio

In Android Studio (as opposed to Visual Studio), anything placed in the assets folder will be automatically included into APK. To reproduce this behavior, we need to edit p_android.Packaging project file:

Close Visual Studio.

In a Text Editor open C:\CPP\a999hello\p_android\p_android.Packaging\p_android.Packaging.androidproj

Scroll down. Closer to the end you will see the section:

    <Content Include="res\values\strings.xml" />
    <AntBuildXml Include="build.xml" />
    <AndroidManifest Include="AndroidManifest.xml" />
    <AntProjectPropertiesFile Include="" />

In the top of this section add:

<Content Include="assets\**" />

so the section will now be:

    <Content Include="assets\**" />
    <Content Include="res\values\strings.xml" />
    <AntBuildXml Include="build.xml" />
    <AndroidManifest Include="AndroidManifest.xml" />
    <AntProjectPropertiesFile Include="" />


5. Start Visual Studio, open C:\CPP\a999hello\p_android\p_android.sln solution.

Top menu -> Build -> Rebuild solution. Result: "2 succeeded, 0 failed, 0 skipped".

Now, if you open C:\CPP\a999hello\p_android\p_android.Packaging\ARM64\Debug\p_android.apk in a text editor, you can make sure that the test0.txt file is there.

6. So now we can read the file on Android?? Most certainly. The code:

	//reading test file
	char buff[256];
	AAssetManager* am = androidApp->activity->assetManager;
	AAsset* asset = AAssetManager_open(am, "dt/shaders/test0.txt", AASSET_MODE_UNKNOWN);
	if (asset != NULL) {
		long size = AAsset_getLength(asset);
		AAsset_read(asset, buff, size);
		mylog("File size = %d, text:\n%s\n", size, buff);
	else {
		mylog("Asset not found.\n");

Open main.cpp. Scroll down. Insert this code right before

Switch on, unlock and plug in Android, allow debugging.

  • Open Logcat first (VS top menu -> Tools -> Android Tools -> Logcat), in search field type "mylog", Enter. If necessary - clean the screen (red cross icon in Logcat's menu).

Now - build and run (green arrow). Re-open Logcat if closed. Result:

Now what? Shaders??

No, not yet, not yet…

To be continued…

Leave a Reply

Your email address will not be published. Required fields are marked *