How to use an external java project in an android application - android

i've been struggling with this issue for a couple of days now...
ill explain my problem in a nutshell....
i have a simple java project (not android library or something more complex) which i want to use as my model..
lets assum it is called mymodel and it has a simple class Model.
i also have an android application myandroidapp and in the main activity i want to create an instance of the class Model.
I can add the mymodel as a project to the build path and compile but when i run this application it throws class not found exception. how can i make it work?
please help.
thanks

Step #1: Create a JAR based on your "simple java project"
Step #2: Put the JAR in the libs/ directory of your Android project
And you're done.
Never mess with the build path of an Android Eclipse project manually.

In Eclipse, you can follow the following steps:
Creating JAR file
Right-click on the root of the mymodel project and click on 'Export...'
In the window that appears, expand the 'Java' category/folder, choose 'JAR file' and click 'Next'
In the next window, choose the options you desire, especially pay attention to where the 'export destination' is. When you're done, click on 'Finish'. The JAR file will now be located at the 'export destination'.
Adding JAR to Android project
In your android project (myandroidapp), copy the JAR file into the libs/ folder. You can do this by simply copy and pasting or dragging and dropping etc.
Then, right-click on the JAR file that is now in the libs folder, go on 'Build Path' and from the menu that appears, click on 'Add to Build Path'
You should now be able to use this JAR file in your Android project.

Related

How to import eclipse library project from github to android studio project?

