开发者

Can't compile an application using Eclipse, Maven, and the Android Plugin for Maven

I'm trying to create an Android application in Eclipse using the Maven plugin and the m2eclipse-android-plugin as well. Things were going "ok" until recently. I'm using Helios on Ubuntu and have the latest JDK (removed the default one installed by Ubuntu).

The project references two libraries that I've also created. One is an Android specific utility project and generates the .apklib (successfully). The other library is a more general purpose set of utilities not specific to Android which produces a JAR file. Both of these projects are also built using the Maven plugin for Eclipse. In addition, I've verified that both the .apklib and .jar files are in the local repository and both included all of the generated class files as would be expected.

When it goes to build the .apk file, I'm getting a "cannot find symbol" on a class in my Android project where the symbol is a class from the non-Android utility JAR file. For some completely bizarre reason, the class file cannot be found insi开发者_JAVA百科de the JAR file. I verified that, in fact, the JAR file is in my local maven repository and that the class file is in the JAR file. I've also run the maven install command with debugging on, copied the command line that gets fed into the Java compiler. When I execute that command in a console, I receive the SAME error (indicating that it's a Java compiler error and not a Maven error).

Has anyone else run into this type of situation before? It's extraordinarily strange and I've completely combed the command line for potential issues and, best as I can tell, everything seems correct.


Well, through what appears to be trial and error I seem to have fixed the problem. I had a file that looked "similar" to this:

import Test.TestObserver;
import com.myself.ImportedClassThatCouldntBeFound;

class Test extends ImportedClassThatCouldntBeFound {
  public interface TestObserver {
    public void event ();
  }

  public void addObserver (TestObserver observer) {
    ...
  }
} 

public class AnotherTest {
  private Test test = new Test ();

  public void blah () {
    this.test.addObserver (new TestObserver () {
      public void event () {
        ...
      } 
    });
  }
}

The problem happened at the TOP of the file. For some reason, Eclipse imported the inner interface!

When I "REMOVED" that import, and then changed AnotherTest to:

public class AnotherTest {
  private Test test = new Test ();

  public void blah () {
    this.test.addObserver (new Test.TestObserver () {
      public void event () {
        ...
      } 
    });
  }
}

it compiled correctly! I even verified it by putting the import BACK into the file and removing the fully declared interface name and it caused it to fail again! It's definitely one of the craziest compiler issues I've ever seen and once I get back the FOUR HOURS of my life that I lost researching this, I'll do more investigation into why this is occurring.

This will be the first time I do this on StackOverflow, but I'm going to mark this as the solution because it most definitely was the issue. However, it definitely requires more research (at least on my part) to try and understand what was causing the compiler to become so confused.

edited this to make it apparent that the class that had the inner interface was extending the class that could not be found when compiled


To me, it looks like a problem caused (ultimately) by putting two top-level classes into a single source code file. This is generally thought to be bad practice.

It is not clear whether the compilation error is mandated by the JLS, whether it is a bug in the Java compiler. But either way, the best fix is to not put multiple classes into one source file.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