23 September 2012

Eclipse-NDK error: "Type 'JNICALL' could not be resolved"

It's not a rare case in Android development when you want to include own native library in your application. Also it's a natural desire to automate the building process of this library. I'm developing my Android projects in Eclipse and it was a natural desire to automate process of native building. Thus, I've found the instructions how to combine Eclipse, Android and NDK, and started my experiments.

The first my experiment was successful and I created a project combining all these tools and it was successfully built. But during my second experiment very unusual errors have appeared (although jni.h header is included in the build path):

Figure 1: Errors in Eclipse
Moreover, it was strange because the project had been built with ndk-build command successfully. Thus, I decided that the problem is somewhere in Eclipse.

I should say several words about the procedure which I use for native development. I create Android java project, then I add jni folder to my project. Using javah tool I generate a header file for my native part. Then I convert project to C/C++ project and configure C/C++ build tools for this project (you should have Eclipse CDT plugin). You can easily find the instructions in the Internet.

Returning to the errors in my project. The first my thought was to compare all project settings in a buggy application with the settings in a working project. I compared all properties and I guess that everywhere they were the same (or maybe I missed something).

Then I tried to look for something similar in the Internet and SO. I've found two similar questions on SO but the solutions did not solve the problem. Hence, this also didn't help me. Thus, I decided to make my own investigation. After several hours of tries, I realized that the problem somehow connected with __cplusplus directive (and in general with C++). I should mention that I developed a C project. Thus, somehow Eclipse had decided that I was developing a C++ project instead of C project. I repeat the steps of converting a project into C/C++ project and found that by default Eclipse converts a project into C++ project and after this it considers it as a C++ project. That's why I receive the errors. Here is a screenshot that shows that step:


To solve this problem you have two options: either to recreate your project from the scratch or to delete one line from your .project file. I think that nobody wants to spend his time on coping files in a new project so I put here the instructions what need to be deleted:

  1. Go to you project directory.
  2. Find .project file. I'm developing in Linux, so it's a hidden file. So you need to make it visible.
  3. Open it with your editor.
  4. Find section <natures>
  5. Then delete a line <nature>org.eclipse.cdt.core.ccnature</nature>
  6. Check that you have a line <nature>org.eclipse.cdt.core.cnature</nature> (with one c!!!)
  7. Save file and restart Eclipse.
Here are two parts of my .project file: before correction and corrected:
Before:

<natures>
 <nature>com.android.ide.eclipse.adt.AndroidNature</nature>
 <nature>org.eclipse.jdt.core.javanature</nature>
 <nature>org.eclipse.cdt.core.cnature</nature>
 <nature>org.eclipse.cdt.core.ccnature</nature>
 <nature>org.eclipse.cdt.managedbuilder.core.managedBuildNature</nature>
 <nature>org.eclipse.cdt.managedbuilder.core.ScannerConfigNature</nature>
 <nature>edu.umd.cs.findbugs.plugin.eclipse.findbugsNature</nature>
</natures>

After:

<natures>
 <nature>com.android.ide.eclipse.adt.AndroidNature</nature>
 <nature>org.eclipse.jdt.core.javanature</nature>
 <nature>org.eclipse.cdt.core.cnature</nature>
 <nature>org.eclipse.cdt.managedbuilder.core.managedBuildNature</nature>
 <nature>org.eclipse.cdt.managedbuilder.core.ScannerConfigNature</nature>
 <nature>edu.umd.cs.findbugs.plugin.eclipse.findbugsNature</nature>
</natures>

2 comments:

  1. I just found out that Eclipse (I'm using 3.8.1) has an editor for .project files. Open the Navigator view to see the .project file and then open the .project file. I don't know if all changes would take effect immediately so it might be best to close and reopen the project after editing.

    ReplyDelete
  2. I have tried following your blog step by step but problem not solved.
    I am still getting
    - Method 'GetObjectClass' could not be resolved
    - Type 'jclass' could not be resolved
    error. Please help me solve this issue

    ReplyDelete