I want to use HoloCircullarProgressBar as a library project in my android studio project.
I tried to do it by copying into pre-created "library" folder in my project and then to add it to project in "Project Structure". But it's not working somehow.
Could anyone give a very specific step-by-step tutorial on how to do that in android studio 0.3.6?
If you're importing a library as source code into a Gradle-based project, then at the moment there's no super-easy way to do it (sorry, it's on the to-do list, see https://code.google.com/p/android/issues/detail?id=62122) so you'll have to author your own build file for the library. Actually, it might be easier to use the New Module wizard to set up the build file and directory structure, then you can trim it down and copy the files over. This set of steps should get you up and running. It seems like a lot of steps but it should hopefully go pretty quick.
From the File menu, choose New Module...
From the wizard that comes up, choose Android Library
From the next page of the wizard, give it the module name HoloCircularProgressBar, and uncheck the options for Create custom launcher icon and Create activity.
Click Finish on the wizard.
It should add the new module to your project, so you'll end up with something like this:
Delete everything inside the src/main folder.
Now copy AndroidManfiest.xml, ic_launcher-web.png, res, and src from the HoloCircularProgressBar source into the src/main folder.
Rename the src folder that you just copied into src/main to java.
The New Module wizard left some things in the build.gradle file in your HoloCircularProgresBar module/directory (make sure you're editing that one, not the one for your main app module) we don't need. Remove the dependencies block and the release block.
At this point you should hopefully be able to build successfully. Now if you want to actually use the module:
Go to File > Project Structure... > Modules > Your main module > Dependencies.
Click on the + button to add a dependency, choose Module dependency, and select HoloCircularProgressBar from the list.
Now import statements and usages of the library should work, and you should be good to go.

how to include jar files in my eclipse project

I'm new to android and java. I downloaded the GSON library for use in my android project which exists in Eclipse. I lookup instructions on how to use the jar file in my project and it says...
Quote...
You can use a third party JAR in your application by adding it to your Eclipse project as follows:
In the Package Explorer panel, right-click on your project and select Properties.
Select Java Build Path, then the tab Libraries.
Press the Add External JARs... button and select the JAR file.
Alternatively, if you want to include third party JARs with your package, create a new directory for them within your project and select Add Library... instead.
It is not necessary to put external JARs in the assets folder
...UnQuote
I'm afraid that I don't know the difference between "using a 3rd party jar in my project" and "include third party jars with my package." I want to be able to use the GSON methods in my Android phone application and, of course, have the capability deployed in my .apk.
So, Which do I want to do?
Thanks, Gary
add the jar to your libs folder in your project.
open eclipse go to package explorer
right click on your project and select project properties
click on java Build path in the bar on the left hand side of the popup
select the libraries tab
select the "Add External Jar File" and navigate to your JAR file
best of luck =)
click Properties
left select Build path
Than add jar, and do not use the external libs, unless it is really required.
You have to copy the jar to your project to find it with "Add jars"
Right click onto your main project in the Project Explorer
Open properties option, found at the bottom of the context menu
A dialog box opens from which you select the Android option
Remove the old .jar file, click on ADD to add a new .jar file and select your the file you want to add
You're done!

How can i add "Add External Jar" to my project in android

I want to add banner to my project. I have done some work in this regard, but i have problem in adding "Add External jar".
[Right click on my Project -> properties -> Java Build Path -> Libraries -> Add External jar].
Now my problem is, as I click "Add External jar" a new widow(browsing) opens and asking for to open jar file, but I have studied "Android Developer documents" for adding banner in project, there was nothing mentioned about the to open jar file. Now what I have to do, to add "Add External jar" and I could continue my project further.
Yours help would be greatly appreciated.
Thank`s in advance.
How can i add “Add External Jar” to my project in android
For, this i always follow below steps -
Just create a libs folder on my project ->And, copy the .jar file to there.
In your workspace select your project and right click -> on Properties, just go to Java Build Path
Click the Add External jars it'll open the window dialog box -> root your workspace -> And, select your project -> go to libs folder -> choose the .jar file -> Click ok.
Hope these steps helps you.
There should be a jar file that you downloaded that contains he libraries you need. You'll need to select that jarfile for the step that you are doing currently.
It is a good idea to keep that file within your project folder as well. Many people like to keep all of their libraries inside of a folder called libs within their project folder.
I'm not sure what "banner" is, but if it's an Android Project it cannot be added as a JAR. Projects that have resources are not able to be fully externally liked libs.
If indeed this is Android Project you will need to import the existing project into your Eclipse workspace here the steps http://developer.android.com/guide/developing/projects/projects-eclipse.html#SettingUpLibraryProject
Create a folder called "libs" in your project.
Go to that folder using Windows Explorer and copy the .jar file there.
Refresh your project in eclipse and right click on the jar file and select "Add to Build Path" option.

Create Jar file for android project

I want to create a jar file from one android project for another android project.I want select only certain classes not all classes from project.I tried eclipse-export way to create a jar.Jar file is created and it also contains all selected class files,after adding the created jar file to the main android project it gives the error:“java.lang.NoClassDefFoundError”.Is the eclipse-export way is not valid to create jar for android project?
I also tried steps provided by this blog http://www.olivacorner.com/2011/04/create-an-android-jar-library-with-eclipse/comment-page-1/#comment-343,but not able to create a desired jar file.
Can anybody please provide the steps to create a jar file for android project.
Thanks.
So you want to create a library for Android? In this case, you can create a Library-project and then reference to it in the main-project.
See the docs.
Step 1. Create Android library project as explained here.(Same as suggested by Lukas)
Step 2. Now you need to give reference of Android Library project created in step 1 to your client application. There are two ways to do so.
Give reference of Android Library Project itself by client app property -> Select 'Android' on left pane -> In Libraty Section, Add Android Libraty project (This is explained at link given in Step 1 above)
Give the reference of .jar file of Android Library project (from Location of Android Library project -> bin -> .jar file). Client Application -> Properties -> Click 'Java Build Path' in lefe pane -> Go to 'Libraries' Tab -> Click 'Add External JARs' button and select the .jar file -> Go to 'Order and Export' tab and select the added .jar file reference and move it to top.
Hope this will help you.

Why can't android find com.google.gson.Gson

I'm trying to use GSON in my project, but my application is crashing, with logcat saying that com.google.gson.Gson cannot be found. I've put import com.google.gson.Gson on my class files, I have gson in my package explorer, and added it by Right click -> build path -> add libraries. It also shows up in Project->properties->java build path->libraries tab->gson. What have I done wrong?
What worked for me: Check the checkbox next to the lib (gson-2.0.jar) in: 'Project Properties' -> 'Java Build Path' -> 'Order and Export' tab. Then do a clean/build.
This adds the exported=true attribute to the classpath entry
<classpathentry exported="true" kind="lib" path="libs/gson-2.0.jar"/>
Have you put the jar file in the libs folder of the project? If not , try moving it there, creating the folder if required. Should be at the same level as the src folder.
I had a similar issue trying to get an app built and that solved it.
I'm using IntelliJ, not eclipse but here is how I did it:
Download the source files from https://google-gson.googlecode.com/files/google-gson-2.2.4-release.zip
Unpack them
Copy them into the "libs" folder of your project. I did this manually by using the terminal but you can do it by going in your project folder located in your computer. Your Project_Name >app >Libs [paste them here]
Go to your editor (IntelliJ in my case) You should see the the following 3 files in the libs folder of the project structure:
gson-2.2.4.jar
gson-2.2.4-javadoc.jar
gson-2.2.4-sources.jar
Open your build.gradle file and insert the following line in the dependencies section:
compile files('libs/gson-2.2.4.jar', 'libs/gson-2.2.4-javadoc.jar', 'libs/gson-2.2.4-javadoc.jar')
Right click on each of the source files and select "Add as Library..."
Use the following options:
Name: name of the jar file
Level: Project Library
Add to Module: select project your adding to
Rebuild project
Build > Rebuild project
Import and use it!
import com.google.gson.Gson;
I have observed today that it doesn't like when you add your library.
The solution that worked for me was to add it as an external jar only. I have observed it by doing the steps described by author of this post i.e. Properties -> Java Build Path -> Libraries -> Add External JARs and point to the downloaded gson library.
I ran into the same issue (when installed SDK 17) ...
The solution is, that you just put the pure jar files into the "libs" folder (without subfolders).
You also don't need to declare them, android wil find them itself. You may need to do a Project > Clean
I had similar problem, but I guess my requirement was more complicated as the GSON libarary was used by an Android Library Project that my main Android project depended on. I have tried both approaches above but none of them worked for me. If I add the GSON jar file directly to the main Android project it works, but that is not what I wanted.
With a bit of investigation I have realised that the GSON jar does not use any dependencies (third party libraries) itself. So my solution was to copy the source file of the GSON project to my Android Library project's src filder and it worked like a charm. so you can use the Source code rather then the binary code. The source code is part of the download in a jar file, just unzip it.
i'm sure you have copied the jar into "libs",
and also added property> Java Build Path >(tab) Libraries, right?
my issue was stupid...
please check other than Gson jar, if you have two stuffs at least in above tab:
android x.x
Android Dependencies (especially this one, i missed this, eclipse show me an error of "could not find class com.google.gson.xx" )
simply do a project copy will solve this problem...
Do not edit .classpath file directly or even put files into lib folders out of Eclipse.
Instead use the Java Build Path->Order and Export tab to select libraries to be exported in the final deployment unit (eg war/apk)
To be on safer side, do a Project->Clean after you change the build path.
I faced the same thing.But the solution was quite easy,just Right-Click on your project->Properies->Libraries->Add external jars->OK and thats it.It solved my problem.hope it will solve yours as well
If Projct Properties -> Java build path -> Add external jars doesn't work and your project is a web project, try adding the gson jar directly to the server lib folder. Example: for apache Tomcat to apache-tomcat/lib.
I have been trying the same thing and read so many answers and tried so many things but to not vial, but i solved this problem by doing just one click and its simple.
1) Add your .jar file in libs folder make sure its libs not lib
2) Clean and Build your project you will see that file under libs folder
3) right click the file and go to build path and select add to build path
Run your project it will work fine. Hope this helps
I am using Android Studio
I had the same problem I had solve it by
select project press f4 from popup
click app and in dependencies
select file dependency and select the three folders which I had pasted in lib directory

Categories

Resources