Android+Ant external jar library - java

I'd like to try org.apache.commons.lang.StringUtils in my Android project. I have downloaded commons-lang3-3.1.jar and placed it in my project's libs directory. However importing it fails with the library not being found. I have used many variations of
import org.apache.commons.lang.StringUtils;
with no success. My environment is the linux console and I'm using ant to compile, no Eclipse. My understanding is that Ant picks up any library in the project libs directory.
Please help. Android/java coding noob.

Lang 3.0 (and subsequent versions) use a different package (org.apache.commons.lang3) than the previous versions (org.apache.commons.lang), allowing it to be used at the same time as an earlier version. ref: http://commons.apache.org/lang/
import org.apache.commons.lang3.StringUtils;

Just put the jars in the libs/ directory. Silly me, I was putting them in the lib/ directory until I ran into the same issue and found the answer here:
How to build an android app with external libraries using ant?

Related

Importing vanilla Java project to use as a library in Android

I've written a piece of vanilla Java code to do a specific task. It contains a bunch of packages and resource files.
I want to import the vanilla Java project to Android to use some of its utilities.
I've tried to build my project to jar file, add it to the libs/ folder and import it. I've also tried to import it directly to the project dependencies. However, none of the above worked. The jar file showed up in the "External Library" part, but I cannot import it from a class inside as it gave me compilation error.
HERE you have plenty of ways how to attach jar to Android project
are you shure that your jar (assuming properly built) will work on Android? not all components and classes of "vanilla" Java are available on Googles OS... check out THIS topic for more info

How do I fully include the Batik library in my Java project?

I am trying to add the Batik library to my Java project. I added the unzipped binary to my lib folder, and added all of the JARs in the top level to the build path.
However, when I make the following import statement:
import org.apache.batik.dom.svg.SAXSVGDocumentFactory; I get the following error:
"The import org.apache.batik.dom.svg.SAXSVGDocumentFactory cannot be resolved"
Other import statements do not exhibit this problem, such as the following:
import org.apache.batik.util.XMLResourceDescriptor;
As far as I understand, the SAXSVGDocumentFactory class exist somewhere in the binary I added to the lib folder, so the issue must be with adding JARs to the build path. How can I make sure that all of the Batik source files are added to the build path correctly and are usable?
It cannot be resolved because it doesn't present in 1.8 jar. It exists in 1.7.
http://www.findjar.com/class/org/apache/batik/dom/svg/SAXSVGDocumentFactory.html
The easy way to find this out is to find the right jar and click to open it by the package: org.apache.batik.dom.svg.SAXSVGDocumentFactory. You wont see this class in 1.8 jar, but you will see it in 1.7 jar.
To fix, download a 1.7 jar and add it to your project again.
http://apache.mirrors.hoobly.com/xmlgraphics/batik/source/
If somebody can't find SAXSVGDocumentFactory after updating Batik in legacy project, just change the import:
import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
with
import org.apache.batik.anim.dom.SAXSVGDocumentFactory;

How do I add a Github library to my Android Project as a JAR in Eclipse?

I'm trying to add https://github.com/astuetz/PagerSlidingTabStrip to my project. Looking at it, I notice that it has files in res and depends on its own R.java file. When I make it a JAR I continue to get errors because the dependencies aren't all there. I'm not sure how to get this to work.
I keep getting errors like
import android.support cannot be resolved and everything else too. I tried referencing different answers on StackOverflow but none of them yielded a working solution.
The resources won't be packaged into the JAR file. Google is working on a new packaging scheme called AAR which is supported by Android Studio.
In order to use this third-party library in your own project, you should clone it to a local repo. Then you can import it directly into your Eclipse workspace and then add the Eclipse project to the classpath of your app's project.
Download the whole source code from https://github.com/astuetz/PagerSlidingTabStrip by git tool.
On your Eclipse IDE, import the library as exsisting source code at Android option.
Add the library your add before into your project.

Cannot import twitter4j classes in Eclipse

