How to update Android jni code changings

1.5k Views Asked by At

currently I am developing an application which contains a library which consists of java and native c++ code. Because the library project was an Eclipse project, I imported it in Eclipse, created the Android.mk and Application.mk with the "Add native support"-Function and then reimport the project into Android Studio.

I startet the app and it works nice :) But now if I would like to do some changes in the native c++ code, I recognized that if I restart the project, the app did not show these changes.

I compared my solution to many of the example projects from google (for example "hello-jni") and there my changings are immediately recognized and displayed on the smartphone.

The only difference I see is the Android.mk file, which is very simple for the google examples and very big in the case of my project library...and only as a java developer I find it a little bit hard to understand, what this file is doing and how to modify it. It looks like the following.

    LOCAL_PATH := $(call my-dir)

$(info TARGET_ARCH_ABI is $(TARGET_ARCH_ABI))

$(info LOCAL_PATH is $(LOCAL_PATH))

PREBUILT_LIBS := $(LOCAL_PATH)/../libs/libraryWithNativeCode/prebuilt/android-$(TARGET_ARCH_ABI)

include $(CLEAR_VARS)
LOCAL_MODULE := libraryWithNativeCode-sdk-lib
LOCAL_SRC_FILES := $(PREBUILT_LIBS)/libraryWithNativeCode-sdk.a
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := png-lib
LOCAL_SRC_FILES := $(PREBUILT_LIBS)/libpng.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/../libs/libraryWithNativeCode/png
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := curl-lib
LOCAL_SRC_FILES := $(PREBUILT_LIBS)/libcurl.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/../libs/libraryWithNativeCode/curl/android-$(TARGET_ARCH_ABI)
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := ssl-lib
LOCAL_SRC_FILES := $(PREBUILT_LIBS)/libssl.a
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := crypto-lib
LOCAL_SRC_FILES := $(PREBUILT_LIBS)/libcrypto.a
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := http-parser-lib
LOCAL_SRC_FILES := $(PREBUILT_LIBS)/libhttp-parser.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/../libs/libraryWithNativeCode/http-parser
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := jpeg-lib
LOCAL_SRC_FILES := $(PREBUILT_LIBS)/libjpeg.a
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := turbojpeg-lib
LOCAL_SRC_FILES := $(PREBUILT_LIBS)/libturbojpeg.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/../libs/libraryWithNativeCode/jpeg-turbo
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)

LOCAL_MODULE := libraryWithNativeCode-mobile-example-app
LOCAL_LDLIBS := -llog -landroid -lEGL -lGLESv2 -lz -lm
LOCAL_LDLIBS += -fuse-ld=bfd
LOCAL_STATIC_LIBRARIES := libraryWithNativeCode-sdk-lib png-lib curl-lib ssl-lib crypto-lib http-parser-lib jpeg-lib turbojpeg-lib android_native_app_glue ndk_helper

LOCAL_CFLAGS += -Wall -Wno-unknown-pragmas -Wno-sign-compare -Wno-format-security -Wno-reorder
#LOCAL_CFLAGS += -Werror

ifdef COMPILE_CPP_11
  $(info Configured for C++11)
  LOCAL_CPPFLAGS += -DCOMPILE_CPP_11=1 -std=c++11
else
  $(info Configured for C++0x)
endif

os_name:=$(shell uname -s)

get_android_cpp_files_cmd := find $(LOCAL_PATH) -type f  -iname "*.cpp"
get_android_includes_cmd  := find $(LOCAL_PATH) -type d
get_shared_cpp_files_cmd  := find $(LOCAL_PATH)/src -type f  -iname "*.cpp"
get_shared_includes_cmd   := find $(LOCAL_PATH)/src -type d
get_platform_includes_cmd := find $(LOCAL_PATH)/../libs/libraryWithNativeCode/platform -type d ! -path "*/OSX/*" ! -path "*/iOS/*"


ifeq ($(os_name),Darwin)
    cppfiles := ${shell ${get_android_cpp_files_cmd}}
    cppfiles += ${shell ${get_shared_cpp_files_cmd}}

    includes := ${shell ${get_android_includes_cmd}}
    includes += ${shell ${get_shared_includes_cmd}}
    includes += ${shell ${get_platform_includes_cmd}}
else
    # assume windows if not specified for now (due to no uname)
    cppfiles := ${shell sh -c '${get_android_cpp_files_cmd}'}
    cppfiles += ${shell sh -c '${get_shared_cpp_files_cmd}'}

    includes := ${shell sh -c '${get_android_includes_cmd}'}
    includes += ${shell sh -c '${get_shared_includes_cmd}'}
    includes += ${shell sh -c '${get_platform_includes_cmd}'}
endif


LOCAL_SRC_FILES := $(cppfiles:$(LOCAL_PATH)/%=%)
LOCAL_C_INCLUDES := $(includes)

LOCAL_C_INCLUDES += $(LOCAL_PATH)/../libs/libraryWithNativeCode/rapidjson
LOCAL_C_INCLUDES += $(LOCAL_PATH)/../libs/libraryWithNativeCode/rapidjson/internal

include $(BUILD_SHARED_LIBRARY)

$(call import-module,android/native_app_glue)
$(call import-module,android/ndk_helper)

My folder structure is like:

- Android_Studio_Project
--.gradle
--.idea
--app
---src
----main (etc)
--libraryProjectWithNativeCode
---src
----main
-----java (contains the java code)
-----jni (contains the jni code)
-----jniLibs
-----etc.
--build.gradle etc.

Anybody an idea how to solve this issue? Thanks in advance :)

2

There are 2 best solutions below

3
On

No problem. $(cppfiles) is a dynamically gathered list of C++ source files. You don't need to update this list manually. Just call ndk-build to rebuild the native part.

1
On

currently Android Studio creates a new Makefile on-the-fly and compiles all the sources from jni/. Unfortunately it cannot handle precompiled libs depencies.

A better NDK support from gradle should be released soon, but in the meantime you can deactivate the current one, and call ndk-build yourself instead:

android {
    sourceSets.main {
        jni.srcDirs = [] //disable the built-in ndk-build call with auto-generated Android.mk
    }
}

ndk-build will place the generated .so files inside the libs/armeabi,x86,... folders, but Android Studio expects them inside jniLibs/armeabi,x86,.... You can move them manually once you've called ndk-build, or change the location of the jniLibs directory, like so:

android {
    sourceSets.main {
        jniLibs.srcDir 'src/main/libs' //set libs as .so's location instead of jniLibs
        jni.srcDirs = [] //disable the built-in ndk-build call with auto-generated Android.mk
    }
}

If you want, you can even get ndk-build to be called by gradle:

import org.apache.tools.ant.taskdefs.condition.Os
...     
android {
    ...

    sourceSets.main {
        jniLibs.srcDir 'src/main/libs' //set libs as .so's location instead of jniLibs
        jni.srcDirs = [] //disable automatic ndk-build call with auto-generated Android.mk
    }

    // call regular ndk-build(.cmd) script from app directory
    task ndkBuild(type: Exec) {
        if (Os.isFamily(Os.FAMILY_WINDOWS)) {
            commandLine 'ndk-build.cmd', '-C', file('src/main').absolutePath
        } else {
            commandLine 'ndk-build', '-C', file('src/main').absolutePath
        }
    }

    tasks.withType(JavaCompile) {
        compileTask -> compileTask.dependsOn ndkBuild
    }
}