简体   繁体   中英

Include Java project as library

I have three projects in my eclipse workspace:

EventKitchenCore
EventKitchenDesktop
EventKitchenAndroid

EventKitchenCore contains all the core functionality, and EventKitchenDesktop and EventKitchenAndroid are essentially just different user interfaces.

I have EventKitchenCore added and working as a library in EventKitchenDesktop , however I am unable to figure out how to add it as a library to EventKitchenAndroid .

How do I do this? It would be a pain in the tush to have to export as a jar in the lib directory every time I make a change...

I tried adding the Core project in Properties > Java Build Path > Libraries and Properties > Android > Library , but no luck :\\

Update - for now, I got it working by exporting the core project as a jar in the libs folder. However I make frequent changes, and like I said, it's a pain in the tush.

Eclipse has builtin Ant support. You can make use of it to automatically create a JAR of the current project and put it in a folder of another project.

Provided that EventKitchenAndroid and EventKitchenCore projects are both in the same workspace, create a build.xml file in EventKitchenCore project which contains just this:

<?xml version="1.0" encoding="UTF-8"?>
<project name="EventKitchenCore" default="createjar">
    <target name="createjar">
        <jar destfile="../EventKitchenAndroid/libs/EventKitchenCore.jar" basedir="bin" />
    </target>
</project>

To test it, select the file in Eclipse and press Ctrl + F11 to run it as Ant build file. If it works the way you want, then you can tell Eclipse to automatically execute this build file everytime the project is built. Go to the project properties of EventKitchenCore and in the Builders property, click Import... and select the build.xml file you just created.

Now, it will be executed everytime the project is built. You can manually force the build of a project by pressing Ctrl + B . See also the video demo I just created.

You can configure your projects as a Maven Artifact that depends of EventKitchenCore, so, Maven will handle this for you:

EventKitchenCore pom.xml:

<project ...>
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.adamgaskins</groupId>
   <artifactId>event-kitchen-core</artifactId>
   <packaging>jar</packaging>
   <version>0.0.1-SNAPSHOT</version>
   <name>EventKitchenCore</name>
</project>

EventKitchenDesktop pom.xml:

<project ...>
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.adamgaskins</groupId>
   <artifactId>event-kitchen-desktop</artifactId>
   <packaging>jar</packaging>
   <version>0.0.1-SNAPSHOT</version>
   <name>EventKitchenDesktop</name>
   <dependencies>
     <dependency>
       <groupId>com.adamgaskins</groupId>
       <artifactId>event-kitchen-core</artifactId>
       <version>0.0.1-SNAPSHOT</version>
     </dependency>
   <dependencies>
</project>

EventKitchenAndroid pom.xml:

<project ...>
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.adamgaskins</groupId>
   <artifactId>event-kitchen-android</artifactId>
   <packaging>apk</packaging> <!-- for this, you need the maven-android-plugin -->
   <version>0.0.1-SNAPSHOT</version>
   <name>EventKitchenAndroid</name>
   <dependencies>
     <dependency>
       <groupId>com.adamgaskins</groupId>
       <artifactId>event-kitchen-core</artifactId>
       <version>0.0.1-SNAPSHOT</version>
     </dependency>
   <dependencies>
</project>

Don't forget of SNAPSHOT suffix, otherwise, Maven will not update your changes.

The simplest idea (which would work outside of Eclipse, too) would be to create a symlink/shortcut from your package-directory of the library into the src -folder of your projects:

|- EventKitchenCore
   |- src/
      |- org.your.library.package <-+
|- EventKitchenDesktop              |
   |- src/                          |
      |- org/desktop/something/     |
      |- symlink to ----------------+
|- EventKitchenAndroid              |
   |- src/                          |
      |- org/android/something/     |
      |- symlink to ----------------+

This way, you could compile the actual projects as if your library would be a part of the applications and you have your changes immediately reflected in both your projects.

I would say as some contributors suggest, to use maven or ant. The configuration is easy, most of them have already put them, so it is a matter of copy and paste. When you are in the development environment, what you can do is to add the projects in your build path. Go to build path and select the "projects" tab and add the projects you need, so every time you make changes, all the changes are visible for the projects that are linked. When you are about to deploy, you can then use the maven or ant. However, you can start with both of them in development as well. But if you want a quick fix, I would suggest to reference the projects if you don't want to use maven or ant yet. But when you deploy I strongly recommend to use one of this two project managers, because as you mentioned, export the projects as jars and then add them to the libraries folder is a pain.

Here is a solution that may help you :

1) Eclipse-Package Explorer - Your Project(Android) - Rightclick on your project.

2) Click on Android (At left side of screen) - Now your screen may look like this -

在此输入图像描述

Now at bottom of screen, in Library section you can add all the library project directly without making any JAR. Just you need to import those library projects in workspace or all your library project should be opened in eclipse.

1) Click on Add button you will get the list of library projects available, choose from them and Build your project.

Good Luck..

You have to go to the preferences of the project you want to make a library and check Is Library in the Android sub-menu. Then you can try to import your project again.

I believe what you are looking for is the "Link Source" option within Eclipse. From your android project, select the project and right-click to select "Build Path" > "Link Source". This will allow you to have your Java library accessible within the same build path as your android app for development.

If all you want is to reference the Core projects code, then in Eclipse you can just reference this project from the Android project. (The 'library project' feature is only to Android library projects, you just have a normal Java project here, right?)

So in your Package Explorer you have all three (Core, Android and Desktop) projects. Right click EventKitchenAndroid and select Properties. Choose Java Build Path -> Project tab and click the Add... button. Choose EventKitchenCore . Go to the Order & Export tab and make sure the EventKitchenCore project is checked and you're good to go. Any change to the source of EventKitchenCore is immediatedly available to the Android project.

This is the way i follow to add library to my projects

项目与图书馆

PHASE 1: Creating Library Project

Here on image you can see 2 projects named library and ListSamples which is using library

To make the project library as a library project right click on the project and select properties

设置库

Mark IsLibrary check box to make it library

you can see .jar file in bin folder of library project

编译的Libary

Now the library project is created

PHASE 2 : Adding Library Project to android Project

Now Right click on your android project and select its property. then click on Add button inside library area and set the path of your library project to it

在此输入图像描述

在此输入图像描述

Then a pop- will appear showing a list of library projects you have created in eclipse which is presently active (closed library projects will not shown)

在此输入图像描述

Here i got only one project so it is listed here

Select which all library you needed and click OK . Now the library projects will be added to your project

在此输入图像描述

While checking your android project you can see the .jar file of your project is been added to your android project

在此输入图像描述

NOTE :- If you are using android support package( android-support-v4.jar ) in library project , There is no need to add it again in your Android Project .

You can also use ant to add library to projects. But i don't have knowledge about that .

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