How to Create a Cross-Platform App Using C++ Code on Android and iOS

Jun 12, 2022 | Programming

Have you ever wanted to write a single piece of code that can run on multiple platforms? Well, you can! This guide will take you step-by-step through the process of using C++ to create an app that runs on both Android and iOS. This is not just a curiosity; it’s a highly productive approach to app development that promotes code reuse!

Getting Started

This application example utilizes the same C++ code for both an Android and an iOS project, allowing for a seamless development experience across these platforms.

Understanding the Architecture

Let’s visualize how our application is structured through a diagram:

Architecture Diagram

Looking at the diagram, we have three layers: the C++ layer at the foundation, responsible for containing all shared logic, while the top layer consists of the platform-specific code (iOS uses Objective-C or Swift, and Android uses Java or Kotlin). The middle layer is where things get interesting and requires some key configurations.

Step-by-Step Guide

1. Shared C++ Code

First, we’ll set up our shared C++ code. This includes a header file CPPCore.h with the method declarations and an implementation file CPPCore.cpp.

2. Compiling for Unix-like Systems

Before diving into mobile platforms, you can quickly test your shared code on Unix or Mac systems. Create a Main.cpp that utilizes your C++ functions.

To compile it, use the provided shell script:

$ sh compileToUnix.sh
Compiled, now execute using .main
$ .main cpp says hello to Unix

3. iOS Application Integration

iOS has a relatively straightforward approach to integrate C++. You simply change your .m files to .mm files (which enables Objective-C++). Implement your interface in a wrapper class CoreWrapper.mm to convert C++ types into Objective-C.

For your view controller, simply call the wrapper function to populate your storyboard:

Xcode Screenshot iPhone App Screenshot

4. Android Application Integration

Switching gears to Android, the first step is to configure CMake in your build.gradle file and create a corresponding CMakeLists.txt to include your C++ files.

In Android, you’ll also create a class CoreWrapper.java to serve as a bridge to access your native C++ code using the Java Native Interface (JNI).

Here’s where it gets curious: JNI requires you to follow a specific naming convention for methods, namely Java_package_class_method. In our case:

Java_ademar_androidioscppexample_CoreWrapper_concatenateMyStringWithCppString

You’ll pass the necessary arguments through the JNI pointer to Java methods, making sure to convert Java types (like jstring) to C++ types as illustrated in native-lib.cpp.

Android Studio Screenshot Android App Screenshot

5. Bonus: Integrating with Swift and MacOS

Since there’s no direct integration between Swift and C++, we’ll employ Objective-C++ as a bridge. The additional component needed is a bridging header, Bridging-Header.h.

Xcode with Swift Screenshot MacOS App Screenshot

Troubleshooting Common Issues

  • Ensure that you are using the correct file extensions for C++ (.mm for Objective-C++) and native coding (.cpp for C++) to avoid compilation errors.
  • If your methods are not being recognized, double-check the naming conventions for methods and JNI implementations.
  • For complete integration issues, verify that your Gradle build scripts and CMake lists are properly set to include necessary folders and files.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Final Thoughts

By following this structured approach, you can effectively create applications for both Android and iOS that leverage the power of C++. The middle layer of integration may pose challenges, but with patience and attention to detail, you’ll be able to successfully bridge the gap and create a robust cross-platform application.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions.

Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox