Monday, December 21, 2009

Howto setup a android project in Eclipse that access both Java and C/C++ code

If you follow the instructions on http://source.android.com/using-eclipse you will only have a Java project and will only see the C/C++ project files as 2:nd class citizens without any indexing. I will propose a another solution that will combine the ideas from http://lorinc.blogspot.com/2006/10/how-to-mix-java-and-c-code-in-singe.html.
1. Copy the already available Java setup
$ cd <your mydroid folder>
$ cp development/ide/eclipse/.classpath .
# You might need to make the copy writable (describe on android.com) 
$ chmod u+w .classpath 
2. From Eclipse create a new C++ project File -> New -> Other, select C/C++ -> C++ Project give it a project name and uncheck "Use default location"
and give it the path to your directory (often called the mydroid dir)
Select Project Type: "Makefile project" -> "Empty Project", let the "-- Other Toolchain" be selected and press "Finish"

3. Add in Java to the same project
Close Eclipse as you will edit a file it holds open and will overwrite otherwise Now a /.project file has been created open it in a text editor and add the following:
<buildCommand>
    <name>org.eclipse.jdt.core.javabuilder</name>
    <arguments></arguments>
</buildCommand>
and
<nature>org.eclipse.jdt.core.javanature</nature>
as described here:
<?xml version="1.0" encoding="UTF-8"?>
<projectdescription>
    <name>android zoom2</name>
    <comment></comment>
    <projects>
    </projects>
    <buildspec>
------------------------------- below is added 
        <buildCommand>
            <name>org.eclipse.jdt.core.javabuilder</name>
     <arguments>
     </arguments>
        </buildCommand>
------------------------------- end of the added stuff 
        <buildcommand>
            <name>org.eclipse.cdt.managedbuilder.core.genmakebuilder</name>
            <triggers>clean,full,incremental,</triggers>
...
        </buildCommand>
    </buildSpec>
    <natures>
        <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>
------------------------------- below is added 
 <nature>org.eclipse.jdt.core.javanature</nature>
------------------------------- end of the added stuff 
    </natures>
...
Now start eclipse again, now the Java classes are available in "Project Explorer" view and C/C++ files in "C/C++ Projects" view.

One problem remain when you build it will use "make all" and that will not work and need to be changed to "make" this can be done if you right-click on the project and select "Properties"
In the "Properties" window select "C/C++ Build" and click on the "Behaviour" tab and remove "all" from all the places where available.

I still used the java heap enlargement in eclipse.ini from the first link by changing the numbers in eclipse.ini to the numbers below:
-Xms128m
-Xmx512m
-XX:MaxPermSize=256m

The standard eclipse from Ubuntu 9.10 (installed from synaptic) uses this file /usr/lib/eclipse/eclipse.ini you need to be root to edit it e.g.

$ sudo gedit /usr/lib/eclipse/eclipse.ini

/Zingo

2010-12-08 Edit: removed wrongly formated spaces around < and >

4 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Really good.
    Acooding to this article, I made android project.
    But the code in C or C + + trace (same as pressing F3 key in eclipse), it does not.
    Please help me.

    sdop25@gmail.com

    ReplyDelete
  3. "But the code in C or C + + trace (same as pressing F3 key in eclipse), it does not."

    How do you mean? I get tranfered to the declaration when pressing F3 is that not working on your setup. I assume you have waited the "long" time it takes for indexing all c/c++ files.

    /Zingo

    ReplyDelete
  4. Nice work and you explained it really well. C++ in Urdu

    ReplyDelete