How to handle libraries used by a referenced library project? - java

I installed a bunch of updates via Android SDK Manager this morning including Android SDK Tools 22.0.1. This has seemed to be disastrous so far. One disaster is handling libraries. Here is the scenario:
I have a project projFoo that uses a library project libFoo. libFoo uses a dozen or so libraries or projects. In other words, libFoo has the following in its Java Build Path:
projA, projB, projC,...
lib1.jar, lib2.jar, lib3.jar ...
I used to need only adding libFoo to the build path of projFoo, and everything was working fine until I installed the updates.
After the updates, I have been adding projA, projB, lib1.jar one by one to projFoo's build path even though projFoo does not use them directly. Without doing this, java.lang.NoClassDefFoundError would occur when I run the app.
I have cleaned all projects and restarted Eclipse many times. I also checked those referenced projects and libraries in Order and Export of libFoo, but adding those references still seems to be necessary. Is this the right way to handle this?

In the order and export tab of Java Build Path make sure Android private Libraries and Android Dependencies are checked.
Any libraries in .jar format should just go in the libs folder of your project. There's no need to add them to the build path.
Any libraries that are a project can just be added to the Java Build Path Projects tab, or if it is an android project, you can add it to the Library section under the Android pane.

Related

Creating my own Android library and exporting it to .jar

Before you say that this question was asked. I want to say that I have read through most of the questions and they give just workarounds for this solution and other ones are out of date.
My question is, how create Android library in Android Studio, so I can export it to closed .jar with no sources and distribute it to other developers.
When I click New Module -> Android Library, it creates me folder under my current project. I don't want that.
I want to create library from the beggining (where is option for this in Android Studio?), so it is standalone project which later I can export to jar.
Android Studio doesn't have UI to allow you to create an Android Library as a new project. To do this, you'll have to one of a few different routes.
Before you start, I'd encourage you to think about packaging your library as an .aar instead of a .jar, since you say it will be calling Android APIs. The reason for this is that .aar is a richer format for Android projects and allows the archive to include manifest information, such as the minimum required SDK and such -- it will go farther to ensure that your library is compatible with the environment it's being included in. The disadvantage is that only the Gradle build system supports .aar files, so Eclipse users wouldn't be able to use the archive.
Though you're not using Android resources, if in the future you do need to include resources in your archive, you'll be set up.
There's more information on .aar here: http://tools.android.com/tech-docs/new-build-system/aar-format
If you want to package in .aar format, you could go a couple routes:
You could create a project with the New Project wizard. This will initially be set up to make a full-fledged Android app that compiles to an APK. You can go into the app module's build.gradle file and change this line:
apply plugin: 'com.android.application'
to:
apply plugin: 'com.android.library'
It will now build an .aar. However, this project will still have resources in it, and maybe layouts and activities depending on what options you chose in the wizard, so you may have to rip out what you don't want.
The alternative is you could start with a blank slate, crafting your own build.gradle file from scratch that uses the com.android.library plugin, and fleshing out the source directories. This will be more cumbersome, but as with all difficult activities, you'll learn a lot and it will build character, or you can at least tell yourself that as you're puzzling things out.
If you want to package as .jar instead of .aar, then again there are a couple routes you could go. For this type, if you want maximal assistance from the UI, you could start a new Android project and add a plain Java module to it. You can either remove the unwanted Android module from the project, or you could even leave it alone; the build process will output a .jar for your plain Java module in any event.
Your plain Java module will need to depend on the android.jar archive from the appropriate platform in your SDK.
Alternately, you could build this module up from scratch, authoring the build.gradle file and setting up the sources yourself, and then you could import that into Android Studio as a new project.
Actually it's a bad idea to use your IDE as project management/build system
Take a look on gradle or maven
also take a look to
http://tools.android.com/tech-docs/new-build-system/user-guide
however, if you want do this in ide:
File -> Project Structure. in opened window add jar in Artifacts tab
Build -> Build Artifact.
https://www.jetbrains.com/idea/webhelp/packaging-a-module-into-a-jar-file.html
it's shouldn't be specific for android lib

android-support-v7-appcombat jar mismatch and app crashing after Android 4.4 SDK update

