Products Shop Support Company |

Android Mixed-Mode Apps (3:58)

Download the Video | View on YouTube

This is a video about Elements.
Watch more Elements Videos. Read more about Elements.

Transcript

Elements allows you to use both the Java- based Android SDK as well as the native NDK to build Android apps.

In this video I will show how to set this up, and how to use mixed-mode debugging to debug both the Java-based and the native portion of your app at the same time. I'm using Water, our IDE for Windows, but the same concepts apply to Fire, our IDE for macOS.

We start by creating a new "Android Application" from a template. I'm using C# here, but any Elements language will work. Click OK to save and create the project.

Next, right-click the solution tree and choose to add a second project to the solution. This time we pick an "Android NDK Extension". Name and save it.

It's important to remember that while these are both Elements projects, using the same language, they target vastly different SDKs. The main application is built against Java Runtime while the extension is a native library that will be built directly for the CPU.

Take a look at the main code. Our template already added a simple function, and we'll keep that as is. All we need to do is change the namespace to match that of our main application.

Let's also set a breakpoint here, to test the mix debugging by simply clicking in the gutter.

Now let's switch over to our Java based main app.

In our activity we add an 'extern' function declaration to import the NDK method. Note that the namespace, class name, and method name here must match the one we provided earlier for the Java to native interface, or JNI, to work its magic.

At the beginning of OnCreate, add a call to System.loadLibrary to load in our extension at runtime. After that we can just call the method as we would any other, for example assigning its result to the Activities Title property.

Let's set a second breakpoint here.

Finally, let's create a project reference from the main app to the extension, simply by dragging the extension's project icon down on to the app. With a project reference, we tell the build chain all it needs to know to build both projects and to properly bundle the extension inside the APK it creates for our app.

Let's select our Android simulator from the device drop-down at the top and press Ctrl-R to run.

As you can see, the Android emulator boots up and our app launches. The first launch of a mixed-mode debug session might take a few moments longer as symbols are downloaded from the device.

After a second or two, we hit our first breakpoint. Looking at the call stack on the left or the Debug Inspector, you can see we're debugging JVM code here.

Click continue and we'll hit our second breakpoint. As you can see we're in a different world now and debugging the native x86 code of our extension.

Click continue again, and as our code finishes we see the title of the activity change to "Hello from NDK".

Success!

This has been Setting up and Debugging Android apps with Mixed-Mode. Stay tuned for more videos. (Music)