一直想建立一個可複用c/c++代碼的跨平臺交叉編譯的工程,特別是能分別使用vs2017,android studio , xcode 三種ide來在各自平臺進行編碼。這樣就可以做到算法用c/c++多平臺複用,平臺特有api直接用nactive編碼。最後再結合每個平臺上的webview來實現界面web化,用h5來寫界面。
這樣一套就是,界面用H5,平臺IO訪問接口(除了文件IO,和socket io,因爲c/c++代碼完全可以進行文件io和socketio)用原生代碼,算法用c/c++編寫,做到寫一次交叉編譯到多個平臺,節約編碼時間和跨平臺難度。
最近在github.com就發現了一個開源工程,實現了,用vs2017,anroid studio,xcode 協調編輯同一套代碼,並分發不同平臺的功能,但是他的as工程用的ndk build模式,as 3.1已經不推薦使用了,因爲代碼比較多,遷移起來好像狠麻煩,於是想自己做。得益於as遷移到用CMake來組織JNI工程代碼,好像實現起來也狠順利。
首先新建一個根目錄 Shot4Read。三個平臺的project都放在這個目錄。
結構如下,其中Prj.Android 放的as 工程。Prj.Windows放的vs2017工程。ddmcore放的是可三端複用的c/c++代碼。
H:\DEVTOOLS\project\git\Shot4Read>dir
驅動器 H 中的卷沒有標籤。
卷的序列號是 CC73-754C
H:\DEVTOOLS\project\git\Shot4Read 的目錄
2019-07-02 16:37 <DIR> .
2019-07-02 16:37 <DIR> ..
2019-07-02 18:45 419 .gitignore
2019-07-02 18:36 <DIR> ddmcore
2019-06-28 16:54 <DIR> Prj.Android
2019-07-02 17:04 <DIR> Prj.Windows
1 個文件 419 字節
6 個目錄 11,622,670,336 可用字節
在Shot4Read目錄建好後,還是空的時候,先用vs2017建立一個MFC對話框工程放到Prj.Windows目錄,然後再建立一個dll工程名字就是ddmcore,放到shot4read目錄。這樣windows工程就建立好了。
然後在shot4read目錄建立一個as的jni工程,放到Prj.Android目錄中。要使as能勾複用dmmcore中的代碼,需要在cpp/CMakeLists.txt文件中改變如下:
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.4.1)
set(CMAKE_VERBOSE_MAKEFILE ON)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
#定義CASSDK位置 // 這裏就是導入複用的c/c++代碼目錄。
if(NOT DDMCORE_DIR)
set( DDMCORE_DIR ${CMAKE_SOURCE_DIR}/../../../../../ddmcore)
endif()
if( IS_DIRECTORY ${DDMCORE_DIR} )
# Add header file include directories
include_directories(${DDMCORE_DIR})#添加多目錄的路徑
# Add block directories
# 第二個cassdk.out參數用於指定外部文件夾在輸出文件夾中的位置
add_subdirectory( ${DDMCORE_DIR} cassdk.out)
else()
message(FATAL_ERROR "INVALID FOLDER 'CASSDK_DIR'=${DDMCORE_DIR}" )
endif()
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
native-lib.cpp)
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log)
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
native-lib
ddmcore-lib
# Links the target library to the log library
# included in the NDK.
${log-lib})
然後還需要在ddmcore目錄創建一個CMakeLists.txt,內容如下:
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.4.1)
set(CMAKE_VERBOSE_MAKEFILE ON)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
add_library( # Sets the name of the library.
ddmcore-lib ##引入ddmcore庫
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
apitest.cpp)
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log)
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
ddmcore-lib #連接ddmcore-lib。
# Links the target library to the log library
# included in the NDK.
${log-lib})
這兩個CMakeLists.txt文件編輯好後,刷新as之後,可以發現as直接把ddmcore目錄的代碼自動包含到工程了。如下圖:
至此就成功了。完整代碼可以在阿里雲git庫查看:(點此查看) 這個是windows vs2017 + android studio 分支。。。xcode分支因爲還沒搞定黑蘋果,暫時弄不了。弄好了再創建一個三端分支。。。