0

I have apps A and B. I want one to use some methods A in B. Is that possible?

I tried adding project A to the build path of B. Compiler is OK with that, but I get runtime NoClassDefFoundError (for the first used class from project A).

I'm making this with Eclipse.

Edit: The reason I need to to that is because my project B is a test of project A. It was not possible to test the usual way, because between other things I have to test AsynchTasks, and that doesn't work with JUnit. So I thought making normal Android project B containing custom test. But since that also doesn't seem to work, I came back to my original test project and implemented there my custom / not JUnit tests. In an ugly way, but it works.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
User
  • 31,811
  • 40
  • 131
  • 232
  • do you want to implement communication between two apps (ie app a influences app b) or do you need code reuse (ie both apps have similar complicated calculation) ? – k3b May 07 '12 at 08:11
  • http://stackoverflow.com/questions/10478126/possible-android-project-depends-of-other-non-library-android-project – Ciro Santilli OurBigBook.com Feb 19 '16 at 20:33

1 Answers1

2

What you are describing is a Library Project. If you have source code and resources that are common to multiple Android projects, you can move them to a library project so that it is easier to maintain across applications and versions. Thus, if you are developing multiple related applications that use some of the same components, you would move the redundant components out of their respective application projects and create a single, re-useable set of the same components in a separate library project.

Thus, what you are describing is almost possible, but not quite. Eclipse doesn't let you reference Android application projects (i.e. projects that compile to a .apk file) as library projects, so simply making project A a library project and having B reference A won't work. Instead, you will need to,

  1. Create a new library project C.
  2. Move the common methods/classes from projects A and B to library project C.
  3. Have both A and B reference library project C.
Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
  • The reason I need to to that is because my project B is a test of project A. It was not possible to test the usual way, because between other things I have to test AsynchTasks, and that doesn't work with JUnit. So I thought making normal Android project B containing custom test. But since that also doesn't seem to work, I came back to my original test project and implemented there my custom / not JUnit tests. In an ugly way, but it works. – User May 07 '12 at 08:12
  • Sorry, I'm not sure I understand. What is preventing you from doing all of the testing within project `A` itself? It seems strange that you have to create an entirely new project `B` in order to test project `A`. – Alex Lockwood May 07 '12 at 08:14
  • Well, because it seems to be the "recommended" way http://developer.android.com/resources/tutorials/testing/helloandroid_test.html And if I do my test in the Android app will it not be exported in the apk? And how do I start it without being linked to an activity? – User May 07 '12 at 08:15
  • Oh, OK. You should clarify in your post that `B` is a test project, (otherwise people will assume it is an ordinary Android application). – Alex Lockwood May 07 '12 at 08:20
  • I think you should re-word your question and add a little more detail :). It is difficult to know what is wrong because the original post doesn't mention test projects and the only information you have given us is that you are getting a `NoClassDefFoundError` exception. Have you read the instructions on making an [Android test project](http://developer.android.com/guide/topics/testing/testing_android.html#TestProjects) on the developers site? – Alex Lockwood May 07 '12 at 08:24
  • Yeah that's why I'm writing that it was not possible the "usual way". I didn't explain at the beginning because the question was very concrete. If this certain thing is generally possible. – User May 07 '12 at 08:40
  • I'm not sure what "certain thing" you are talking about either... maybe you could expand on that too. – Alex Lockwood May 07 '12 at 08:44
  • If it's possible that an Android project depends of other (non library) Android project. It's a very concrete question. – User May 07 '12 at 08:59