Is it possible to access files (with build action "AndroidAsset") from a monodroid class library in a monodroid application that references the class library ?
I have created an "Assets" folder in the class lib and added a text file with build action "AndroidAsset", but from the app I could not access it via Assets.Open("file.txt");
I was hoping that the Assets from the class lib and the main application could somehow be "merged"...
Yes. Just access them "as normal" via Context.Assets.Open. There is only one source of assets in the application, so any component can access any and all assets, if they have access to an
AssetManager
instance.However, that just takes what you're asking at face-value. Can a Library project use the
Context.Assets
property? Certainly, if you provide it aContext
instance.But can a Library project provide it's own assets for inclusion into the application? No.
Library projects cannot provide assets that will be included in the larger application. Java doesn't support this either, afaik.
Things to remember
1 Build Action is AndroidAsset (Properties)
2 Copy to output directory Do not copy (Properties)
3 Add the file to the Assets folder
Sometimes this will still fail, monodroid bug. The best thing to do is check to see if the files were included correctly.
Also if you try to grab a file off a physical device your testing on, some allow it and other don't. This is easiest to test on the simulator.
No - you a can't access the assets from a class lib - only from the main exe project.
This is similar to the situation with Java Android projects.
One route around this might be to use embedded resources instead - see the answer to this question I asked on xamarin's forums - http://forums.xamarin.com/discussion/186/is-there-a-cross-platform-way-to-include-string-resources-from-class-library-projects - note that I haven't tried this yet
It is possible to access the Assets files from a library: