how to reference an asset in a library project

2020-01-26 05:37发布

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" ?

7条回答
贪生不怕死
2楼-- · 2020-01-26 06:20

Okay. Ive been stressing out and losing sleep about this for a while. Im the type of person that loves API creation, and HATES complicated integration.

There arent many solutions around on the internet, so im quite proud of what Ive discovered with a bit of Eclipse Hackery.

It turns out that when you put a file in the Android Lib's /assets folder. The target apk will capture this and place it on the root of the APK archive. Thus, making general access fail. This can be resolved by simply creating a Raw Java Library, and placing all assets in there, ie (JAVALIB)/assets/fileX.txt.

You can in turn then include this as a Java Build Path Folder Source in Project > Properties > Java Build Path > Source > Link Source.

  1. Link Source
  2. Click on Variables. and Add New Variable, ie VAR_NAME_X. location : ../../(relative_path_to_assets_project)
  3. Click Ok

Now, when you build and run your app, the assets folder in the APK will contain your (GLOBAL Library) files as you intended.

No need to reconfigure android internals or nothing. Its all capable within a few clicks of Eclipse.

查看更多
Explosion°爆炸
3楼-- · 2020-01-26 06:23

I found this older question, it might help you, too.

This is the official way Google uses to archive this (from the above post): Link

查看更多
Rolldiameter
4楼-- · 2020-01-26 06:25

You can achieve this by creating a symbolic link in the project's asset folder that points to the directory in the library project.

Then you can access as below:

webview.loadUrl("file:///android_asset/folder_in_a_libary_project/info.html", null);
查看更多
淡お忘
5楼-- · 2020-01-26 06:26

This is now possible using the Gradle build system.

Testing with Android Studio 0.5.0 and v0.9 of the Android Gradle plugin, I've found that files such as

MyLibProject/src/main/assets/test.html

are correctly packaged in the final application and can be accessed at runtime via the expected URL:

file:///android_asset/test.html
查看更多
祖国的老花朵
6楼-- · 2020-01-26 06:32

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.

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

查看更多
Ridiculous、
7楼-- · 2020-01-26 06:34

If you want to use a setup where multiple derivate products are created from one library you might consider using svn:externals or similar solution in your SCM system. This will also do the trick that static assets like online help may be versioned seperately from the android source code.

查看更多
登录 后发表回答