How to Reference an Asset in a Library Project

how to reference an asset in a library project

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.

Assets in assets/ directory for Android library (aar)

I think that some of the Development considerations are outdated. For example they also says that <uses-library> elements must be declared in both the library and the application (this is now handled by the manifest merger).

By the way a few lines below, Anatomy of an AAR file shows that an AAR file may include an /assets/ directory

Include assets when building angular library

As already said, angular library now support assets since the v9.x of angular.

But it isn't well explained on their website. To make it work you'll have to:

  1. Add an assets folder at the root of your library project

    Sample Image
  2. Add "assets": ["./assets"], into the ng-package.json file of the library
{
"$schema": "../../node_modules/ng-packagr/ng-package.schema.json",
"dest": "../../dist/icon",
"assets": ["./assets"], // <-- Add it here
"lib": {
"entryFile": "src/public-api.ts"
}
}

Reminder: tsconfig.lib.json path do not work in an angular libraries, so after your change you may have to edit manually the import of the assets with relative path


  1. ng build custom-project --prod. It then appear in your dist folder

    Sample Image

Then you could just add what you want in this folder

Additional tips : Use it within your project

Then if you wish to use it into the project it get imported into, do :

Assets, js, styles in angular.json

Add those files into your angular.json file

 {
/*...*/
"assets": [ // Import every assets
{
"glob": "**/*",
"input": "./node_modules/custom-project/assets",
"output": "/assets/"
}
],
"styles" : [ // Only custom css
"node_modules/custom-project/assets/my-css-file.css"
],
"scripts" : [
"node_modules/custom-project/assets/my-js-file.js"
]
}

Js as part of the app.module

You could directly also add js into children file even if I'm not sure if it really lazy loading those files.

example, into your home.module.ts file, import

 import 'custom-project/assets/my-js-file.js'

Android Library assets folder doesn't get copied

The new Android Build System that is based on Gradle supports asset folders in library projects!

With ANT based builds it's still not possible:

Library projects cannot include raw assets. (See docs)

But you could

  • copy the assets manually
  • or even patch the aapt tool (see http://devmaze.wordpress.com/2011/06/26/enabling-assets-in-android-libraries/)

android accessing assets form library project

From Android Docs : http://developer.android.com/tools/projects/index.html

Library modules cannot include raw assets

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

And this is how you can read a JSON from res/raw folder : JSON Parsing in android - from resource folder

Other workarounds :

how to reference an asset in a library project

how to point to library files from component in Angular?

Yes you can use angular.json for this. Here are the docs: https://angular.io/guide/workspace-config#asset-config

Pointing from withing the app to node_modules is not workable since this path is not available in the build app. In this way the assets will be available in the distributable package.

Example from one of my projects:

"assets": [
"src/favicon.ico",
"src/assets",
{
"glob": "**/*",
"input": "./node_modules/package-namespace/package/lib/assets",
"output": "./assets/"
}
],

How to get AssetManager from a library project

We can't add assets folder in lib module

Library modules cannot include raw assets The tools do not support the
use of raw asset files (saved in the assets/ directory) in a library
module. Any asset resources used by an app must be stored in the
assets/ directory of the app module itself.

For More details, you can refer here



Related Topics



Leave a reply



Submit