What's the best way to go about this? I've never coded using two languages before, let alone for an android phone
-
1\$\begingroup\$ What's your goal exactly? Do you want to implement performance critical parts in C++, or why do you want to write in both languages? \$\endgroup\$– bummzackAug 28, 2011 at 9:25
-
\$\begingroup\$ yes, the most important areas. \$\endgroup\$– blissfreakAug 28, 2011 at 9:57
1 Answer
Using the Android NDK you can build a native library in C/C++ and by exporting the functions as native you can call your functions as if it was standard Java code once you've initialised the library. There's plenty of tutorials available on getting started with the NDK as well as samples provided by Google.
It's important to note that the NDK isn't necessary for a lot of purposes, see the FAQ:
When to Develop in Native Code
The NDK will not benefit most applications. As a developer, you need to balance its benefits against its drawbacks; notably, using native code does not result in an automatic performance increase, but always increases application complexity. In general, you should only use native code if it is essential to your application, not just because you prefer to program in C/C++.
Typical good candidates for the NDK are self-contained, CPU-intensive operations that don't allocate much memory, such as signal processing, physics simulation, and so on. Simply re-coding a method to run in C usually does not result in a large performance increase. When examining whether or not you should develop in native code, think about your requirements and see if the Android framework APIs provide the functionality that you need. The NDK can, however, can be an effective way to reuse a large corpus of existing C/C++ code.
As for developing on an Android device, I would suggest downloading the SDK (and NDK if your application requires), getting the emulator working, and having a go at the tutorials, reading the technical articles for best practices, and making sure you understand the application fundamentals.