Skip to content

DanAlbert/ndk-app-template

Repository files navigation

NDK App Template

This is a template application that configures a bit more than the new project wizard in Android Studio. The differences are:

  • Better default gitignore
  • A gitattributes file
  • Includes testing support
  • Removed empty proguard config
  • Updated out of date dependencies
  • A less cluttered CMakeLists.txt
  • Added a version script to protect the app's symbol visibility
  • Uses RegisterNatives via JNI_OnLoad instead of name-based lookups
  • Added C++ unit tests
  • Enable clang-tidy

You should change the following after forking this template:

  1. Change the project name using the rootProject.name property in settings.gradle.
  2. Update the groupId and applicationId. git grep com.example and git grep -i myapplication to find places to update. You'll also need to rename and com/example/myapplication directories to match your new IDs, and update the argument to env->FindClass in JNI_OnLoad.
  3. Update the CMake project name in CMakeLists.txt.
  4. (Optional) Relicense the project if you don't want your code covered by Zero Clause BSD.

How to use this template in Android Studio

Open Android Studio and open this directory when selecting a project.

How to use this template in Visual Studio Code

Warning: I struggled a lot to get this to work, and wasn't happy with the result. The C++ features are annoying to keep working, and I didn't find any answers about how to work with Kotlin or even Java in Android projects. Please send a PR if you figure out better instructions.

Open VSCode and open this directory to create the workspace.

Install the C++ extension.

The C++ extension needs the compile_commands.json file that is generated by CMake during the build, so build the project once to prepare that for the editor: ./gradlew assemble.

Next you need to configure VSCode to use the generated compile_commands.json file. If you open a C++ file in the app project, VSCode may pop up a dialog in the bottom right of the window that will do that for you. If it doesn't, you can configure it manually:

  1. Run find app/.cxx -name compile_commands.json to find the fils that were generated.
  2. Open the C/C++: Edit Configurations (UI) window (ctrl+shift+P or cmd+shift+P on macOS, then search for that)
  3. (Optional) Add a new configuration for this build type, such as Android Debug arm64-v8a (the name doesn't have any special meaning, it's just for your benefit).
  4. Expand the "Advanced" settings
  5. Set the "Compile commands" field to the path of the compile_commands.json file.

In either case, you'll have multiple compile_commands.json files to choose from; one per CMake build type and ABI combo. For example, you'll have RelWithDebInfo/<some hash>/arm64-v8a and Debug/<some hash>/x86. Pick whichever is appropriate for the config you're testing.

Warning: The output location of compile_commands.json is not stable because AGP uses a hash of the CMake commands as part of the output directory as a way to share objects between identical build variants. This means that you may need to repeat these steps if you change your build.gradle file. If the find command above is showing more than one hash for each CMake build type, you'll need to do a clean build and then rebuild to determine which path is the correct one. See https://issuetracker.google.com/232986281 for more information.

About

A revised template for NDK apps.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published