Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Open Declaration' in Eclipse navigates to class files instead of to source files for Android Libraries

Tags:

I have two Android Projects 'A' and 'B'. 'B' depends on 'A' and is a Library. Whenever I'm in Project 'B' and hit 'Open Declaration' for a Java source file which resides in 'A', Eclipse navigates to the class file instead of the source file. This of course has several disadvantages!

I had a look at the build path of project 'B' to see if the referenced library 'A' has the correct path to it's source project which is the case.

Anyone knows how to fix that?

Here are the .classpath and project.properties files for each project:

ProjectA .classpath

<?xml version="1.0" encoding="UTF-8"?>   <classpath>   <classpathentry kind="src" path="src"/>   <classpathentry kind="src" path="gen"/>   <classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>   <classpathentry kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>   <classpathentry kind="output" path="bin/classes"/> </classpath> 

ProjectA project.properties

# This file is automatically generated by Android Tools. # Do not modify this file -- YOUR CHANGES WILL BE ERASED! # # This file must be checked in Version Control Systems. # # To customize properties used by the Ant build system use, # "ant.properties", and override values to adapt the script to your # project structure.  # Project target. target=android-8 android.library=true 

ProjectB .classpath

<?xml version="1.0" encoding="UTF-8"?> <classpath>   <classpathentry kind="src" path="src"/>   <classpathentry kind="src" path="gen"/>   <classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>   <classpathentry kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>   <classpathentry kind="output" path="bin/classes"/> </classpath> 

ProjectB project.properties

# This file is automatically generated by Android Tools. # Do not modify this file -- YOUR CHANGES WILL BE ERASED! # # This file must be checked in Version Control Systems. # # To customize properties used by the Ant build system use, # "ant.properties", and override values to adapt the script to your # project structure.  # Project target. target=android-8 android.library.reference.1=../ProjectA 
like image 839
Andreas Avatar asked Jan 26 '12 10:01

Andreas


People also ask

How do I enable open declaration in Eclipse?

Right Click on the project -> Properties -> Project Facets -> Click on the Configuration Link -> Click on Apply Button -> Click on OK button.

What is open declaration in Eclipse?

You can select an element name in your code and quickly navigate to its declaration. Open declaration will attempt to navigate to the exact definition of the selected element if the selected element is a reference or a declaration.

Can we run class file in Eclipse?

You can't use Eclipse to run an external class file with a main method without creatin a java project with this class in its class path. This would give Error: Could not find or load main class classname.

How do I debug a .class file in Eclipse?

There are two ways to debug a class file. The first way is to set the decompiler preference, and to realign the line number. The second way is to check the debug mode in the decompiler menu bar. When your Eclipse workspace is in debug perspective, the debug mode becomes the default.


2 Answers

You can import the library project not only as an Android Lib (that is the way it's meant to be done), but also as a linked project in the usual "Java Build Path" menu. Next, be sure to move this import to the top (at least more that "Library projects" entry) in the "Order and export" tab.

This works, but it a hack and it's dirty. This is not the way it is meant to be done... and when you deploy the app to the phone, eclipse will write a error message in the console saying that some apk cannot be found...

Still, I find it more convenient that linking to the class file.

I don't know what this is happening. My buddies in the office are running the same configuration than me, and they have not this issue, but it seems we are not the only ones whith this problem: http://code.google.com/p/android/issues/detail?id=20731

like image 87
GaRRaPeTa Avatar answered Nov 19 '22 22:11

GaRRaPeTa


I had the same problem when my version of library project A was in "Properties > Java Build Path > Libraries", but not in "Properties > Java Build Path > Projects".

The problem was solved after I did the following:

  1. Remove "Library Projects" from "Properties > Java Build Path > Libraries", which was added by "Properties > Java Build Path > Libraries" (I am not sure if this step is necessary, but this is what I did).
  2. Add project A to "Properties > Java Build Path > Projects".
  3. Restart Eclipse (Indigo). Somehow Eclipse recreates Library Projects in "Properties > Java Build Path > Libraries"

The .classpath should have the following:

<classpathentry combineaccessrules="false" kind="src" path="/A"/> 

Actually navigating to class files was not the most annoying, but breaking in class files instead of java files during debugging was driving me crazy before this problem was solved.

like image 22
Hong Avatar answered Nov 19 '22 23:11

Hong