I added twitter4j 3.0.3, into an Eclipse project but it does not import any classes from the jar. I copied in some code samples from the twitter4j jar files but eclipse can't find any of the classes in those files - e.g. Twitter, ConfigurationBuilder, TwitterFactory etc... I've added twitter4j-core-3.0.3-sources.jar to my build path. But Eclipse doesn't suggest to import any classes in these files. Is there anything I'm missing?
You need to add the twitter4j-core-3.0.3.jar JAR to your build path, it can be found under the lib directory of the download.
The twitter4j-core-3.0.3-sources.jar JAR only contains source code.

NoClassDefFoundError on external library project for Android

I use eclipse for Google Android development.
I've created a library project ([x] Is Library in the Android-settings), which includes an external jar-file (Referenced Libraries). This library project are referenced in another Project (the actual project which will use the library project). This is done by add the project under the Android-settings.
the source compiles but if I want to execute it on the device, I get the NoClassDefFoundError for a class which is inside the jar-file which is included in the library project.
Edit: The jar-file ist added to the exported entries ([x] my.jar on the Order and Export-Tab from the library project)
Is there a clean way to get this working?
It has been clearly stated in offcial API here:
A library project can include a JAR library
You can develop a library project that itself includes a JAR library, however you need to manually edit the dependent application project's build path and add a path to the JAR file
The jar lib must be manually added to the dependent application project's build path, not only the library project build path itself.
Update from SDK r17:
This is automatically handled by ADT now, check out new feature for ADT 17.0.0 release here:
Added feature to automatically setup JAR dependencies. Any .jar files in the /libs folder are added to the build configuration (similar to how the Ant build system works). Also, .jar files needed by library projects are also automatically added to projects that depend on those library projects. (more info)
For those who followed the steps(even check the projects in "Order and Export") and still have the java.lang.ClassNotFoundException in the API 17, the final step is to check that your compiler does not run with Java 1.7. If is 1.7 then you should change it to 1.6 for all your projects. After that it will ask to rebuild all the projects and successfully ran on my phone :)
To change the java compile version in eclipse, this is located in: Project properties > Java Compiler > Compiler Compliance level: 1.6
Go to project properties -> build path-> libraies
If you see your jar files like this
snmp4j.jar - e:\software\jars
Its may your problem
Add libs folder in your project and copy jar file in that folder. Right click jar file and go build path -> add to build path. Then you can see your jar as
snmp4j.jar - project_name/libs
Its worked for me.
I had two projects using the same library: one working, the other one crashing with java.lang.NoClassDefFoundError.
After nothing else helped me, I looked into the file project.properties in the root directory of my project.
The working project had the android.library.reference line (the last line below), the crashing one did not:
# Project target.
target=android-17
android.library.reference.1=../my-library-project
I manually added the line and it started working!
(Yes, I did try both (project) properties -- java build path -- projects and (project) properties -- java build path -- order and exports -- nothing helped.)
PS By the way, "project properties" has also the "project references" tab. No idea if it would work.
I had a minor issue when I upgraded to ADT17 where my libs weren't being imported properly. Turns out this is because my librarys were being linked as dependancies from my lib folder not libs!
Seems librarys have to be in the libs folder from now
I had a similar problem and non of the solutions out here fixed it.
Short version: the JAR was compiled in Java 1.7 and could not be found by Dalvik.
We build an internal JAR that we share across backend and mobile clients with Maven. The problem ended up being that the JAR was compiled with Java 1.7. Androids dalvik only supports Java 1.5 and 1.6. This is why everything works fine in Eclipse as it's not compiled to dalvik at this point.
We even specified the target and source version in the Maven compiler plugin to compile with Java 1.6. This did not work because only JDK 1.7 was installed on the build machine. A small note at the bottom of the Maven page gave us the hint: Note: Merely setting the target option does not guarantee that your code actually runs on a JRE with the specified version.
To see if you have this problem as well, rename your *.jar file to *.zip unpack it, and look in the MANIFEST.MF file for the Build-Jdk: parameter to see what Java version actually compiled your JAR.
Another thing to pay attention to is library package names.
If you are using ADT21 and you happen to have libraries that have the same package name, there will be error during compile but it will still compile and run in Eclipse, giving you an APK that is missing some of the resource classes. Hence the crash when you run the app.
If you compile it with ANT then you can see the compile error that says two or more libraries use the same package name.
To fix this, rename your library project by using Android Tools -> Rename Application Package. Then everything will go back to normal.
It took me almost entire day to figure this out...

Categories