I have been successfully using android-support-v7-appcompat library for last few months in my app to support action bar in older devices. Recently I have downloaded the Android 4.4(kitkat) updates with system image and SDK platform from the SDK Manager. I also added targetSdkVersion to "19" in manifest file. But my project was showing error as 'android-support-v7-appcompat jar mismatch. Fix your dependaecies.' Then I added the jar from /extras/android/support/.... to the project as an external jar using build path menu > add external jar. Now there is no error in the project and android-support-v7-appcompat.jar & android-support0v4.jar file is showing twice under 'Referenced Libraries' folder and also once under 'libs' folder. The project is not showing error and running, but crashing in different activities.
Previously I imported the android-support-v7-appcombat library as an external project in the workplace. I can see error in that project. Please suggest what should I do now. Also, android-support-v7-appcompat.jar umder Android Dependencies in Java Build Path showing error.
The error I see is:
Found 2 versions of android-support-v7-appcompat.jar in the dependency list,
but not all the versions are identical (check is based on SHA-1 only at this time).
All versions of the libraries must be the same at this time.
Versions found are:
Path: C:\<project hierarchy>\libs\android-support-v7-appcompat.jar
Path: C:\Users\....\sdk\extras\android\support\v7\appcompat\libs\android-support-v7-appcompat.jar
Jar mismatch! Fix your dependencies
After getting idea from different forum, I removed all the android-support-v7-appcompat libraries from different places, and imported android-v7 support library again in the project. Still it's showing error. Should I also delete android-support-v4 libraries from buildpath and libs/private librariry/referenced library folders?
This error occurs when there are multiple and different instances of the same library are found in the same project, or libraries used by it. A very simple way to resolve this is use the common lib at both the places. To do so:
Just copy your C:\Users....\AppData\Local\Android\android-studio\sdk\extras\android\support\v7\appcompat\libs\android-support-v7-appcompat.jar
and paste it in your libs folder, replace the old one with this. Clean the project, and it shall compile now..
Edit 1:
Basically the idea is if you are using the same libraries, it shall be the common jar, or the same jar.
In your case if you are using the appv7 as an external project, please go to libs folder of your project copy android-support-v4.jar, and paste into the libs of android-support-v7-appcompat project.
Also, make sure if there are other external jars or support libraries used by you, they shall be the similar jars in all the projects you are using.
I hope it helps!
I had exactly the same problem after installing all the updates mentioned in the question. I am sure there are different ways to solve this problem. Here is the way I used:
Previously, when an activity was created, the following project was automatically generated by the wizard:
androi-support-v7-appcompat and this project was added to the build path.
After all the updates, the following equivalent is generated when an activity is created:
appcompat_v7 with the jars of the same names.
I deleted androi-support-v7-appcompat and made sure the projects use appcompat_v7 to replace androi-support-v7-appcompat in their build paths. This made hundreds of errors generated by the duplicates go away.
I am glad that i finally found the answer. I was encountering the same problem again and again and i was like fed up.
Here is the answer to your question.
whenever you get such an error just don't touch anything other than libs folder. Just go directly into the libs folder and right away delete the android-support-v4.jar. and clean build your project. You will be relieved to get rid of errors.
Hope its useful to you.
Keep coding..
I have actually figured out the solution by trying different options suggested from different forum. I had manually change the android-support-v7-appcompat project's(which was added as an external project) project.properties file and had to set 'target=android-19'.

referencing (pure java) project in Android project (Could not find class)

I am trying to reference a pure java-Project in my Android-Project -> The Java-Project has a whole bunch of classes I need to use. Oh, and as the first response pointed out: I am using eclipse, yes :)
Only Problem is: I Always get Could not find class 'XXX', referenced from method com.example.helloworld.MainActivity.onCreate. I seem to have missed SOME step or error...?
What I already did:
The Project is added as Project into the Java Build Path (logically nessecary)
It is marked in "Order and Export" in the Java Build Path and pushed to the top (this solved the problem for someone else here when dealing with Jar-files)
Ir is marked in "Project References" on Project settings.
I added the folder where the relevant class is under "Libraries" in build Path... I am not sure if that should be nessecary.
The Java-Project I reference has a whole load of Jars, but if that is the reason, should I not get a different errormessage?
I had the same issue and after some hours of frustration and search I have finally found the answer here:
Android, class not found from imported jar file
Basically, the issue was that the referenced pure-java project or the generated jar was built with Java 1.7, and Android projects are set to use 1.6.
Two ways to do this.
Jar that java project and copy the jar to the libs folder of your Android project.
Add the project as a dependent project to your Android project
Both work wonderfully.
But mind you, this pure-java project must add android.jar and not things like rt.jar :). Else you will get Dalvik exceptions.
Edit :
Dont forget to refresh and Project - Clean your android project.
Two things to check (you mentioned you are using Eclipse):
If your android project needs to use the pure-java project's JARs, check that those JARs are being exported in the build path options of the pure-java project. This is in the build path of the pure-java project you are trying to include, not the build path of the android project that is trying to include it. Under "Order and Export" tab for the pure-java project, note the comment "Exported entries are contributed to dependent projects". In this case, your android project is the dependent project and your pure-java project needs to do the contributing.
Make sure the pure-java project is actually opened in the IDE in addition to your android project. Probably trivial but sometimes overlooked.
If answer does not suffice, you may wish to mention if the missing classes are in .java source files, or if they are only found in JARs in the build path of the pure-java project
Edit: To further track down the issue, please confirm the following to make sure I understood your question:
The pure-java project does successfully build in the IDE
The 'class not found' error is one you are getting when you try and -build- your android project and not when you try and -run- it.
CLASSPATH (aka build path) can be nasty to untangle. If including the project is not working (assuming it does build), you could try building a JAR of your pure-java project and copying (and including) that and all the other JARs into your android project.
The android SDK does certainly complicate the build environment. One way to find out whether the problem is (A) your android project setup) OR (B) the way the pure-java project is packaged) is to create a different pure java project and try including the first one, preferably using the -exact- same line of code that gives you the build error in the android project, if possible.

