Android and Eclipse - referencing Library - android

What is the correct way to reference a library?
I start a new Android application using Eclipse.
I add a new Library application.
I add the project in the Java Build Path for the main application.
This compiles fine, but at runtime I get an error because the "library" file is missing from the main application, so my question is, what is the "correct" way to get the library into the main application?
I am used to Visual Studio, and referencing another project automatically ensures that the reference copies the required DLLs to the project BIN folder.
I understand that the library file's JAR/APK must be in the LIB folder, but does Eclipse do this for you when you reference it? It appears not - so do I import?

First you need to build your library project, and only after that add it into main project as library. Please follow this post.

Related

What is the correct way to add an Android Library to an Android Project?

I am trying to add android-support-v7-appcompat to my newly created Android project.
Following the Support Library Setup Documentation (developer.android.com/tools/support-library/setup.html), section Adding libraries with resources, I have created an android-support-v7-appcompat project, before adding it as an Android dependency.
EDIT: Here are more details on the process I followed to add the library project as an Android Dependency to the main project:
In the Project Explorer, right-click your project and select Properties.
In the category panel on the left side of the dialog, select Android.
In the Library pane, click the Add button.
Select the library project and click OK.
However, even if the dependency is correctly recognized, Eclipse still sees the compatibility references, such as ActionBarActivity, as errors, forbidding me to build the project. (See picture here).
To solve this problem, I tried the solutions accepted by similar questions on SE, i.e. adding android-support-v7-appcompat to the Project's Build Path, which successfully silents the errors that prevented me to compile earlier.
However, now when building and deploying the project, I get an error: the app crashes at launch, and the Console displays an error message: Could not find android-support-v7-appcompat.apk! (See picture here).
To resume, with the first configuration I am not able to compile with Eclipse, while with the second configuration I can't launch the application.
Therefore, I have two questions:
First, how should I be adding a library to my project, so I can at the same time build my project and be able to use the library's additions in my code in Eclipse?
Secondly, I feel like I am missing theoretical knowledge on how Eclipse handles dependencies.
What is the difference between adding a library as Android dependency vs Java Dependency / vs adding the project or its jar to the Build path?
First, how should I be adding a library to my project, so I can at the same time build my project and be able to use the library's additions in my code in Eclipse?
Your first approach should be fine, which would indicate that there is some other problem. For example, there may be errors in the appcompat library project that are preventing it from being compiled. However, given the error from the second approach, either you are running the appcompat library (which will not work) or something else is more fundamentally broken in your project setup, where your app thinks that it is supposed to build android-support-v7-appcompat.apk, when there is no such APK.
What is the difference between adding a library as Android dependency vs Java Dependency / vs adding the project or its jar to the Build path?
Never manually modify the build path in Eclipse for an Android project. While that will satisfy the compiler, the contents of the JAR(s) will not be packaged into your APK for use at runtime, resulting in runtime crashes (e.g., VerifyError).
"Java dependency" is for pure Java source code. Not only do you not have the source code to appcompat from the SDK, but an Android library project includes Android resources (and, at least on Android Studio, also assets and an optional manifest file).
For Eclipse, library projects are attached via the approach you describe as the first approach in your question. For Eclipse, plain JARs are just dropped into libs/ in your project root, and those are automatically added to your compile-time and runtime classpaths.

Trying to add facebooksdk.jar file to my project

I have installed facebookSDK and the sample apps. The sample apps all copilr and run fine.
I tried to make my own app. The only thing I did was creat a new android project, test it, runs fine. Then I added the facebooksdk jar file. Now my project no longer runs and I get a error that says
“jar miss match fix your dependencies”
The jar file seed to load in correctly, if I click on android dependences I see
Facebooksdk.jar ../programming/facebooksdk/……
I tried to see what was different from my project ans facebook's sample code.
Under android dependencies it had the same line I had Facebooksdk.jar ../programming/facebooksdk/……
And also had another jar file called android-suppert-v4-.jar that I do not have.
Other then that both the sample code and my test project seem to be the same.
there is a jar that exists both in the facebook sdk project and in yours.
This is caused by having a duplicate jar -- a .jar file of the same name that appears in both projects, but is not the exact same file. In this case, it is probably android.support.v4.jar, since Facebook uses it, and your project likely does too.
Check your dependencies carefully (and check your assets, bin, etc. folders) for the android support library in your own project -- even if you didn't add it yourself, the dependency could have been added by default, especially if you used the wizards provided with ADT for eclipse.

Android - Taking classes from my project and making that into a jar

Alright so I have a android project and It is working fine. Now i want to take some of the classes in it and put it into a jar and then include that jar in my project. I'm not exactly sure how to do that. The files have R in it and not sure if they will affect anything. Im keeping the xml files in the original project, so basically all i want is the .java files be put into the jar and then referenced.
Is there anyway to do this?
Make a new project for your classes and mark it as library, in eclipse go to
Project properties -> Android and then check Is library
To use your library in a new project, again go to
Project properties -> Android and click Add button and select your library.
You might need to have both projects in the same workspace.
The resources files (i.e. R) defined in you library will be accessible in your new project, so you can use it as if it is defined in your new project.
You can create a separate library project. If you are using the command line, create a directory for the project, cd to it, and use
android create lib-project
The help output will tell you the flags this command requires.
After you move the classes to the library project, cd back to your main project and type
android update project -p . -l <path-to-library-project>
Now when you compile your main project, it will also compile the library project, create a jar file, and import its classes into your main project. For more details about library projects, check out Managing Projects. If you are using Eclipse, check out Managing Projects from Eclipse with ADT.

Android app with eclipse project dependency

I have an Android app which uses a jar library generated from another Eclipse project.
But is it possible to directly reference this project instead of using an intermediate jar ?
When I do that, the build succeeds but I get VerifyErrors at runtime saying the classes of my external project can't be found...
Yes, you can directly reference this project by setting it as library. To do so:
Right click on your desired library project, choose properties and mark checkbox 'isLibrary'
Then just simply right click on you project, go to properties and add those library project
Yes, ADT supports libraries in form of shareable source code and resources, not just .jar. Please see Android Developer website, especially Managing Projects -> Library Projects article.

Referencing a library project in Android

If I decided to use a library project in mine I still have to keep this library project in my workspace. Is there a way to avoid this? Also, does it mean I have to deliver an addition folder of library project with a folder of mine?
Library projects works like a charm, but of course you need to keep them open in the workspace as everytime eclipse builds your project it needs to access all the stuff from the library project.
If you don't want to keep this project open AND you don't plan to change it a lot AND it's not an Android project, it's a JAVA commom project you can build it once and instead of using a library project reference a JAR in your android project
If you want to do this outside of Eclipse, (say you're building from the terminal with ant, ie. in continuous integration, or if you just hate Eclipse.) Just place the library project in an adjacent directory. In your main project, edit project.properties to include:
android.library.reference.1=../LibraryFolderName
If you have more than one library to add, just number them accordingly, ie.
android.library.reference.1=../LibraryFolderName
android.library.reference.2=../AnotherLibraryFolderName

Categories

Resources