目前我正在开发一个包含一个由java和本机c ++代码组成的库的应用程序。因为库项目是Eclipse项目,所以我在Eclipse中导入它,使用“添加本机支持”-Function创建Android.mk和Application.mk,然后将项目重新导入到Android Studio中。
我启动应用程序并且它运行良好:)但是现在如果我想在本机c ++代码中进行一些更改,我认识到如果我重新启动项目,应用程序没有显示这些更改。
我将我的解决方案与谷歌的许多示例项目进行了比较(例如“hello-jni”),我的变化立即被识别并显示在智能手机上。
我看到的唯一区别是Android.mk文件,这对于google示例非常简单,对于我的项目库来说非常大......而且只有作为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 :(得分:0)
没问题。 $(cppfiles)
是动态收集的C ++源文件列表。您无需手动更新此列表。只需致电ndk-build
即可重建原生部分。
答案 1 :(得分:0)
目前,Android Studio即时创建一个新的Makefile,并编译来自jni/
的所有源代码。不幸的是,它无法处理预编译的libs依赖项。
应该尽快发布gradle的更好的NDK支持,但在此期间你可以停用当前的支持,并自己调用ndk-build:
android {
sourceSets.main {
jni.srcDirs = [] //disable the built-in ndk-build call with auto-generated Android.mk
}
}
ndk-build会将生成的.so文件放在libs/armeabi,x86,...
文件夹中,但Android Studio会将它们放在jniLibs/armeabi,x86,...
内。您可以在调用ndk-build后手动移动它们,或更改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
}
}
如果你愿意,你甚至可以通过gradle调用ndk-build:
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
}
}