Android, referencing other libraries - java

Is there a way, how to add custom library to android app. If I add non-android library to my project it throws error "Only Android project can be add as dependency" and if I reference Android Library to Java Application it throws "This project cannot be added because it does not produce a JAR file using an Ant script." the point is, that I want to keep code on one place (library) and reuse it.

Your best bet is :
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

Related

Can not access Libraries inside jar library of a Library project

I have made a library project that uses Async and Json/Gson libraries. It is working perfectly fine if I use this project as library project inside another Application Project. I don't want my code to be open to world so I tried to make a JAR of my Library Project. I am getting error that shows that the Async and GSON libraries can not be referenced.
Is my JAR not including the ASYNC and JSON libraries?
if not then how to do it? I have Checked the libs folder when exporting Library Project to JAR.
If Jar has included Libraries then why can't it get a reference? Is there any way that I can make a reference to the libraries present inside of the JAR of my library project?
Where am I making a mistake?
Update-1
Ok I have checked by extracting my jar and there is a lib folder there which has all the libraries that I have used in my Library Project.
Update-2
How I am exporting my Library Project as Jar.
Select Project to Export
Chose Jar File
Chose only src and lib folder.
Checked
Export generated class files and resources
Compress the contents of the JAR file
Chose Destination
Left All other checks as defaults and Finish.

Android: Eclipse jar icons are white and can't access classes

I downloaded a java/android library .zip file and extracted it. Then I used the command
$jar -cvf java-lib-name.jar topfile/
and it seemed to traverse the folder and create the jar. However, when I then move the .jar into the /libs/ folder, the icons for the packages in the jar are white instead of brown and I can't access them. When I went into Properties->Java Build Path, the .jar is in the Private Libararies section. Did I create the jar incorrectly? Is there another setting in Eclipse I need to use?
You need to compile java classes before creating jar.
Or another thing you can do for this project is to import the library as android library project to your current eclipse workspace and then use it on your android project.
Add the library project using File > Import > Existing Android Code Into Workspace. Make sure this project is marked as a library. Right click on your project, choose Properties, click on Android in left pane > Under library heading, add the library project

Android creating library project without source code

I am developing android library project.I don't want share my source code.I want to create something like jar file.But i can't create jar file as it includes xml layouts.so is there any way to build project without including source code?
I can give you an example of such library. Open Android SDK manager and download Google play services library.
This library contains 'libs' folder with google-play-services.jar which contains all the source code. Also there is 'res' folder and android manifest file to be able to add this library to every android project.
First thing is to have the full library-project ( example : FullProject).
Then I import the full library project (FullProject) in a new workspace and check “Copy Projects into workspace”. This new project must have the same name ( FullProject)
in the copy, i removed the source code.
in the app-project i added the source-less-library-project as android library
in the app-project i added the .jar file with all the binary classes from the full library-project ( right clic on directory “source” of the full library-project, export as jar and check « Add directory entry » ).
in the build path of the app project, in "order and export", check the .jar exported
update the manifest file of the app project with all views of the library project .
clean the app project
It’s all !

Android import library

In my project, I need to use external library. That library consists of .class files. I have added folder with this library to my project using properties -> Java Build Path -> Libraries -> Add class folder. I can compile project without any problem, but when I run it, I get failed resolving xy and debugger stops on line where I create instance of class from imported files.
I guess the external library I use isnt linked to my .apk file. How can I fix this? I have tried to check this library on "Order and Export" tab assuming that, it will export the library with my application. But it didnt work.
How can I link this library to my project .apk? Do I need to somehow install this library on device first? Do I need to copy this library in application folder?
PS: I'm using Eclipse Juno, android SDK target 10.
Thank you
UPDATE
I have this library copied in libs folder. External library path is:
my_project/libs/sk/aicit/leg/libraries*.class
I have added ./libs folder in Libraries settings of project.
I do not have .jar version of this library, only .class files.
I have also souce code of this library, but I didnt want to include its classes in my project, it has its own dependencies, I wanted to just "link" it like library. Its an external library I havent written.
UPDATE 2
I have copied all the source files from library to my projects "src/" dir. I have removed Library from Java build path. I can stil compile my project, but when I run it Iget
FATAL EXCEPTION:main
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{
com.example/myvideorecord/com.example.myvideorecord.Robot}: java.langNullPointerException
...
What can I do, how to debug this error? :(
steps
Copy the library file into libs folder
select jar file -> right click -> Build Path -> Add to Build Path
clean the project Project-> Clean -> select your project -> ok
Android Studio
For importing a Android library into Android Studio, use the following steps:
1) (Optional) I like having my main Android project contain everything it needs to compile correctly, so I move external libraries into the libs/ directory. Generally, you only need to keep the following directories:
res
src
AndroidManifest.xml
*.iml (This will typically be the name of the library you're importing.)
2) Go to your Project Properties.
3) Go to Modules.
4) Import a New Module (Command + N and then Import Module).
5) Navigate to the library directory you want to import.
6) Create module from existing sources.
7) Click Next three times to add the necessary files.
8) Click Finish to complete the module additional.
9) Click on your project in the module list and go to the Dependencies tab.
10) Click the + button at the bottom and click Module Dependency....
11) Make sure your library you're importing is selected and click OK.
12) Click OK in the Project Properties window.
And you should be good to go.
Copy that library file into libs folder
if u have the source code of the library you can import it in eclipse .
Right click on your project -> properties->android -> reference-> add
and add the library project which u have imported. This will include only .class files of the library in ur project

Creating and using custom JAR in Android project

I am trying to create and use jar file in an Android project under Eclipse. I have tried various methods without any success. Here are the steps:
Create jar file from the source files jar -cf lib.jar *.java
Copy jar file to libs folder
Right click, Build Path->Add to Build path
Now, compiler gives unresolved symbols if I try to use a class from the jar file?
Can someone please let me know correct method to create and use an external jar file for the Android project under eclipse.
This worked fine for me:
1)Place Jars in assets folder.
2)Right click on project name.
3)Select properties.
4)Select Java build path.
5) Select Libraries.
6) Click Add Jars.

Categories