Android project cannot reference another project in eclipse

A has an Android project called TestAndroid. I can run it without problems on my Android device. However, I want to be able to use code from another project. So I go to my build path and add the Test project. Test is a standard java project using java6. The Test project has a Test class, which is just an empty class.

Here is where this happens, although when I create a Test instance in TestAndroid, I get a runtime error. Here is the error I get from logcat.

05-27 21:47:49.976: E/dalvikvm(27493): Could not find class 'com.tests.eclipseisbroken.Test', referenced from method com.tests.eclipseisbroken.TestAndrodiActivity.onCreate 05-27 21:47:49.986: W/dalvikvm(27493): VFY: unable to resolve new-instance 11 (Lcom/tests/eclipseisbroken/Test;) in Lcom/tests/eclipseisbroken/TestAndrodiActivity; 05-27 21:47:49.986: D/dalvikvm(27493): VFY: replacing opcode 0x22 at 0x0008 05-27 21:47:49.986: D/dalvikvm(27493): VFY: dead code 0x000a-000d in Lcom/tests/eclipseisbroken/TestAndrodiActivity;.onCreate (Landroid/os/Bundle;)V 05-27 21:47:50.026: D/AndroidRuntime(27493): Shutting down VM 05-27 21:47:50.046: W/dalvikvm(27493): threadid=1: thread exiting with uncaught exception (group=0x40028a00) 05-27 21:47:50.056: E/AndroidRuntime(27493): FATAL EXCEPTION: main 05-27 21:47:50.056: E/AndroidRuntime(27493): java.lang.NoClassDefFoundError: com.tests.eclipseisbroken.Test 05-27 21:47:50.056: E/AndroidRuntime(27493): at com.tests.eclipseisbroken.TestAndrodiActivity.onCreate(TestAndrodiActivity.java:16) 05-27 21:47:50.056: E/AndroidRuntime(27493): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1065) 05-27 21:47:50.056: E/AndroidRuntime(27493): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2745) 05-27 21:47:50.056: E/AndroidRuntime(27493): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2797) 05-27 21:47:50.056: E/AndroidRuntime(27493): at android.app.ActivityThread.access$2300(ActivityThread.java:135) 05-27 21:47:50.056: E/AndroidRuntime(27493): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2132) 05-27 21:47:50.056: E/AndroidRuntime(27493): at android.os.Handler.dispatchMessage(Handler.java:99) 05-27 21:47:50.056: E/AndroidRuntime(27493): at android.os.Looper.loop(Looper.java:143) 05-27 21:47:50.056: E/AndroidRuntime(27493): at android.app.ActivityThread.main(ActivityThread.java:4914) 05-27 21:47:50.056: E/AndroidRuntime(27493): at java.lang.reflect.Method.invokeNative(Native Method) 05-27 21:47:50.056: E/AndroidRuntime(27493): at java.lang.reflect.Method.invoke(Method.java:521) 05-27 21:47:50.056: E/AndroidRuntime(27493): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) 05-27 21:47:50.056: E/AndroidRuntime(27493): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 05-27 21:47:50.056: E/AndroidRuntime(27493): at dalvik.system.NativeStart.main(Native Method) 

I think something is wrong with my eclipse installation because I had a project, I had to reference another project, and it worked. I would rather fix this now to completely reinstall eclipse and all my plugins. If someone knows what happened and could help me, I would really appreciate it. Thank you !!

+6
source share
1 answer

Suppose you are using the latest version of the Android SDK and ADT, and you want to add the standard java B project as a dependency in the android A project:

  • Add project B to project A build path: Properties -> Java Build Path -> Projects -> Add ...
  • Mark project B in the export list of project A build: Properties -> Java Build Path -> Order and Export

Now you can use the class from the standard java library in your Android project and build / run / debug it in Eclipse.

Hope this helps.

UPDATE: the above operations just add one line to .classpath

 <classpathentry combineaccessrules="false" exported="true" kind="src" path="/projB"/> 
+9
source

Source: https://habr.com/ru/post/916808/


All Articles