Build library project - android

all.
I've created an android library project and it's works perfectly when i reference it from main project. But when i build the library project apart it doesn't contains R.java and resources. Is there way to build a library project with resources and R.java?

It's not possible now.
Now we can create a binary-only library project via the following steps:
Create an Android library project, with your source code and such –
this is your master project, from which you will create a version of
the library project for distribution
Compile the Java source (e.g., ant compile) and turn it into a JAR file
Create a distribution Android library project, with the same
resources as the master library project, but no source code
Put the JAR file in the distribution Android library project's libs/
directory
The resulting distribution Android library project will have everything a
main project will need, just without the source code.
There is some restrictions in this solution:
We still have to ship the resources.
We have to rewrite our code to avoid using R. values, as they
will be wrong. We will have to look up all resource IDs using
getResources().getIdentifier() and/or reflection.

I use Eclipse and never manually build my Android Library Project independently, but I think the development considerations stated on official dev guide here should answer your question:
Each library project creates its own R class
When you build the dependent application project, library projects are compiled and merged with the application project. Each library has its own R class, named according to the library's package name. The R class generated from main project and the library project is created in all the packages that are needed including the main project's package and the libraries' packages.
Update with Another note quoted from the official dev giude Library Projects:
However, a library project differs from an standard Android application project in that you cannot compile it directly to its own .apk and run it on an Android device. Similarly, you cannot export the library project to a self-contained JAR file, as you would do for a true library. Instead, you must compile the library indirectly, by referencing the library in the dependent application and building that application.

Related

Importing android gradle project into eclipse

I need to import this library project into my eclipse. While i am trying to use this project it have some error. Can you help me on this. https://github.com/daimajia/AndroidImageSlider
That project is created for use with Gradle for Android. Eclipse does not support that yet. It is also distributed as an AAR, which Eclipse does not support. There are recipes for converting AARs into Eclipse-friendly Android library projects that you can try. Otherwise, you will need to reorganize the project code yourself to support the classic Eclipse-style project structure. Mostly, that will involve moving the contents of library/src/main/ into a regular Eclipse Android library project:
The res/ directory and AndroidManifest.xml file would go in the library project root directory
The java/ directory would be renamed src/ and also go in the library project directory
However, you will have to repeat this process each and every time the library's author updates the library, at least for those updates that you are interested in.
You may wish to see if there is an alternative library that meets your needs but is better packaged for use with Eclipse.
Basically there are some changes to be done to eclipse project before importing it to eclipse like src folder.
The project you posted may contains many error since it have two more android project dependency.
I tired to convert above project with its dependency :
Checkout Complete Source Code
There is one more project lib u need to add LIB

Library Project Supporting Eclipse and Android Studio

I'm working on a library project that provides access to a service. We started the project few months ago and we were supporting Eclipse only (since Android Studio was a prewview edition).
Now that Android Studio has become a "beta" version, and its popularity has increased greatly, we had the intention to support it as welll, but we are facing the problem of how to support both "styles" with the same base (project structure and code).
The library we are building has a UI that forced us to have the library as library project instead of just a simple jar. We have this project working with ANT to build the required files (jars) and packaging everthing in a library project.
Android Studio now introduces the .aar library files, that can also contain UI.
So our problem is finding examples of other library projects containing UI that are also supporting both IDE's. Wondering if someone else have face this same situation.
Is is possible to have a Library Project to support both IDEs? (Eclipse and Android Studio)
Thanks to #CommonsWare. When I looked at your projects I realize that we didn't need our project to be "Android Studio compatible". Since we wanted to share the project as an .aar file, I had only to make a build.gradle at the root of my library project and add the gradle folder (containing the gradle wrapper jars).
In this way I can use the console and create a .aar file using "./gradlew aR" command. Now I can distribute the library project for Eclipse users or the .aar file for Android Studio users.
I'm testing the .aar file, and the only problem I have right now is that classes inside a jar file within libs folder inside the .aar file are not recognized, just the classes present inside "classes.jar", but I think I would create another question here in SOF since is not relevant for this question.

Don't want to manually copy android JNI(.so) and Jar libraries to App project each time

