I have created an Android project and added an external JAR (hessian-4.0.1.jar) to my project. I then added the JAR to the build path and checked it off in Order and Export.
Order and Export is ignored it seems, and all classes from the external JAR are missing at runtime.
Is there a trick to properly include the needed classes from an external JAR when building an Android application using the Eclipse plugin? I do not want to use ant or Maven.
For Eclipse
A good way to add external JARs to your Android project or any Java project is:
Create a folder called libs in your project's root folder
Copy your JAR files to the libs folder
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
By doing this, you will not
lose your libraries that are being referenced on your
hard drive whenever you transfer your project to another computer.
For Android Studio
If you are in Android View in project explorer, change it to Project view as below
Right click the desired module where you would like to add the external library, then select New > Directroy and name it as 'libs'
Now copy the blah_blah.jar into the 'libs' folder
Right click the blah_blah.jar, Then select 'Add as Library..'. This will automatically add and entry in build.gradle as compile files('libs/blah_blah.jar') and sync the gradle. And you are done
Please Note : If you are using 3rd party libraries then it is better to use transitive dependencies where Gradle script automatically downloads the JAR and the dependency JAR when gradle script run.
Ex : compile 'com.google.android.gms:play-services-ads:9.4.0'
Read more about Gradle Dependency Mangement
Yes, you can use it. Here is how:
Your Project -> right click -> Import -> File System -> yourjar.jar
Your Project -> right click -> Properties -> Java Build Path -> Libraries -> Add Jar -> yourjar.jar
This video might be useful in case you are having some issues.
I know the OP ends his question with reference to the Eclipse plugin, but I arrived here with a search that didn't specify Eclipse. So here goes for Android Studio:
Add jar file to libs directory (such as copy/paste)
Right-Click on jar file and select "Add as Library..."
click "Ok" on next dialog or renamed if you choose to.
That's it!
I'm currently using SDK 20.0.3 and none of the previous solutions worked for me.
The reason that hessdroid works where hess failed is because the two jar files contain java that is compiled for different virtual machines. The byte code created by the Java compiler is not guaranteed to run on the Dalvik virtual machine. The byte code created by the Android compiler is not guaranteed to run on the Java virtual machine.
In my case I had access to the source code and was able to create an Android jar file for it using the method that I described here: https://stackoverflow.com/a/13144382/545064
Turns out I have not looked good enough at my stack trace, the problem is not that the external JAR is not included.
The problem is that Android platform is missing javax.naming.* and many other packages that the external JAR has dependencies too.
Adding external JAR files, and setting Order and Export in Eclipse works as expected with Android projects.
Goto Current Project
RightClick->Properties->Java Build Path->Add Jar Files into Libraries -> Click OK
Then it is added into the Referenced Libraries File in your Current Project .
Android's Java API does not support javax.naming.* and many other javax.* stuff. You need to include the dependencies as separate jars.
If using Android Studio, do the following (I've copied and modified #Vinayak Bs answer):
Select the Project view in the Project sideview (instead of Packages or Android)
Create a folder called libs in your project's root folder
Copy your JAR files to the libs folder
The sideview will be updated and the JAR files will show up in your project
Now right click on each JAR file you want to import and then select "Add as Library...", which will include it in your project
After that, all you need to do is reference the new classes in your code, eg. import javax.mail.*
in android studio if using gradle
add this to build.gradle
compile fileTree(dir: 'libs', include: ['*.jar'])
and add the jar file to libs folder
If you are using gradle build system, follow these steps:
put jar files inside respective libs folder of your android app. You will generally find it at Project > app > libs. If libs folder is missing, create one.
add this to your build.gradle file your app. (Not to your Project's build.gradle)
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
// other dependencies
}
This will include all your jar files available in libs folder.
If don't want to include all jar files, then you can add it individually.
compile fileTree(dir: 'libs', include: 'file.jar')
create a folder (like lib) inside your project, copy your jar to that folder.
now go to configure build path from right click on project, there in build path select
'add jar' browse to the folder you created and pick the jar.
Copying the .jar file into the Android project's folder isn't always possible.
Especially if it's an output of another project in your workspace, and it keeps getting updated.
To solve this you'll have to add the jar as a linked file to your project, instead of importing it (which will copy it locally).
In the UI choose:
Project -> Import -> File System -> yourjar.jar -> (Options area) Advanced -> Create link in workspace.
The link is save in the .project file:
<linkedResources>
<link>
<name>yourjar.jar</name>
<type>1</type>
<locationURI>PARENT-5-PROJECT_LOC/bin/android_ndk10d_armeabi-v7a/yourjar.jar</locationURI>
</link>
</linkedResources>
PARENT-5-PROJECT_LOC means relative to the project file, 5 directories up (../../../../../).
Then add it to the libraries:
Project -> Properties -> Java Build Path -> Libraries -> Add Jar -> yourjar.jar
In the same window choose the Order and Export tab and mark your jar so it will be added to the apk.
Related
I am trying to add this library to my project because I want to change something in a class there. That is why I adding it as a dependencies in my gradile setting is not enough.
I know that in the library it says "If you want use this library, you only have to download MaterialDesign project, import it into your workspace and add the project as a library in your android project settings.". But what does that mean, should I copy all files and put them in my libs folder ?
Quoting the documentation:
If you want use this library, you only have to download MaterialDesign project, import it into your workspace and add the project as a library in your android project settings.
Step #1: Download the project, whether via the ZIP file that GitHub gives you or by using git to clone the repository
Step #2: Move the MaterialDesign/ directory into your project directory, as a peer of your existing app/ module
Step #3: Modify your settings.gradle in your project directory to include :MaterialDesign in the list of modules to build
Step #4: In your app module's build.gradle file, add compile project(':MaterialDesign') to your dependencies
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.
I imported an Android project into Eclipse (Custom ADT). During the import process, I chose to copy project into Workspace.
My project could compile and run well but after sometime, it stopped working. When I check in the 'Problems' View, I see that it still looks for the BIN folder in the folder from which the project was imported and it can't find the library it wants to reference.
Aren't Libraries copied along with the project in Android? How do I fix this?
A good way to add external JARs to your Android project or any Java project is:
Create a folder called libs in your project's root folder - you already had this
Copy your JAR files to the libs folder
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
By doing this, you will not
lose your libraries that are being referenced on your
hard drive whenever you transfer your project to another computer.
You need to place the Libraries into your libs folder before you copy/import. Because if you just added the Library over the Build-path its just copy the Reference, not the Library itself.
I want to use a svg-android library to work with *.svg files in Android. For the same, I've installed the apache ant, created a folder in workspace called: SVGAndroid and then added
android.library.reference.1=C:\Android\workspace\SVGAndroid
to project.properties file.
There were no documentation for functions in ReadMe, so I'm wondering if the installation correct or not, how to reinstall an external lib in the second case and where are the examples of using svg-android?
For your including of the project, just include the project into your workspace and in your respective application under the properties of that project just add a dependency on a library that references svgandroid. Make SURE to include the files and ensure that the compile class files are present in the final output of the apk. This typically means that you add the project to your build path/order.
Another approach is to just take the entire project and compile it into a jar file and then include that jar in your libs folder and add that dependency on the jar file of that respective project to your core application.
Useful links
How can I use external JARs in an Android project?
I would rather suggest to not add the path to the project manually, but from Project Properties -> Android and you will have a list with all the library projects in the workspace from which you can choose the desired project.
Also I suggest adding in project.properties the following line:
manifestmerger.enabled=true
in order to have all the AndroidManifest.xml files of the referenced library projects merged into the main project's manifest.
If you want to include a library file in to your project follow the below steps;
Download a jar file and save it in any folder.
open your project.
Right click--> Build path--> Add External Archives---> Add your external jar file to your project
I am working on an app which uses Commonslang.jar libraries. I was trying to setup it on a new system with newly installed Eclipse.The project is not showing any error But when I run the project it force closes with NoClassDefFoundError at each point where I use Commonslang lib.
I dont know how to fix this problem.Please help?
I think there are "lib" folder in your project directory so just change it "libs" instead of "lib" and clean project and check it..
> Note: No need to build path for any third party library
You need to create libs folder in your project and copy that jar file there. Then in eclipse, right-click on that jar file (which is in libs folder) select Build Path and then select Add to Build Path.
Remember: if you are using a jar file in your Android Library Project, and associating that library project with your regular UI
project, it won't be enough. You must need to include that jar file
(as described above) in both of your projects (Android Library and UI
project) to make it work.
Have you added the jar to the Java Build Path?
Go to the Properties for the Project and select Java Build Path -> Libraries tab then add the external jar there.