Android NDK tips

Written by Xavier Gouchet - 23 april 2014 - no comments

The Android NDK can get really powerful, but one of the few drawbacks is that the Android.mk configuration file must list the source files. And if your project contains too many files, it can get quite difficult to maintain. Here's a little tip to list the files in a JNI source trees that is more readable, and more maintainable.

Here's your default Android.mk file


   LOCAL_PATH := $(call my-dir)

   include $(CLEAR_VARS)

   LOCAL_MODULE    := hello-jni
   LOCAL_SRC_FILES := hello-jni.c foo/spam.c  foo/eggs.c foo/bacon.c bar/banana.c 

   include $(BUILD_SHARED_LIBRARY)

Assuming you have folders foo and bar inside your jni folders, each with many source files, here's how you can list all your sources. First create a sources.mk file in each subfolder, with the following code in it :


LOCAL_SRC_FILES += foo/spam.c  foo/eggs.c foo/bacon.c

Then modify the Android.mk file that resides in your jni folder like this ;


   LOCAL_PATH := $(call my-dir)

   include $(CLEAR_VARS)

   LOCAL_MODULE    := hello-jni
   LOCAL_SRC_FILES := hello-jni.c

   include $(LOCAL_PATH)/foo/sources.mk

   include $(BUILD_SHARED_LIBRARY)

Write a comment