Write applications in C or C++ for Android?

Go To StackoverFlow.com

279

I'm trying to develop/port a game to Android, but it's in C, and Android supports Java, but I'm sure there must be a way to get a C app on there, anyone knows of a good tutorial on the subject?

Thanks!

2009-06-16 15:14
by Robert Gould
only if you jailbreak the phone AFAIK - Cogsy 2009-06-16 15:16
Isn't jailbraking for iPhones - Robert Gould 2009-06-16 15:19
The G1 doesn't allow root access by default, and there are a lot of restrictions in place regarding what can be executed outside of the 'Android' environment.

I wouldn't have much hope finding a way to execute native code from within Android - Cogsy 2009-06-16 15:23

but I've downloaded DOOM for Android from the marketplace. So it should be doabl - Robert Gould 2009-06-16 15:33
Make your Application a shell the holds a compilre and source. On first launch compile your application on the Android Phone and then execute - Martin York 2009-06-16 17:46
DOOM for Android is written in Java http://code.google.com/p/doom-for-android/source/browse/#svn/trun - Marco Mustapic 2009-06-26 01:07
@Marco M. Wow, is all I can say. Can't believe somone would ave rewritten it, but it seems your right!! - Robert Gould 2009-06-26 07:27
@Robert Gould, many people have re-written DOOM, I personally know of 2 people who have done so. Its open source, so its a good project for people starting off in games programming as they can see how it was originally done as a reference (plus, it gives them something to aim for and something rather flashy to show off at the end) - Grant Peters 2010-07-30 06:47
you can also use C++ builderA-Sharabiani 2015-06-27 21:48
the question is long answered, but there's no reason for it being closed as "not constructive - Anne Quinn 2016-05-04 12:38
@Clairvoire The question is off topic by current rules as its asks for a tutorial. If you really believe it should be reopened then I suggest you try and persuade a moderator to reopen it; there are some old but (currently) off topic question that are open, they include a message about their historical significance. Perhaps you could ask on meta.stackoverflow.com - AdrianHHH 2016-05-04 14:31


240

For anyone coming to this via Google, note that starting from SDK 1.6 Android now has an official native SDK.

You can download the Android NDK (Native Development Kit) from here: https://developer.android.com/ndk/downloads/index.html

Also there is an blog post about the NDK:
http://android-developers.blogspot.com/2009/06/introducing-android-15-ndk-release-1.html

2009-10-15 11:00
by Andy Johnson
OK, so now how about C# and .Net?- - Avi 2009-10-28 07:34
Can't see .net support ever happenin - vickirk 2010-01-18 21:45
@vickirk why not? Mono was ported to the iPhone - Prof. Falken 2010-01-18 22:44
I was talking about the android SDK, sure there is nothing stopping third parties porting it to android phones - vickirk 2010-01-19 09:24
@Avi http://mono-android.ne - Tomas Andrle 2011-04-12 14:44
Please refer to this link http://androidsnippets.wordpress.com/2012/08/30/how-to-run-execute-native-c-code-in-android-using-ndk-in-linux-ubuntu-operating-system - KK_07k11A0585 2012-08-31 06:28
for .net / c# code and android check out xamari - bbqchickenrobot 2012-11-16 07:48


40

The Android NDK is a toolset that lets you implement parts of your app in native code, using languages such as C and C++. For certain types of apps, this can help you reuse code libraries written in those languages.

For more info on how to get started with native development, follow this link.

Sample applications can be found here.

2009-06-25 23:39
by MannyNS
or 2009, 2010, 2011, 2012 or 201 - Christopher Berman 2014-09-23 22:14


17

Normally, you have to:

  1. Install Google Android NDK. It contains libs, headers, makfile examples and gcc toolchain
  2. Build an executable from your C code for ARM, optimize and link it with provided libs if required
  3. Connect to a phone using provided adb interface and test your executable

If you are looking to sell an app:

  1. Build a library from your C code
  2. Create simple Java code which will use this library
  3. Embed this library into application package file
  4. Test your app
  5. Sell it or distribute it for free