What is the best way to include another (non-android) project in an android project under eclipse

I'm new to android programming. I'm trying to include code from a non-android project in an android app. This contains shared code used by a lot of my other non-android apps.
I was wondering what the best way to do this is from a code maintenance perspective. The shared code that I want to include does get modified from time to time and I want to keep the process of updating any apps I write as simple as possible (automatic if at all possible).
Am I better to build a .jar file containing the shared code and copy this to my app (eg: using an ant script) or is there a more streamlined approach.
I specifically want to avoid turning my shared code project into any kind of android project.
Make a jar and add it to the android project's build path. Whenever there is an update to jar, you would need to update it in your project and update (increase) version of your app to automatically allow users to download and update the update (android market would take care of that for you).
Its the most widely and maintainable way.
Hope this helps.
Once you have your .jar, you simply have to add it to your project (or update with the new .jar file if this library gets updated).
Once it is in you project (let's say under the /lib folder,
right click on the lib folder -> select build -> add to build path.
Unfortunately, I'm pretty sure there's no way to make an automatic update.
In the projects' properties go to "Project References". It will should you the other open projects and you can click whichever you want.
I haven't tried it, but if you change the non-Android code and run the Android project, I would expect the non-Android project to be recompiled automatically.

Android Eclipse Classpath - want to add classpath container path but eclipse won't let me

I'm using Eclipse to learn to develop Android applications in Java. I haven't used Eclipse before.
The project I'm trying to use (supplied by OReilly as part of 'Android Application Development') is MJAndroid. When attempting to run the project, the Run fails, and the Problems tab mentions com.java.Object can't be found, and Eclipse asks me to check the build path. Clicking
Properties -> Java Build Path -> Libraries, the message 'Unable to get system library for the project' is displayed.
On a working project, Properties -> Java Build Path -> Libraries has an entry for Android 1.1, which if I click Edit, has the classpath container path of com.android.ide.eclipse.adt.ANDROID_FRAMEWORK.
It seems a simple matter of adding the correct container path to my non-working project. However Eclipse seems determined to make this as difficult as possible. No matter what I chose to add - jars, externals jars, variables, libraries, class folders, external class folders, nothing seems to take the form of 'com.android.ide.eclipse.adt.ANDROID_FRAMEWORK' that the 'Android 1.1' entry on the working app has.
How can I add this namespace to my project so it resembles the working one?
I'm quite sure it's a problem with Eclipse's somewhat odd user interface. Frankly there' nothing I'd prefer more than a file to modify and set such information - my background is in Python, and the whole eclipse environment seems an unnecessary burden.
I had faced the same issue when I imported a sample code downloaded from the internet. I am using android sdk 1.5 with 0.9 ADT plugins. Here is a simpler way to fix the andoid library reference issues
Right click on the project which has
issues and select properties
Select the appropriate Android build
(1.1, 1.5 or 1.5 with google api) and
say ok
Again right click on the project and
select "Android Tools > Fix Project
Properties"
Fix the imports once (if required)
Refresh the project and you should be
ready to go without any manual
editing
I faced this same problem after importing a project through GIT. The problem was that I didn't have the same target android platform installed, and the build path somehow got corrupted.
The first obvious thing i did was changing the target sdk in the project.properties, but even after cleaning up the project and Android Tools > Fix Project Properties, it didn't help and I was still getting the build error.
My solution after wasting close to 1 hour trying to figure this out?
RESTART ECLIPSE
Everything worked fine after that. Eclipse is pretty fickle. Only through years of experience you'll then understand her well. :)
Had the same problem and it turns out the Android SDK Location was not set which was a really simple fix. Go to Window->Preferences, click on 'Android' on the left hand menu, fill in the location of the Android SDK (e.g. C:\Program Files\Android\android-sdk) and click OK. Everything worked fine for me after that.
Found the answer in this set up guide: http://developer.motorola.com/docstools/library/Installing-Android-Dev-Environment/
Sometimes Eclipse can get a bit funny (technical term) with classpath container resolution.
You have a working project in your workspace, so you can rule out plugin installation issues. You could try opening the .classpath file (hidden by default, select the triangle in the top-right of the Package Explorer view, select Filters... and uncheck .* resources) and manually adding the container declaration.
`<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>`
You may then have to close the project and open it again (right-click->Close Project) before Eclipse recognises the change.
You may also find that there is an Android builder and/or nature. If you compare the .project files for the working and non-working versions you may see entries for Android builders/natures. Copying the relevant entries may resolve the issue.
sometimes, eclipse can help you.
In Android projects, click right button on the project-> Android Tools -> Fix Project Properties.
It's usefull in class path errors!!
Luck!
I would verify you have the correct version of Java installed as well as the Android SDK. I would recommend installing/reinstalling the ADT plugin in your eclipse, makes it alot easier to create the android project.
http://developer.android.com/sdk/1.5_r2/installing.html
Also. I guess the path of the Android SDK must be setup correctly. This happened when I was overloaded with projects and I decided to create a new workspace copying some existing stuff into it.
Seems that the new workspace requires you to setup these details too once again
Is that setup properly?

Categories

Resources