Android /data/app directory

Let's say some app com.qux.bar has some assets (app's installation files) like {project}/scenes/waterfall.webp (not statically incorporated through include_bytes! in Rust).

According to AI information, these files reside in /data/app/{...}, and not /data/data/{ID}. I've downloaded ZArchiver in my Android phone (not rooted) to see what's in /data/app/{...}, but it turns out it's structured like

  • data/
    • app/
      • ~~-ZDrxMxFFkgo...bg==/
        • com.instagram.android-QD9SphAoXwr...Hg==/
          • lib/
          • oat/
          • base.apk
          • ...
      • ...

I'm not sure if this is what is considered an "installation directory" (in Windows it's generally the place where executable resides plus files that aren't fixed in the RAM like heavy icon assets, object descriptions and scenes).

I know that Adobe AIR has the concept of app:// URLs, which are relative to the app's "installation directory". I wanted to produce the same in Rust, but am a bit confused about the Android case.

We have at least android_activity::AndroidApp::internal_data_path(), but it probably yields something like /data/data/{ID}, and as I've shown, data/app/... entries seem to have a base64 encoding inside them.

It says application's internal data directory, which is a pretty vague description to me.

base.apk is where the app being executed. Unlike Windows or Linux, Android runs applications directly from the .apk or .oat (optimized with dex2oat) files.

There are some data directories in the Android world (you must be rooted to see most of them with another app):

applicationContext.filesDir: /data/data/<pkg>/files
applicationContext.cacheDir: /data/data/<pkg>/cache

The reference at Android documentation: https://developer.android.com/training/data-storage/app-specific

1 Like

The dev docs say this about filesDir:

The returned path may change over time if the calling app is moved to an adopted storage device, so only relative paths should be persisted.

So it may not always return /data/data/<pkg>/files? I'm confused!

I also wonder: if this is the case, then /data/data/<pkg>/files is created automatically by Android (so I don't have to do it manually)?

1 Like

I guess this means it will return something like ${PREFIX}/data/data/<pkg>/files, where PREFIX is either empty (by default) or points to the SD card (if application was installed there). This is only a guess though.

1 Like

The data folders was created automatically by Android.

I'm not sure about that, I don't have a SD card on my device

I know Android can install apps on a SD card, but I don't have a device with a SD card slot to test this behavior.

I have highlighted the /data/ folder on the screenshot below:

1 Like

I see, but installation files aren't copied to that files directory according to AI:

The files directory within an Android app's internal data directory (e.g., /data/data/YOUR_PACKAGE_NAME/files) is primarily intended for storing app-specific data files, not the application's installation files.

Here's a breakdown:

  • Installation Files:

The core installation files of an Android application (the APK) are typically stored in the /data/app directory after installation. This location is managed by the Android system.

Here's the AndroidAppInner::internal_data_path() implementation. Maybe it shouldn't be a lot of effort to get into this /data/app directory... Might require a PR or a fork, but I'm not sure if the app's resources will really be available there after installing the app (nor do I know how to get that directory yet)...

Whether it's possible to use the files directory for that or not depends on whether I'm able to run a post-install script on an .apk, which I believe isn't possible. :confused:

what do you mean installation files

Android is just copying the provided apk to /data/app/<base64>/<pkg>-<base64>/base.apk to install an application.

When you click the icon on the launcher, Android runs the application from the .apk file.

Since apk is build from Java/Kotlin. So something works like Java (not at all, but most)

(I'm sorry about that I don't know how to explain these things clearly)

1 Like

No, static resource files like image assets are not directly copied to a general /data/app directory. Instead, they are embedded within the app's APK (or AAB) file, which is a package that gets installed in the /data/app location, and are accessed by the app at runtime from their bundled location or via asset delivery mechanisms. The Android system handles the unpacking and access of these resources when the app is installed and launched.

How Resources Are Bundled and Accessed

  • Bundled in APK/AAB:

When you build an Android app, the image assets, along with other resources like layouts and strings, are placed into a specially structured resource directory (like res/drawable) and then compiled into the app's APK (Android Package) or AAB (Android App Bundle) file.

  • Installation:

During installation, the Android system installs the entire APK/AAB to a dedicated directory for that app under /data/app.

  • Runtime Access:

At runtime, your app doesn't need to access a separate location for these static resources. The Android system provides APIs that allow the app to load these resources directly from within its own packaged structure.

Special Cases

  • Asset Delivery: For very large assets that would significantly increase the initial app download size, Google Play offers asset delivery mechanisms like:

@cubewhy

what do you mean installation files

In Adobe AIR you can do things like:

const file:File = new File("app://scenary/waterfall.png");
file.addEventListener("complete", function(e:Event):void {
    trace(file.data); // [object ByteArray]
});
file.load();

The app project would have this typically:

  • scenary
    • waterfall.png
  • src
    • AS3/MXML stuff
  • application.xml (AIR app descriptor)
  • asconfig.json (AS3 and some AIR configuration)

asconfig.json instructs the adt -package and adl (dev launcher) commands so that the app will embed scenary/waterfall.png, but not "statically" into the ActionScript 3 runtime lifetime (RAM), but rather in the app's installation directory.

In Windows this scenary/waterfall.png file would typically get in the same directory as the executable's.


Android is just copying the provided apk to /data/app/<base64>/<pkg>-<base64>/base.apk to install an application.

I see. Probably I'll have to bind to some Java API for accessing resource files from the .apk.

I'm aware of Dalvik (DEX), but it's mostly a worry when doing Rust bindings stuff.

BTW, I have viewed the directory in an AVD

There actually exists an application data dir on the sd card.

1 Like

The image assets are bundled with the APK

1 Like

BTW, you cannot modify the APK directly

Do modifies to an APK may change its signature (if you don't have the origin certs)

There's no way to install an apk with a different signature unless use some xposed modules (like corepatch) or uninstall the application

Yeah, the app:// scheme is read-only in my framework :smile: No writes or deletes, just a mere read() operation for different platforms

I'm not familiar with Rust on Android so much

But I think open the .apk file as a zip file is a solution

1 Like

I guess I found out what I want, but I'll need to manually access the JNI from android-activity.

For reading files, I just need the AssetManager:

public class MyActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // ...
        AssetManager assetManager = getAssets();
        // Use assetManager to access assets

        // assetManager.open("path/to/file")
    }
}

When bundling/launching I'll need cargo-apk and configure the assets option to point to an internal directory where I'll drop the "installation files" (based on my globs).

The Android NDK provides C APIs to access the asset manager directly without needing to use JNI: https://developer.android.com/ndk/reference/group/asset

There appear to be bindings for this in several NDK-related Rust crates, e.g. ndk::asset - Rust - but I haven't actually done any Android development using Rust, so can't recommend anything in particular.

1 Like

Nice, we've it already then :smiley:

Only now I've noticed AndroidApp::asset_manager()

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.