开发者

Using linked sources with Android projects

I need to use a java source file in two my Android projects (Eclipse Helios 3.6.1). The source file (let's na开发者_运维问答me it "library") contains a public class with public routines, it does not use any resources. I placed the library file in a separate directory from both projects.

To include the library in each project, I added the folder with the library to Project properties \ Java Build Path \ Source \ Link Source. The library was added to the project tree, but compiler forced me to remove "package" declaration from the library, which is OK for me.

The problem is that I don't know how to reference the library from the projects sources. Which namespace should be used? What should be placed in "import" clause? I seached a lot, but found nothing helpful (may be I used a wrong key words for the search).

The library excerpt (no "package" clause at the beginning!):

public class Misc {
    public static void myRoutine() {
    }
}

A project code (no "import" clause):

public class MyActivity extends Activity {
    public onEvent() {
        myRoutine();
        Misc.myRoutine();
    }
}

Compiler does not allow me to use myRoutine() nor Misc.myRoutine(). Is there a workaround?


The easiest and best way is to set up the classes you want to re-use as a library project in Eclipse and then add a reference to it in your project on the Android tab in Properties.


First, you should not remove the package declaration. Second, you should be able to compile this class into a jar and add it in the way you mentioned above. Everything else failing you should be able to create a separate project in Eclipse for this and make this new library project a dependency for your main project. Try these options, then it should work.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