共享Java库与Android应用 [英] Sharing Java library with Android Apps

查看:176
本文介绍了共享Java库与Android应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始在Android开发,并使用NetBeans与NBAndroid和SDK 17。

I'm just getting started in Android development, and use Netbeans with NBAndroid and SDK 17.

我想使用相同的Java源代码code在我的Java和Android应用程序。

I'd like to use the same Java source code in my Java and Android app.

http://developer.android.com/guide/developing/项目/工程,eclipse.html 说,如何做到这一点在Eclipse(虽然是粗略的.JAR连接东西),但我似乎无法使它在NB工作。

http://developer.android.com/guide/developing/projects/projects-eclipse.html says how to do it in Eclipse (although it is sketchy on the .JAR connection thing), but I can't seem to make it work in NB.

根据该链接,我的理解是,对于Android应用正确的设置是一个Android应用程序项目,它引用一个Android库项目反过来引用由Java库项目产生一个.jar库。然后,我也有一个Java应用程序项目指的是同一个Java库项目。

Based on that link, My understanding is that the correct setup for the Android app is an Android Application project which references an Android Library project which in turn references a .JAR library produced by a Java Library project. I could then also have a Java Application project referring to the same Java Library project.

所以,我给自己定了这个项目的结构......我有一个AndroidApp项目,该项目是在com.ex包一个基本的HelloAndroid活动。该项目包括在库文件夹中的Andr​​oidLib库项目。我也有它定义它有一个功能的getText()刚刚返回要显示的字符串一个简单的在LibClass类LibClass.java文件中。在AndroidApp在MainActivity调用它来获取字符串输出。

So, I've set up this project structure... I have an AndroidApp project which is a basic HelloAndroid Activity in a com.ex package. This project includes an AndroidLib library project in the Libraries folder. I also have a LibClass.java file which defines a simple LibClass class which has one function getText() that just returns a String to be displayed. The MainActivity in the AndroidApp calls this to get the String to output.

当我把LibClass.java直接进入AndroidLib项目,一切都很好。

When I put LibClass.java directly into the AndroidLib project, everything is fine.

但我想要做的是共享源$ C ​​$ C与Java。

But what I want to do is to share the source code with Java.

因此​​,我希望向LibClass.java搬入JavaLib库,其.jar文件包含在AndroidLib项目。然而,当我尝试了,我得到在MainActivity类错误,抱怨它无法找到在LibClass。看着Projects窗口中,我可以看到LibClass.class的com.ex包中的JavaLib.jar在AndroidLib项目的库文件夹内。和AndroidLib在AndroidApp项目的库文件夹可见的,但它并没有显示出任何包或其他内容有

So I want to move the LibClass.java into the JavaLib library, whose .JAR file is included in the AndroidLib project. However, when I tried that, I get an error in the MainActivity class, complaining it can't find LibClass. Looking at the Projects window, I can see LibClass.class inside the com.ex package in the JavaLib.jar in the Libraries folder of the AndroidLib project. And AndroidLib is visible in the Libraries folder of the AndroidApp project, but it doesn't show any packages or other contents there.

所以我觉得我只是一个步骤就可以做这项工作。我需要做一些与一个或其它AndroidManifest的文件吧?还是用的build.xml文件的东西吗?还是我在错误的轨道上一共?

So I feel like I'm just one step away from making this work. Do I need to do something with one or other of the AndroidManifest files perhaps? Or do something with the build.xml files? Or am I on the wrong track altogether?

我会很感激,如果有人可以张贴如何做这个。

I'd be really grateful if someone could post a how-to for this.

推荐答案

我想类似的东西;我有Java EE的项目,使用Eclipse建成,我想利用一些code从我的Andr​​oid项目。这应该给我一个共享codeBase的,而不是混乱SVN外部的一堆我已经收到忍受。

I'm trying something similar; I've got Java EE projects, built using Eclipse, and I'm trying to utilize some of that code from my Android projects. This should give me a shared codebase rather than a bunch of confusing SVN externals which I've had to endure before.

而不是创建JAR文件,我发现,与源和建筑为平台的工作效果最好(当然,它一直在努力,但我已经得到的时刻出了问题)。所以,我在做什么是:

Rather than creating JAR files I've found that working with the source and building for the platform works best (well, it has been working but I've got a problem with it at the moment). So, what I'm doing is:

c:\MySvnFolderStructure\MyJavaProjectFolder\src\     (and then all the source under that)
c:\MySvnFolderStructure\MyJavaProjectFolder\android\ (and all the Eclipse Android project gubbins)
c:\MySvnFolderStructure\MyJavaProjectFolder\jee\     (and all the Eclipse JEE project gubbins)

而Android和Java EE项目并没有自己的src的文件夹,在他们的父文件夹他们都链接到src文件夹。这意味着,每个Java实现的是从源建立自己的字节code版本,并且使用其自己的外部库(比如将Apache HTTP那些,例如)。

The Android and Java EE projects do not have their own src folders, they both link to the src folder in their parent folder. What this means is that each of the Java implementations is building its own byte code version from the source, and using its own external libraries (like the Apache HTTP ones, for example).

当然,他们不能分享这样的东西(如在另一篇文章中提到)AWT,但有很多东西,做交叉尤其是如果它正在使用的核心Java类。

Naturally they can't share stuff like awt (as mentioned in another post), but there's plenty of stuff that does cross-over especially if it's core Java classes that are being used.

此外,它的证明有点棘手编写JUnit测试,需要有测试code上有一些重复的时刻,因为Android的那些需要额外的仪表,但我的工作就可以了。

Also, it's proving a bit tricky writing JUnit tests as there needs to be some duplication of the test code at the moment because the Android ones need extra instrumentation, but I'm working on it.

此外,请参阅这篇文章关于相对路径在Eclipse中,这意味着的文件夹可以检查出不同的地方在不同的机器(如我们都与我们的版本控制退房),并仍然能够共享。

Also, see this post about relative paths in Eclipse, which means the folders can be checked-out to different places on different machines (like we all do with our version control check-outs) and still be shared.

这篇关于共享Java库与Android应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