如何在项目android中加载两个预建库的.so文件?

时间:2018-01-23 13:18:48

标签: android cmake android-ndk ndk-build android.mk

我有两个项目.so文件。 project one包含使用CMake构建的预构建的openssl .so文件。第二个项目包含一些使用Android.mk构建的.so文件。现在我的问题是我想在我的新项目中使用这两个项目。但我没有得到如何编写CMake或Android.mk。

这里是第一个项目的CMake代码

# 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)

# 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.
             native-lib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             src/main/cpp/native-lib.cpp )


             add_library(openssl SHARED IMPORTED)
             set_target_properties (openssl PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/lib/libcrypto.so )

             add_library(prebuilt_ssl SHARED IMPORTED)
             set_target_properties (prebuilt_ssl PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/lib/libssl.so )






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

include_directories(openssl-armeabi-v7a/include/)
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
                      ${CMAKE_CURRENT_SOURCE_DIR}/openssl-armeabi-v7a/lib/libcrypto.a
                      ${CMAKE_CURRENT_SOURCE_DIR}/openssl-armeabi-v7a/lib/libssl.a

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

和第二个项目的Android.mk

#
# Copyright 2009 Cedric Priscal
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
# http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License. 
#

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

TARGET_PLATFORM := android-3
LOCAL_MODULE    := serial_port
LOCAL_SRC_FILES := SerialPort.c
LOCAL_LDLIBS    := -llog
include $(BUILD_SHARED_LIBRARY)

请帮我写CMake或Android.mk?哪个很容易CMake或Android.mk?

1 个答案:

答案 0 :(得分:0)

Google似乎鼓励我们最近使用 CMake 。对于简单构建,例如 libserial_port.so ,从 Android.mk CMakeLists.txt 的翻译并不难:

add_library(serial_port SHARED SerialPort.c)
target_link_libraries(serial_port log)

(请记住,日志不需要 find_library )。