2011-02-16 10:03
by psihodelia
i am a new in NDK and i want to run C lan in Android.have any ref link - Parag Chauhan 2012-05-19 20:20


14

Google has released a Native Development Kit (NDK) (according to http://www.youtube.com/watch?v=Z5whfaLH1-E at 00:07:30).

Hopefully the information will be updated on the google groups page (http://groups.google.com/group/android-ndk), as it says it hasn't been released yet.

I'm not sure where to get a simple download for it, but I've heard that you can get a copy of the NDK from Google's Git repository under the donut branch.

2009-06-18 15:03
by Marc


12

The official position seems to be that this isn't something you'd ever "want to do". See this thread on the Android Developers list. Google envisage android running on a variety of different devices (CPUs, displays, etc). The best way to enable development is therefore to use (portable) managed code that targets the Dalvik VM. For this reason, the Android SDK doesn't support C/C++.

BUT, take a look at this page:

Android includes a set of C/C++ libraries used by various components of the Android system. These capabilities are exposed to developers through the Android application framework.

The managed application framework appears to be layered on-top of these libraries. The page goes on to list the C/C++ libs: standard C library, media, 3D, SQL lite, and others.

So all you need is a compiler chain that will compile C/C++ to the appropriate CPU (ARM, in the case of the G1). Some brief instructions on how to do this are here.

What I don't know is where to find descriptions of the APIs that these libraries provide. I'd guess there may be header files buried in the SDK somewhere, but documentation may be sketchy/missing. But I think it can be done!

Hope thats useful. For the record, I haven't written any native android apps - just a few simple managed ones.

Andy

2009-06-16 15:45
by Andy Johnson
Downvoting. The NDK appears to be an official compiler for Android native code now. Making this answer obsolete - unixman83 2012-04-23 22:18


8

You can use nestedvm to translate C (or other GCC languages) into Java bytecode, and use that as the basis of your port. For example, see the Android port of Simon Tathams portable puzzle collection.

I expect this method is made obsolete by the NDK, but it might not be in if some networks or something don't allow people to upgrade their phones.

2009-10-27 23:25
by rjmunro
+1 for an alternative to the NDK, even though the NDK makes it obsolete - tobsen 2010-06-30 20:52
Isn't nestedvm compatible with more devices - charo 2017-01-30 14:11


8

Google has already launched Google I/O 2011: Bringing C and C++ Games to Android session which is available at http://www.youtube.com/watch?v=5yorhsSPFG4

which is good to understand the use of NDK for writing application in c and c++ for android.

If you just want to cross compile any console based native game and run them on android then this Article has shown 3 methods for the same.

1: Static compilation using standalone toolchain

2: Cross compilation using Android NDK’s toolchain

3: Cross compilation using AOSP source code

2012-01-17 18:10
by Jeegar Patel


7

Looking at this it seems it is possible:

"the fact is only Java language is supported doesn’t mean that you cannot develop applications in other languages. This have been proved by many developers, hackers and experts in application development for mobile. The guys at Elements Interactive B.V., the company behind Edgelib library, succeeded to run native C++ applications on the Android platform, even that at this time there is still many issues on display and sound … etc. This include the S-Tris2 game and a 3D animation demo of Edgelib."

2009-06-16 15:35
by backslash17


7

Maybe you are looking for this?

http://www.mosync.com/

It is a middle layer for developing for several mobile platforms using c++.

2010-10-11 23:22
by joepal
link is 404 not found. It is always a good idea to put in some content from the original post into the answer in case the site owner wins the lottery, divorces his wife and moves to Bahamas - Zuzlx 2015-09-08 23:19


6

Since 2009 there is a development on this matter.
Necessitas - Qt(C++ framework) for Android
Getting started video.

2011-07-02 08:08
by zkunov
Good point. Just as an extra note, Necessitas uses the Android NDK - Jaime Ivan Cervantes 2013-02-28 16:23


5

Take a look at google ndk group it looks promising, first version of the NDK will be available in 1H2009.

Update: And it is released http://android-developers.blogspot.com/2009/06/introducing-android-15-ndk-release-1.html

2009-06-16 21:26
by Gordon Freeman


5

I'm not sure the NDK provides full coverage of the official Java API.

From http://developer.android.com/sdk/ndk/index.html#overview :

Please note that the NDK does not enable you to develop native-only applications. Android's primary runtime remains the Dalvik virtual machine.

2010-08-20 02:35
by Android Eve


3



3

You can download c4droid and then install the GCC plugin and install to your SD. From the shell I just traverse to the directory where the GCC binary is and then call it to make an on board executable.

find / -name gcc

/mnt/sdcard/Android/data/com.n0n3m4.droidc/files/gcc/bin/arm-linux-androideabi-gcc

cat > test.c

#include<stdio.h>
int main(){ 
 printf("hello arm!\n");
return 0;
}

./arm-linux-androideabi-gcc test.c -o test

./test

hello arm!

2012-01-19 16:12
by elahtrebor


2

This blog post may be a good start: http://benno.id.au/blog/2007/11/13/android-native-apps Unfortunately, lots of the important stuff is "left as an exercise to the reader".

2009-06-16 15:41
by Krzysiek Goj


2

Native C/c++ Files libstdc++.* from your Ubuntu are x86 (or x86_64) binaries but Android devices and emulators are ARM. Of course, this will not work anyway, even if you'll set correct soname. This is very naive way which leads nowhere. Android has very limited support of C++ meaning there is no exceptions, standard C++ library (including STL) and RTTI. If you need such functionality, use my custom NDK distribution from

http://crystax.net/android/ndk.php - it support full C++ features listed above.

Why is there error: undefined reference to '__cxa_end_cleanup' link error. Android stlport

time. Because there is no link to libstdc + +. A. So wrong.

Because it uses some static library, it is necessary to link the full libstdc + +. A. Can

http://crystax.net/android/ndk.php here to download the package

sources \ cxx-stl \ gnu-libstdc + + \ libs \ armeabi directory.

Android on its own libstdc + + support is limited, it must be linked to a complete libstdc + +. A the job.

Add file in Android.mk LOCAL_LDFLAGS = $ (LOCAL_PATH) / libs / libcurl.a \

 $ (LOCAL_PATH) / libs / liblua.a \
 `$ (LOCAL_PATH) / libs / libstdc + +. A`  

And LOCAL_CPPFLAGS + =-lstdc + +-fexceptions can be compiled

2011-11-18 09:52
by ZhangXuelian


2

I do not know a tutorial but a good development tool: Airplay SDK from Ideaworks Labs. (Recently rebranded "Marmelade") Using C/C++ you can build apps for Windows Mobile, iPhones, Android. The only component I didn't like was the GUI composer - a buggy one, but you always can substitute it with the Notepad.

2011-12-01 22:42
by zergius


2



1

There is a plan to allow C/C++ libraries in the next SDK version of Android (Codename Eclair?)To date, it's not possible through the Android Java SDK. However, you can grab the HUGE open source project, roll your own libraries, and then flash your own device...but anyone who wants to use your library will have to flash your custom build as well.

2009-06-17 19:49
by haseman


-7

Short answer: You can't.

@Backslash17: Looking through the article and the following link with installation instructions, all the company got working is to launch a self compiled executable on the emulator, bypassing the android framework. Once you have the emulator you can telnet in and have a linux shell. Running a linux program there is trivial of course. But that's not working for phones without jailbreak(aka root access) and not deliverable on the market.

Unless Google opens other opportunities or someone writes a custom compiler that compiles Language X into Dalvik bytecode (Dalvik is the VM that runs Android programs) you won't be able to write native code.

2009-06-16 15:55
by buster
Right, that's why I wrote "It seems...". See to believe - backslash17 2009-06-16 16:53
Ads