Android Library assets folder doesn't get copied
Asked Answered
S

5

16

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?

Sexed answered 4/5, 2011 at 21:3 Comment(0)
S
36

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

Schlicher answered 29/6, 2011 at 22:17 Comment(2)
Just in case it is not clear (as it was not to me): You do not need to configure anything special with Gradle in Android Studio: Looks like the assets of the libraries are automatically included.Filch
@Filch Yes, but I’d like to add that you probably need to declare the dependency in your consuming application’s build.gradle as an @aar to ensure that the dependency is included as an aar not just a jar. This might not be necessary in all cases but i’ve found it necessary most of the time. So to be safe, I’d use the @aar suffix when depending on a library that requires it’s resources to be shared. I think this is required when the library is deployed on MavenOgpu
I
11

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

Iridotomy answered 4/4, 2013 at 5:57 Comment(1)
But this won't do it with Eclipse, or does it?Caldera
F
2

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.

Favors answered 26/2, 2013 at 0:12 Comment(0)
I
1

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.

enter image description here

enter image description here

Isola answered 27/3, 2015 at 16:41 Comment(1)
I know you're not supposed to post "thank you" comments. But, THANK YOU! I just upgraded my IntelliJ and my library Assets vanished.Ronnironnica
C
0

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

Chandless answered 7/4, 2015 at 20:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.