[Android] Force clean if API level or toolchain changed

Android NDK is failing to automatically detect if input API level
or build toolchain has been changed. As such it's not forcing an
objs clean. Deal with such case by using build attribute cache files.

Signed-off-by: Anestis Bechtsoudis <anestis@census-labs.com>
diff --git a/android/Android.mk b/android/Android.mk
index 2168dde..492d295 100644
--- a/android/Android.mk
+++ b/android/Android.mk
@@ -15,6 +15,24 @@
 
 LOCAL_PATH := $(abspath $(call my-dir)/..)
 
+# Force a clean if target API has changed and a previous build exists
+ifneq ("$(wildcard $(LOCAL_PATH)/libs/$(TARGET_ARCH_ABI)/android_api.txt)","")
+  CACHED_API := $(shell cat "$(LOCAL_PATH)/libs/$(TARGET_ARCH_ABI)/android_api.txt")
+  ifneq ($(ANDROID_API),$(CACHED_API))
+    $(info [!] Previous build was targeting different API level - cleaning)
+    DUMMY_CLEAN := $(shell make clean)
+  endif
+endif
+
+# Force a clean if selected toolchain has changed and a previous build exists
+ifneq ("$(wildcard $(LOCAL_PATH)/libs/$(TARGET_ARCH_ABI)/ndk_toolchain.txt)","")
+  CACHED_TOOLCHAIN := $(shell cat "$(LOCAL_PATH)/libs/$(TARGET_ARCH_ABI)/ndk_toolchain.txt")
+  ifneq ($(NDK_TOOLCHAIN),$(CACHED_TOOLCHAIN))
+    $(info [!] Previous build was using different toolchain - cleaning)
+    DUMMY_CLEAN := $(shell make clean)
+  endif
+endif
+
 # Enable Linux ptrace() instead of POSIX signal interface by default
 ANDROID_WITH_PTRACE ?= true
 
@@ -161,9 +179,13 @@
 include $(BUILD_EXECUTABLE)
 
 # The NDK build system does not copy static libraries into project/packages
-# so it has to be done manually in order to have all output under a single path
+# so it has to be done manually in order to have all output under a single path.
+# Also save some build attribute cache files so that cleans can be enforced when
+# required.
 all:POST_BUILD_EVENT
 POST_BUILD_EVENT:
+	@echo $(ANDROID_API) > $(LOCAL_PATH)/libs/$(TARGET_ARCH_ABI)/android_api.txt
+	@echo $(NDK_TOOLCHAIN) > $(LOCAL_PATH)/libs/$(TARGET_ARCH_ABI)/ndk_toolchain.txt
 	@test -f $(LOCAL_PATH)/obj/local/$(TARGET_ARCH_ABI)/libhfuzz.a && \
 	  cp $(LOCAL_PATH)/obj/local/$(TARGET_ARCH_ABI)/libhfuzz.a \
 	    $(LOCAL_PATH)/libs/$(TARGET_ARCH_ABI)/libhfuzz.a || true