android developer
android developer

Reputation: 116060

How to correctly import an Android library with JNI code?

Background

I've made a tiny SDK for bitmap handling using JNI (link here).

It has only 2 projects : a sample project (demonstrates usage of the SDK), and the SDK itself .

The SDK project is an Android project, and it includes some C/C++ code within it.

The problem

For some reason, even though when I've created the project , everything went fine, now that I try to get the library project and import it to Eclipse, and then I open the CPP file I've made, I see plenty of errors on it, as such:

enter image description here

The question

Why does it occur? How do I import the project correctly? Can I help whoever is using this to be able to import it nicely?

I've tried to create a totally new project with JNI and just copied (carefully) the files from my library, and it compiled fine, but that's not a nice way to import a project...

Upvotes: 9

Views: 2955

Answers (1)

MichaelCMS
MichaelCMS

Reputation: 4763

This eclipse error happens often , and your solution (starting a empty project then adding the files) is the fastest work-around, and we all use it when it comes to this. Another solution would be to remove the c nature and Android nature and add again these natures to the project (often times this works as well).

When a new project is created, Eclipse will add it's natures and it's tool chains. If you import an old project, these toolchains / natures might be badly imported.

For a project containing c and c++ , you will need to have cnature and ccnature defined in your ".project" file. For android you need Android Nature. For compiling android, you will need to have com.android.gcc as the toolchain defined in your .cproject file.

Depending on the version of ADT / CDT / Eclipse , the value from the toolchains in .cproject differs, hence they aren't easy to import from machine to machine.

My guess is you have your storm of errors because the .cproject file was generated wrongfully .

EDIT

After Android Developer's further research, a faster way to bypass Eclipse's limitation caused by how native nature is added to a project can be done in the following steps:

  1. delete from the ".project" file the "cnature" and "ccnature"

    <nature>org.eclipse.cdt.core.cnature</nature>
    <nature>org.eclipse.cdt.core.ccnature</nature>
    
  2. delete the ".cproject" file from eclipse workspace

  3. remove all generated files and folders : libs, obj, gen and bin

  4. Re-add the native support for android via the "android tools" context menu

Thank you android developer.

Upvotes: 5

Related Questions