how to access resources in a android library project

后端 未结 2 1263
青春惊慌失措
青春惊慌失措 2020-12-01 23:47

I am building an android library project which need some static resources(images/xml and etc) internally.

Then I wonder where I can put these resources and how to a

相关标签:
2条回答
  • 2020-12-02 00:17

    Then I wonder where I can put these resources and how to access them?

    From Android Doc's

    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.

    Docs Reference Link : LibraryProjects

    Solution : How to access resource files saved in res/ directory of library project from application project

    Note : If you want to access the resources from Library Project itself add the resources to res/ directory not in assets. Because the files in assets won't be bundled with the .jar file. You can access it as usual using the reference generated in R.java file.

    Ex: To access a image from drawable use

    R.drawable.image_name
    

    Does it mean that I have to hold a reference of the Context object anywhere I want to get any resources?

    Really you should hold a reference to context to access the resources

    Update:

    Now Android library project can be generated as aar (Android Archive) file which allows you to bundle the resource files needed for the library project instead of the old jar (Java Archive) file which only allow you to bundle Java classes.

    0 讨论(0)
  • 2020-12-02 00:19

    Or you can use a Linked Folder in Eclipse, and link your assets folder across to your new project.

    0 讨论(0)
提交回复
热议问题