androidandroid-libraryandroid-assetslibrary-project

how to reference an asset in a library project


In a class belonging to a Library project I call:

webview.loadUrl("file:///android_asset/info.html", null);

Unfortunately, this only works if I duplicate the file info.html into the Application's project asset folder as well.

Is there a way to tell an Android library code: "look for this file in the library's assets folder, not in the application's assets folder" ?


Solution

  • This answer is out of date, the gradle build system and AAR files support assets.


    From the Android Docs:

    Library projects cannot include raw assets

    The tools do not support the use of raw asset files (saved in the assets/ directory) in a library project. Any asset resources used by an application must be stored in the assets/ directory of the application project itself. However, resource files saved in the res/ directory are supported.

    If you want to include files from a Library project, you'll need to put it in the resources instead of the assets. If you're trying to load HTML files from your library project into a WebView, this means that you'll have to go a more roundabout method than the usual asset URL. Instead you'll have to read the resource data and use something like loadData.