【问题标题】:How to load .so files of two pre built libraries in project android?如何在项目android中加载两个预建库的.so文件?
【发布时间】:2018-01-23 13:18:47
【问题描述】:

我有两个项目 .so 文件。项目一包含使用 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 ?

【问题讨论】:

  • 你想链接libnative_liblibserial_port
  • @Michael 是的,我想同时使用两者

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


【解决方案1】:

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

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

(请记住,log 不需要 find_library)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-20
    • 1970-01-01
    • 1970-01-01
    • 2015-07-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多