0

I've been trying to get ANTLR-3.5-complete.jar to work on an Android app, but I can't get it to work! Bart Kiers's answer HERE has helped me a lot, but I still can't get it to work.

I'm trying to make a Button that, when clicked, will validate whether the text in the EditText view is correct according to my grammar.g, but it doesn't matter whether the text I input is correct or not, my app always crashes when I click the button while instantiating the ANTLRStringStream. Here's what I have:

public void onClickVerify(View v) throws TypeNotPresentException {
    String source = "foobar";
    Log.e("TestDebug", "Instantiating views");
    TextView out = (TextView) findViewById(R.id.textView1);
    EditText in = (EditText) findViewById(R.id.editText1);
    try {
        Log.e("TestDebug", "Getting source from EditText");
        source = in.getText().toString();
        Log.e("TestDebug", "source = " + source);
        Log.e("TestDebug", "Instantiating stream");
        ANTLRStringStream stream = new ANTLRStringStream(source);
        Log.e("TestDebug", "Instantiating lexer");
        grammarLexer lexer = new grammarLexer(stream);
        Log.e("TestDebug", "Instantiating parser");
        grammarParser parser = new grammarParser(
                new BufferedTokenStream(lexer));
        Log.e("TestDebug", "Applying rule to parser");
        out.setText(source + " = " + !parser.failed());
    } catch (IllegalStateException ise) {
        out.setText("Exception caught");
    }
}

And here's what I see in the logs:

  • Instantiating views
  • Getting source from EditText
  • source = test33
  • Instantiating stream

And then the "Unfortunately, TestAntlrApp has stopped." window pops up.

EDIT: Here's more of my logcat.

java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3591)
at android.view.View.performClick(View.java:4084)
at android.view.View$PerformClick.run(View.java:16966)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View$1.onClick(View.java:3586)
... 11 more
Caused by: java.lang.NoClassDefFoundError: org.antlr.runtime.ANTLRStringStream
at com.test.antlr.MainActivity.onClickVerify(MainActivity.java:89)

Something about not finding the class ANTLRStringStream class? However there seems to be no errors/warnings in the code and I can even see the class's hierarchy.

Community
  • 1
  • 1
Cramps
  • 464
  • 6
  • 16
  • What happens if you use the ANTLR version mentioned in that previous answer? – Bart Kiers May 01 '13 at 20:11
  • And what exception is thrown? Replace your `catch` with this and post what you see on your log-cat: `catch (Exception e) { Log.e("TestDebug", "Exception caught: ", e); }` – Bart Kiers May 01 '13 at 20:29
  • @BartKiers I'll try changing the ANTLR version and see what happens! I changed the `catch` but the code never gets excecuted. It crashes within the `try` without ever reaching `catch`, so my log output remains the same. Edit: v3.3 seems to have been removed from the ANTLR website. I can only find v3.5. – Cramps May 01 '13 at 20:53
  • The website changed from antlr.org to antlr3.org. So the link is now: http://www.antlr3.org/download/antlr-3.3-complete.jar – Bart Kiers May 01 '13 at 21:06
  • @BartKiers Thanks! I tried it with v3.3 but I still get the same problem. The app simply dies when instantiating `stream`... I can't find a possible explanation as to why this could be happening. – Cramps May 01 '13 at 21:33
  • A `NoClassDefFoundError` occurs when a certain class, `org.antlr.runtime.ANTLRStringStream` in your case, cannot be found by the (Dalvik) JVM. It seems it is not properly packaged inside your APK. Hard to say more about it since I can't see exactly how you're building and running your APK. – Bart Kiers May 01 '13 at 21:48
  • Alright, I worked it out! Turns out I'm not supposed to add my libraries to the Build Path using Eclipse's conventional way of doing it. Instead, I have to put it inside the "libs" folder for it to be packed within the .apk. If I simply add it to the build path, it's omitted. – Cramps May 02 '13 at 01:51

1 Answers1

0

Due to a recent ADT update, you can't just add external libraries to the Build Path using Eclipse's conventional way of doing it. Instead, you have to put it inside the libs folder for it to be packed within the .apk. If you simply add it to the build path, it will be ignored when building your .apk.

Symptom : The app was not recognizing external library classes.

Solution : Put the .jar file in the libs folder.

Cramps
  • 464
  • 6
  • 16