Search code examples
android-studioandroid-ndkandroid-studio-3.0android.mk

Android Studio 3.0.1 Won't build NDK static library


I'm trying to build and include a static library in my Android Studio project. My project builds and runs fine without the static library.

My top-level Android.mk includes 2 explicit SQLite.mk and Hello.mk lines.

I have specified full paths in all of these .mk files just for my own sanity.

SQLite.mk is as follows:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

JNILIB_PATH := C:/androidprojects/SDLTest_SQLite/app/jniLibs 

SRC_SQLITE := C:/androidprojects/SDLTest_SQLite/app/jni/SQLite/sqlite3.c

LOCAL_CFLAGS += -Wno-write-strings

LOCAL_MODULE    := SQLite
LOCAL_SRC_FILES := $(SRC_SQLITE)

TARGET_OUT=$(JNILIB_PATH)/$(TARGET_ARCH_ABI)
#TARGET_OUT=../jniLibs/$(TARGET_ARCH_ABI)

include $(BUILD_STATIC_LIBRARY)

Android Studio/Gradle error is as follows:

Android NDK: ERROR:C:\androidprojects\SDLTest_SQLite\app\jni\Android.mk:SQLite: LOCAL_SRC_FILES points to a missing file    
Android NDK: Check that C:/androidprojects/SDLTest_SQLite/app/jniLibs/armeabi/libSQLite.a exists  or that its path is correct   
Build command failed.

My fundamental problem is that I cannot get a libSQLite.a file to be generated anywhere. Is my TARGET_OUT line the problem?

Can somebody please help me generate my static library file?

Here is a link to my project


Solution

  • You should not set TARGET_OUT in Android.mk: this is a sure way to break the subtle ndk-build logic. Your two mk files contradict one another: SQLite cannot be both prebuilt and built from sources.

    I don't know why your error message says that the SQLite source is missing, but you use full paths which is unhealthy.

    Here are the three mk files that do their job:

    Android.mk

    ROOT_PATH   := $(call my-dir)
    
    include $(ROOT_PATH)/src/Hello.mk
    include $(ROOT_PATH)/SQLite/SQLite.mk
    

    SQLite.mk

    LOCAL_PATH := $(call my-dir)
    
    include $(CLEAR_VARS)
    
    SRC_SQLITE := sqlite3.c
    
    LOCAL_CFLAGS += -Wno-write-strings
    
    LOCAL_MODULE    := SQLite
    LOCAL_SRC_FILES := $(SRC_SQLITE)
    
    include $(BUILD_STATIC_LIBRARY)
    

    Hello.mk

    LOCAL_PATH := $(call my-dir)
    
    include $(CLEAR_VARS)
    LOCAL_MODULE := libSDL2
    LOCAL_SRC_FILES := ../../jniLibs/SDL2-prebuilt/$(TARGET_ARCH_ABI)/libSDL2.so
    include $(PREBUILT_SHARED_LIBRARY)
    
    include $(CLEAR_VARS)
    
    LOCAL_MODULE := main
    
    SDL_PATH := $(LOCAL_PATH)/../../jniLibs/SDL2-prebuilt
    
    LOCAL_C_INCLUDES := $(SDL_PATH)/include
    
    # Add your application source files here...
    LOCAL_SRC_FILES := 52_hello_mobile.cpp
    LOCAL_STATIC_LIBRARIES := libSDL2 SQLite
    LOCAL_LDLIBS := -lGLESv1_CM -lGLESv2 -llog
    
    include $(BUILD_SHARED_LIBRARY)
    

    The latter can be slightly improved even more:

    LOCAL_PATH := $(call my-dir)
    
    include $(CLEAR_VARS)
    LOCAL_MODULE := libSDL2
    LOCAL_SRC_FILES := ../../jniLibs/SDL2-prebuilt/$(TARGET_ARCH_ABI)/libSDL2.so
    SDL_PATH := $(LOCAL_PATH)/../../jniLibs/SDL2-prebuilt
    LOCAL_EXPORT_C_INCLUDES := $(SDL_PATH)/include
    
    include $(PREBUILT_SHARED_LIBRARY)
    
    include $(CLEAR_VARS)
    
    LOCAL_MODULE := main
    
    # Add your application source files here...
    LOCAL_SRC_FILES := 52_hello_mobile.cpp
    LOCAL_STATIC_LIBRARIES := libSDL2 SQLite
    LOCAL_LDLIBS := -lGLESv1_CM -lGLESv2 -llog
    
    include $(BUILD_SHARED_LIBRARY)
    

    One more fox to your setup: you cannot build 64-bit versions with APP_PLATFORM=14. You should use compileSdkVersion at least 21, and don't set APP_PLATFORM explicitly. Then, gradle will automatically set APP_PLATFORM according to minSdkVersion for 32-bit targets (i.e. android-14), and push it to android-21 for 64-bit targets.

    In Aplication.mk, APP_PLATFORM setting is silently ignored.