2015-06-09 68 views
1

目前我正在開發包含其中包括Java和本地C++代碼庫的應用程序。因爲庫項目是一個Eclipse項目,所以我將它導入到Eclipse中,使用「添加本機支持」功能創建了Android.mk和Application.mk,然後將項目重新導入到Android Studio中。如何更新的Android JNI代碼changings

我startet應用程序和它的作品不錯:)但現在,如果我想在做本地C++代碼的一些變化,我認識到,如果我重新啓動該項目,應用程序並沒有顯示這些改變。

我比我從谷歌的解決方案的許多範例項目(例如,「HELLO-JNI」),並有我的changings立即被識別和智能手機上的顯示。

唯一的區別我看到的是Android.mk文件,這是谷歌的例子很簡單,在我的項目庫的情況下,非常大的......只有作爲一個Java開發我覺得有點硬理解,該文件在做什麼以及如何修改它。它看起來像下面。

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) 

我的文件夾結構是這樣的:

- 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. 

任何人的想法如何解決這個問題呢?感謝提前:)

回答

0

沒問題。 $(cppfiles)是一個動態收集的C++源文件列表。您不需要手動更新此列表。只需撥打ndk-build即可重建本機部分。

+0

你好MSalters,你是對的$(cppfiles)包含了所有的我的源文件,我只是編輯從哪裏文件包含的路徑,但是當我打電話'NDK-build'我現在得到的錯誤我的一個包含語句無法找到:'Types.h:沒有這樣的文件或目錄......有關我如何解決這個問題的任何想法? – jennymo

+0

這就是'$(includes)'的用途。 – MSalters

+0

哦,是的,很明顯。我搜索了我的文件夾中的Types.h,並找到它,我用來保存到$(includes)變量中的路徑也是正確的。該文件位於文件夾「/ Users/jenny/appproject/libraryProjectWithNativeCode/src/main/jniLibs/platform」中。我打印了我的'$(includes)'變量,路徑被列在其中......但是錯誤仍然存​​在...... – jennymo

0

目前Android Studio中創建的即時新的Makefile和編譯所有源代碼從jni/。不幸的是,它無法處理預編譯的庫依賴。

從gradle這個更好的NDK支持應該很快被釋放,但在此期間,你可以關閉當前的一個,並調用NDK,建立自己代替:

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

NDK建造將放置。所以產生libs/armeabi,x86,...文件夾中的文件,但Android Studio期望它們在jniLibs/armeabi,x86,...之內。您可以手動移動它們一旦你叫NDK建造,或改變jniLibs目錄的位置,就像這樣:

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 
    } 
} 

如果你願意,你甚至可以得到NDK建造由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 
    } 
} 
+0

我已經在其他帖子中看到過這個解決方案,但不幸的是這並不適用於我。我結束了在Eclipse中導入這個項目並在Eclipse中構建.so文件......但感謝您的回答:) – jennymo