Android Library assets folder doesn't get copi

2019-01-09 09:34发布

问题:

I am creating an Android library and it has an assets folder with images. When I use it in another project the assets doesn't get copied.

Anyone else had this issue?

回答1:

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/)


回答2:

It is possible to put assets into resulting library jar by small fix:
Put "custom_rules.xml" into you library project home (near the build.xml):

<?xml version="1.0" encoding="UTF-8"?>
<project name="custom_rules">
    <target name="-post-compile" if="${project.is.library}">
    <echo>Post Compile: add assests from ${asset.absolute.dir} to ${out.library.jar.file}</echo>
    <jar destfile="${out.library.jar.file}" update="true">
        <zipfileset dir="${asset.absolute.dir}" prefix="assets" excludes="**/*.java ${android.package.excludes}"/>
    </jar>
</target>

This will pack your library assets into you resulting library jar. As result, these assets will be included into resulting .apk of your application.

Checked on Android SDK Tools Revision 21.1.0 on OSX



回答3:

From Eclipse and ANT you can reference the same "assets" folder from multiple projects. This allows your source tree to have a single copy of the assets files, but have them included in multiple APKs.

See Android: targeted res folders for debugging in eclipse

From ProjectA I was able to reference ..\ProjectA\Assets from ProjectB. On Eclipse (under Windows at least, I've not tried on Linux yet), I had to create a new variable to reference ..\ProjectA and use that variable in "Linked Folder Location". If I attempted to use ".." in "Linked Folder Location" eclipse wouldn't accept it.



回答4:

For those using IntelliJ IDEA, you can make a change to your application module's packaging settings to include depedent assets. Note, this was done on version 14.0.2 of IntelliJ Community edition. My main application module now inherits files form my library project's assets folder as if they were right in the main project!

Right click application module > Open Module SEttings. Select Android tree node on module > Packaging Tab. Check "Include assets from dependencies into APK"

Note: I've never used Android Studio, but I would guess there is a similar setting for this in that application.



回答5:

start by creating android lib project, close after creation. this'll prevert autobuild

use cmd terminal on windows android command is batch file

add tools to path C:\Users\user_bss>PATH=%PATH%;C:\adt-bundle-windows-x86-20140321\sdk\tools

Generate build.xml for ant android update project -p C:\Users\user_bss\Documents\Workspace\SDKAdvanced -n SDKAdvanced

notepad custom_rules.xml add the code from above don't forget end tag

run "ant debug" or "ant release" to build

you'll see classes.jar in bin dir this is your packed lib