简体   繁体   中英

Creating a Java jar library to use on Android project

I am working on developing a game for Android and I have a lot of the code written on a java application. I would like to use the classes and methods in the Java application within my Android application. I am using Eclipse to develop my application so I tried to create a Jar for the Java application By doing: Right click project->Export->Jar I then saved the jar somewhere and added the jar to my Android buildpath. When I try to use methods from that jar I get compilation errors because Eclipse cant find the methods. Am I supposed to have any import statements? The name of the package on my java app is (default package) so I am not sure what the import statement would be. I would appreciate any help.

EDIT: I changed the package name of my application so that I could import the classes within it and that got rid of the compilation errors. When I try to run the project I now get NoClassDefFoundError Could it be that I am exporting the jar for my Java application wrong?

Open Project properties -> Java Build Path -> Order and Report tab.

Try to check jar check boxes,

see example:

在此处输入图片说明

To make sure that you created jar properly, here other snapshot of jar export:

在此处输入图片说明

Rename the package first. http://docs.oracle.com/javase/tutorial/java/package/namingpkgs.html

Yes, you need to import the package inorder to use in a class. Else the compiler will not identify the codes. Import the jar into build path and import the package into the Java code as :

import java.util.HashMap;

If I understand your correctly you exporting a Java .jar (built using Java compiler)? As far as I know Android does not understand Java bytecode though the compiler for Android understands Java source code.

I'd suggest you just put all that source files in the Android project and compile them there, hopefully you aren't using functions that Android API does not have.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM