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?
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/)
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
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.
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.
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
Related
I have setup an android test project for my application in ECLIPSE. My test project depends on some external libraries for Mock behavior.
I can add dependencies to my test project in following ways:
If I put all the dependency jar files to my 'application\libs' folder, but they will be bundled with my application APK, irrespective of that my source does not depend on the these jars.
If I put all the dependency jar files to my 'testProject\libs' folder, run test from ECLIPSE and success but logcat keeps givieng me warning of not finding classes. and i have read on SO that ant ingnores 'test\libs'? so is that the same case with ADT also.
So is it good practice to put external dependencies of test project in 'test\libs' only?
NOTE: In both the cases my test get run successfully But I am curious to get the right way.
If you are adding external dependencies in android libs folder is most recommended. Just follow this following steps:
1.Create a folder called libs in your project's root folder.
2.Copy your JAR files to the libs folder.
3.Now right click on the Jar file and then select Build Path > Add to Build Path, which will create a folder called 'Referenced Libraries' within your project.
Third step gives reference to your libraries file.
is there any way to use .aar library (with resources) in eclipse ant project? I know that there is no simply way to use .aar format like in gradle, but maybe there is some workaround to unzip it and import it manually?
This solution was helpful for me
EDIT: A .aar file is simply a zip file with the following contents:
AndroidManifest.xml (Required file)
classes.jar (required file)
Res / (compulsory folder)
R.txt (Required file)
Assets / (Optional folder)
libs/*.jar (folder option)
jni//*.so (optional folder)
proguard.txt (optional file)
lint.jar (optional file)
You see, within the .aar file you have everything you need to operate the library in eclipse.
to create library:
CREATE a new project (hereafter library project ) to your workspace. Do not forget to mark it as a library.
Leave empty src folder library project .
.aar Decompresses the file. You can rename it to .zip and unzip or
use any tools.
Copy the file classes.jar into the libs file folder library project .
Res folder replaces the library project with .aar res file folder.
You've created the project that contains almost everything you need.
Now let's see how to configure your project to reference the library project.
In the project you want to use the library (henceforth, project goal ) added as the dependency library project .
Open AndroidManifest.xml .aar within the file and make sure to copy
everything it takes (permits, activities, services, receivers ...)
in the file AndroidManifest.xml project objective .
If there is, copy the entire contents of the folder assets .aar file in the assets folder target project .
If there is, copy the entire contents of the file .aar libs folder
in folder libs target project .
Make a Clean the workspace and recompiled.
http://www.iphonedroid.com/blog/utilizar-ficheros-aar-en-eclipse/#.Vh3krye1Gko
Indeed, aar files are just archive files. So you can unzip it and find jar files along with ressources files. The question have already been partially answered here:
https://stackoverflow.com/a/21485222/1836870
If you want to get the ressources inside your jar, you could try repackage like it's suggested in this answer:
https://stackoverflow.com/a/21417599/1836870
The brunt of the work can be done by a wonderful script called deaar. The gist explains things, but basically you run:
ruby deaar.rb [path/to/aar] [output_directory]
It outputs an almost ready to use library. Next, you need to run:
cd [output_directory]
android update lib-project -p . -t android-xx
Replace xx with the Android version you're targeting. Now put that directory where your build.xml and project.properties are. Finally, add a line like this to your project.properties:
android.library.reference.1=./output_directory
Replace the directory name with the one you created. You can use ".2" and so on for additional libraries. And that's it!
Twitter Fabric(Crashlytics) has a kits-libs plugin for dependencies management http://docs.fabric.io/android/fabric/eclipse.html.
And there's another gradle plugin project https://github.com/ksoichiro/gradle-eclipse-aar-plugin.
This is an Android applicatoin.
I add a .so native libray, in the folder:
Libs/armeabi/libxx.so
However, I cannot commit it to the repository.
When I right-click "commit", there is no change to reflect that I have added a .so native library file.
How can I solve the problem?
OK, I find one solution. Right click the file, choose Team -> Add to Version Control
Then the file will be able to be commited.
Subversion ignores .so files by default. You could try changing settings at Preferences > Team > Ignored Resources (Ignoring Eclipse project files in SVN project). If not, just add the .so file via the command line.
I am going to create a library project which has several dependencies (jar files).
I am confused because I've seen some android projects which has LIB or LIBS folder under project root folder (together with bin,gen,res,src.etc..)
Even I add LIB or LIBS folder, then put the jar files on it. Still need to set the project properties in order to recognize the added jar files by doing Project Properties => Java Build Path => Libraries tab => Add JARs... button.
I am using Eclipse with Android Plugin.
When I add LIBS, it will appear the Android logo on the top of the LIBS folder. It is recognized by the plugin but still need to set it in the project properties (same the above adding JARs)
Which is the correct directory structure for Android Project?
If you use the Android command line to create your project, it'll default to libs/. It turns out that the Android Ant tasks are set to look in libs/ by default.
See $ANDROID_HOME/tools/ant/main_rules.xml:
<property name="jar.libs.dir" value="libs" />
If you just care about Eclipse, you can use whatever you want.
In newer revisions of ADT (revision 17 onwards), the Ant-based build system and the Eclipse plug-in are aligned so that they behave the same way. This means, by default, only the jars that are present in the libs folder are included in the final apk file. These jars would automatically appear under "Android Dependencies" in your Eclipse project.
For more details please refer here. http://tools.android.com/recent/dealingwithdependenciesinandroidprojects
It doesn't matter actually. It can be lib or libs. Whether jar files are in lib or in libs, you just need to do right click on project, select "Build path => Add to build path" , thats it.
Citing: http://tools.android.com/recent/dealingwithdependenciesinandroidprojects
If you are still referencing jar libraries manually instead of putting them under libs/ be aware of the following:
If the project is a Library project, these jar libraries will not be automatically visible to application projects. You should really move these to libs/
If the project is an application, this can work but you must make sure to mark the jar files as exported.
I have an existing project that builds fine using my IDE. I'd like to use the "android update" command to generate an ant buildfile for this project.
The buildfile is generated fine, but the build fails because it's not building with some jarfiles I have in my libs directory.
I'd like to figure out the proper way to tell ant to build with some external jar files in my libs directory. How should I do this? Is it a property in build.properties? Do I need to modify build.xml somehow? Or is there a different solution entirely?
but the build fails because it's not
building with some jarfiles I have in
my libs directory.
And your error message is...what? I suspect you may be misinterpreting the error message.
I'd like to figure out the proper way
to tell ant to build with some
external jar files in my libs
directory. How should I do this?
Just put them in libs/, as Ant will add everything in there to your build path. See this project, and this project, and this project for examples.
I spent some time trying to get the Facebook API to work with ant. The trick for me was to add this to my default.properties files.
android.library.reference.1=../Facebook
Where ../Facebook contains AndroidManifest.xml, etc. The real key being the relative path. Don't use an absolute path because Ant seems to treat your project directory as the root.
This should hold true for other library projects that you are including from source code.
I was dealing with similar issue. I'm building Android project on Jenkins using standard Ant build.xml (generated by Android SDK). I also have reference to another Java project with some shared domain classes. In Eclipse there is no problem with it. The domain project is a project reference. However on Jenkins this domain.jar is built by Maven and it was not accessible by Android project.
I have finally solved it by adding this at the end of build.xml:
<target name="-pre-build">
<copy todir="${jar.libs.dir}">
<fileset
dir="../path-to-another-project/target"
includes="*.jar" />
</copy>
</target>
This copies all jars from the target directory of another project into "libs" directory of my Android project. The -pre-build Ant target is automatically called before Android compilation starts.
I agree with Mark, however, if you're planning to modify your build script further - than you need to make it custom. Bring tasks from android/platforms/android-PLATFORMVERSION/templates/android_rules.xml to your build.xml and modify whatever you want to modify. Including location for external libs.