开发者

How to link an Android library jar to a regular Java project?

First I know that this probably sounds like it isn't the best way to do this but changing the overall design is out of my control so please don't post answers to do it a different way. I have a plain Java project that I am linking to my Android application. I need to use some Android stuff inside the Java project so I just linked to the android.jar file. This worked fine for using the Android library until I try to compile which complains about开发者_JAVA技巧 not having a default.properties file. I tried adding the default.properties file to the root of the project but the problem is still there. Any help on this would be great.

Thanks


changing the overall design is out of my control so please don't post answers to do it a different way.

If what you want is impossible, doing it a different way is the only answer you can get.

I need to use some Android stuff inside the Java project

That is not strictly possible, any more than you can use some Windows stuff in an OS X app. Android is an operating system, not just some Java files.

so I just linked to the android.jar file

This JAR file contains class and method stubs, not the real classes and methods. It is for compilation of Android projects -- the real classes will be part of the runtime classpath on an Android device.

IOW, that JAR will do you no good for a plain Java project.

This worked fine for using the Android library until I try to compile which complains about not having a default.properties file.

It would appear that you are attempting to build a non-Android project with an Android build process (not sure if it's a malformed Eclipse project or you just tried copying over an Ant script from the Android project, based on your description).

Any help on this would be great.

If the "some Android stuff" is pure Java, not dependent upon Android, move it into a JAR or something that both projects can use.

If the "some Android stuff" really refers to Android classes (e.g., android.* packages), that can only be used on an Android device and cannot be used by a plain Java project. You're welcome to poke at the Android source code and see if there are classes that are self-contained that you could copy into the Java project, but you will need to keep that stuff separate from your Android app, particularly if you start modifying that source code.


I do the exact same thing - I have a Java project in Eclipse that I use to provide some common code to all my apps (although I'm moving to an Android library project).

The only reason you'd need to use the properties is if you're mistakenly using the Android pre and post compilers.

If you've created a pure java project this should just work, here are the the definition files for mine:

.project

<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
    <name>Project Name</name>
    <comment></comment>
    <projects>
    </projects>
    <buildSpec>
        <buildCommand>
            <name>org.eclipse.jdt.core.javabuilder</name>
            <arguments>
            </arguments>
        </buildCommand>
    </buildSpec>
    <natures>
        <nature>org.eclipse.jdt.core.javanature</nature>
    </natures>
</projectDescription>

and .classpath

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path="src"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
    <classpathentry kind="lib" path="D:/Android/android-sdk-windows/platforms/android-4/android.jar"/>
    <classpathentry kind="output" path="bin"/>
</classpath>

You can then either use the project as a dependency for other projects or compile it to a jar file to import.

I can't imagine you're actually having problems compiling the plain java project (if it's really plain java) as the stubs will work perfectly to compile against - so is the problem in the Android project that is linking against the Java project?

Try creating a new Android project from scratch, make sure it works and then link it against the java project - there's no reason it shouldn't work.


In order to get the project to compile, you may need to put something in the default.properties file. Mine has this one line (which version of Android is the target)

target=android-8

You may need more stuff, I'm not sure - I had Eclipse create the project for me.

However, you will not be able to get the project to run. As soon as you actually call any of the Android constructors or methods you'll get a Stub! error.

e.g. see How to execute Java tests on Android code

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