1

How can I obtain the path to library assets from the main app? Where in the filesystem are assets stored?

In my Android Studio (1.1.0) project I have app and library modules like so:

project
  - [lib]
      - src
          - main
              - assets   // <--
                  - hello.txt
              - java
              - jni      // native files
  - [app]
      - src
          - main
              - java

I wasn't able to access the assets by calling getFilesDir().getPath() + "hello.txt" which resolves to something like "/data/data/com.package.app/files/hello.txt"

Does gradle merge library and app assets automatically? Do I need to modify build.gradle in my lib or app?

The reason I need the path is so that I can access the assets from the NDK.

Ryan R
  • 8,342
  • 15
  • 84
  • 111

2 Answers2

1

Assuming your code runs in an Activity or anything that is Context, to get input stream for a file store in assets:

AssetManager am = getAssets(); // called from Context
String name = "hello.txt"; // relative path to a file in assets directory
InputStream is = am.open(name);

Read more about AssetManager More about doing the same in NDK

Community
  • 1
  • 1
Kirill K
  • 771
  • 6
  • 17
1

How can I obtain the path to library assets from the main app?

There is no path to the assets.

Where in the filesystem are assets stored?

They are not stored in the filesystem. They are entries in the APK.

The reason I need the path is so that I can access the assets from the NDK.

I am not aware that NDK code has direct ability to access assets. My guess is that you will need to get that data over to your NDK code in some other way. Worst-case, you could pass the InputStream that you get from AssetManager 9see Kirill's answer) into your JNI method, though I don't know how well that works.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    oh, I missed the NDK part of the question! It's [already on SO](http://stackoverflow.com/questions/10409125/android-read-text-file-from-asset-folder-using-c-ndk) – Kirill K Mar 18 '15 at 22:19
  • @KirillK: Oh, cool, they do provide an option for that. Thanks for pointing it out! – CommonsWare Mar 18 '15 at 22:27
  • Thanks. In my case will the assets from the library module be included in the APK? – Ryan R Mar 19 '15 at 16:59
  • @RyanR: Yes, assets from Android library projects/modules should be packaged into the APK. – CommonsWare Mar 19 '15 at 17:58