I'm building and Eclipse to make an Android library I want to distribute to developers.
TedLibJni:
It uses the Android NDK and so it compiles down to a .so file.
TedLibJar:
It also has a Java interface that binds the then extern'd calls in the JNI, so it has a Jar library associated with it.
TedDroidApp:
The concensus is that I need to manually copy both TedLibJni.so and TedLibJar.jar to lib/armeabi of this App for it to be used.
Question: Is there any way that TedDroidApp can pick up the externally located .so or .jar files? It seems crazy that I would have to manually copy and paste these files accross each time I iterate them.
Use an Android library project for the JNI code and the JAR. You can then attach the Android library project to other projects. With the new Gradle-based build system, you can package the library project up as an AAR and obtain it from an artifact repository as well.
CWAC-AndDown, my wrapper around the C hoedown library, works this way.

How to distribute an Android Library

I've been spinning a jar for android library project and including this jar in my other apps. But on developer.android.com, I see this statement that I can't distribute a library in a jar:
You cannot export a library project to a JAR file
A library cannot be distributed as a binary file (such as a JAR file).
This will be added in a future version of the SDK Tools.
I really don't understand what does that mean.
It is possible to create an Android library project that does not
include source code. The limitations are:
You still have to ship the resources.
You have to rewrite your code to avoid using R. values, as they
will be wrong. You will have to look up all resource IDs using
getResources().getIdentifier() and/or reflection.
I have the instructions in The Busy Coder's Guide to Advanced Android
Development (http://commonsware.com/AdvAndroid), though the
instructions are new enough that none of my free versions have them.
Quoting some of the instructions from the current edition:
"You can create a binary-only library project via the following steps:
Create an Android library project, with your source code and such –
this is your master project, from which you will create a version of
the library project for distribution.
Compile the Java source (e.g., ant compile) and turn it into a JAR file
Create a distribution Android library project, with the same
resources as the master library project, but no source code
Put the JAR file in the distribution Android library project's libs/
directory
The resulting distribution Android library project will have everything a
main project will need, just without the source code."
Personally, I'd just wait a bit. I am hopeful that the official
support for library-projects-as-JARs will be available soonish.
It means that (at the current time) you must distribute your entire project folder. Rather than just a jar file like you can for java libraries.
When someone wants to use your library they will import your project into eclipse, and then in project properties->android they will add your project as an android library.
A few common ways used to distribute a Library project are by using git, or zipping your project folder and making it available online.

Is it possible to create jar that includes android functions?

As the topic indicates I would like to create a jar library that uses some android functions (no layouts) and that will be included in an Android project.
Is that possible and how?
From the research I've made I managed to include a simple jar file that uses pure Java (JAVA SE 1.6), but
when I tried creating a jar file I encountered the following exception when I tried to run the Andoid app: FATAL EXCEPTION: main
java.lang.NoClassDefFoundError: mylib.pleasework.amen
I tried including android.jar in my library and removing the java library, so that the jar file is build against android sdk, but it didn't work.
I tried including the jar file under a /libs folder as it is said to be the correct way to import jars in android projects from ADT v17 and after, but that didn't work either.
The jar I want to create will not use any resources (xml layouts, strings.xml) just Log.d and WifiManager.I am aware of Android Library Project but my library source is sensitive and I am afraid that it won't be safe if exposed in a Android library project. I was thinking of creating a jar and using ProGuard ( http://developer.android.com/tools/help/proguard.html ) obfuscate it.
I think I mentioned everything. Any help will be appreciated.
Thanks,
Thomas
As the topic indicates I would like to create a jar library that uses some android functions (no layouts) and that will be included in an Android project. Is that possible and how?
Use the jar command, or the <jar> Ant task. I am sure that there are ways to export a JAR from Eclipse, but I personally have never used them.
For example, in this GitHub repo I have a reusable component and a sub-project that is a sample app. My build.xml for the repo contains the following custom task:
<target name="jar" depends="debug">
<jar
destfile="bin/CWAC-WakefulIntentService.jar"
basedir="bin/classes"
/>
</target>
This generates a JAR file, that other Android applications can use by adding to their libs/ directories.
I am aware of Android Library Project but my library source is sensitive and I am afraid that it won't be safe if exposed in a Android library project.
It won't be safe exposed as a JAR, then, either. You can create an Android library project for public consumption that replaces the src/ tree's contents with a compiled JAR in libs/ in the library.
The way I did it in the end was: to create an Android Library project (check isLibrary checkbox in project properties) export it through Eclipse (right click on the project->export->jar file, careful to deselect all resources - res folder, androidmanifest.xml, *.png etc) and put it in the project you want by importing it under /libs folder. I don't know if this is the best solution but it worked for me.Used ADT r20, Eclipse 3.7.1, Android api level 7

Categories

Resources