Ccna final exam - java, php, javascript, ios, cshap all in one. This is a collaboratively edited question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
Friday, June 1, 2012
Write applications in C or C++ for Android?
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?
For anyone coming to this via google, note that starting from SDK 1.6 Android now has an official native SDK. There doesn't seem to be a general page for the 'NDK', but the download page for the first release is here.
You can develop native applications for Android, I have done some work in that area. The best link for you is Android NDK page. In brief, the easiest way for you would be to use some JNI wrapper to start and navigate your application. For this work, you should can also be using Android compiler that is available along with Android source code. An old alternative is CodeSourcery, but it's better to use the native prebuilt compiler. You will have to use Bionic C-library, and accommodate needs of your application to the existing features in C/C++ on Android. Some Linux IPC is missing, such as System V, but your C/C++ application probably isn't using that anyway. Note that in your research you should avoid articles that refer to older versions of Android SDK. So, avoid all links from 2008. or 2007.
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.
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.
"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."
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.
Install Google Android NDK. It contains libs, headers, makfile examples and gcc toolchain Build an executable from your C code for ARM, optimize and link it with provided libs if required Connect to a phone using provided adb interface and test your executable
If you are looking to sell an app:
Build a library from your C code Create simple Java code which will use this library Embed this library into application package file Test your app Sell it or distribute it for free
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.
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.
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".
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.
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.
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
@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.
For anyone coming to this via google, note that starting from SDK 1.6 Android now has an official native SDK. There doesn't seem to be a general page for the 'NDK', but the download page for the first release is here.
ReplyDeleteYou can develop native applications for Android, I have done some work in that area.
ReplyDeleteThe best link for you is Android NDK page.
In brief, the easiest way for you would be to use some JNI wrapper to start and navigate your application. For this work, you should can also be using Android compiler that is available along with Android source code. An old alternative is CodeSourcery, but it's better to use the native prebuilt compiler.
You will have to use Bionic C-library, and accommodate needs of your application to the existing features in C/C++ on Android. Some Linux IPC is missing, such as System V, but your C/C++ application probably isn't using that anyway.
Note that in your research you should avoid articles that refer to older versions of Android SDK. So, avoid all links from 2008. or 2007.
Google has released a Native Development Kit (NDK) (according to http://www.youtube.com/watch?v=Z5whfaLH1-E at 00:07:30).
ReplyDeleteHopefully 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.
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++.
ReplyDeleteBUT, 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
Looking at this it seems it is possible:
ReplyDeletehttp://openhandsetmagazine.com/2007/11/running-c-native-applications-on-android-the-final-point/
"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."
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.
ReplyDeleteI 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.
Normally, you have to:
ReplyDeleteInstall Google Android NDK. It
contains libs, headers, makfile
examples and gcc toolchain
Build an executable from your C code
for ARM, optimize and link it with
provided libs if required
Connect to a phone using provided
adb interface and test your
executable
If you are looking to sell an app:
Build a library from your C code
Create simple Java code which will
use this library
Embed this library into application
package file
Test your app
Sell it or distribute it for free
Take a look at google ndk group it looks promising, first version of the NDK will be available in 1H2009.
ReplyDeleteUpdate:
And it is released http://android-developers.blogspot.com/2009/06/introducing-android-15-ndk-release-1.html
Google just released the NDK which allows exactly that.
ReplyDeletehttp://feedproxy.google.com/~r/blogspot/hsDu/~3/2foWz7hwFtE/introducing-android-15-ndk-release-1.html
It can be found here:
http://developer.android.com/sdk/ndk/1.5_r1/index.html
I'm not sure the NDK provides full coverage of the official Java API.
ReplyDeleteFrom 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.
Maybe you are looking for this?
ReplyDeletehttp://www.mosync.com/
It is a middle layer for developing for several mobile platforms using c++.
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
ReplyDeletewhich is good to understand the use of NDK for writing application in c and c++ for android.
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.
ReplyDeletefind / -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!
This blog post may be a good start: http://benno.id.au/blog/2007/11/13/android-native-apps
ReplyDeleteUnfortunately, lots of the important stuff is "left as an exercise to the reader".
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.
ReplyDeleteSince 2009 there is a development on this matter.
ReplyDeleteNecessitas - Qt(C++ framework) for Android
Getting started video.
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.
ReplyDeleteThis three steps are good to have and store in this post.
ReplyDelete1) How to port native c code on android
2) http://www.integratingstuff.com/2010/12/12/calling-native-c-code-through-jni-in-android-applications/
3) http://mindtherobot.com/blog/452/android-beginners-ndk-setup-step-by-step/
Native C/c++ Files libstdc++.* from your Ubuntu are x86 (or x86_64)
ReplyDeletebinaries 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
Short answer: You can't.
ReplyDelete@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.