android9.0-ndk開發(1)-第三方庫的生成


注1:android9.0-ndk開發系列文檔提供一個ndk編譯JNI庫,並訪問第三方庫,APP 直接通過jar接口調用的方法。
注2:本文爲個人學習記錄,可能存在個別或多處錯誤,歡迎指正和討論。
android9.0-ndk開發(1)-第三方庫的生成
android9.0-ndk開發(2)-JNI代碼
android9.0-ndk開發(3)-Jar打包
android9.0-ndk開發(4)-APP 調用實例

一、流程圖

流程如下:
流程圖

二、ndk庫的生成(第三方庫)

2.1 ndk下載:

https://developer.android.google.cn/ndk/downloads/
我這裏選擇的是Linux 64 位 (x86) android-ndk-r20-linux-x86_64.zip版本
使用以下命令後,即可使用ndk-build編譯工具

export PATH=/home/ttx/work_runtime/alex/android-ndk-r20:$PATH

查看ndk-build

$ ndk-build -v
GNU Make 3.81
Copyright (C) 2006  Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.

This program built for x86_64-pc-linux-gnu

2.2 sample.cpp

/*
 * Copyright (C) 2019 Alex
 *
 * 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.
 */
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>

#include "sample.h"
static char str_save[255] = {0};

int sample_SetString(char *pstr_set)
{
    printf("[third-party lib] SetString \n");
    if(NULL == pstr_set)
        return -1;
    memset(str_save,0,sizeof(str_save));
    strncpy(str_save,pstr_set,255);
    return 0;
} 

int sample_GetString(char *pstr_get)
{
    printf("[third-party lib] GetString \n");
    if(NULL == pstr_get)
        return -1;
    
     strncpy(pstr_get,str_save,255);
     
    return 0;
} 

2.3 sample.h

#ifndef __SAMPLE_H__
#define __SAMPLE_H__

int sample_SetString(char *pstr_set);

int sample_GetString(char *pstr_get);

#endif

2.4 創建Android.mk

LOCAL_PATH := $(call my-dir)

local_c_includes := \
    $(LOCAL_PATH) \
    $(LOCAL_PATH)/include 

local_src_files:= \
    sample.cpp 

include $(CLEAR_VARS) 
LOCAL_MODULE :=sample_thirdparty
LOCAL_SRC_FILES += $(local_src_files)	
LOCAL_C_INCLUDES += $(local_c_includes)
LOCAL_MODULE_TAGS := optional

include $(BUILD_SHARED_LIBRARY)

2.5 創建 Application.mk

LOCAL_PATH := $(call my-dir)

APP_STL :=c++_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI :=all
APP_PLATFORM :=android-28

APP_BUILD_SCRIPT := $(LOCAL_PATH)/Android.mk

ABI 是 Application Binary Interface 的縮寫。
不同的 Android 手機使用不同的 CPU,而不同的 CPU 支持不同的指令集。
具體的可以查看:https://developer.android.com/ndk/guides/abis.html?hl=zh-cn
我現在使用的是:all 編譯所有平臺的指令集。
支持的平指令集有:arm64-v8a armeabi-v7a x86 x86_64
ABI

2.6 編譯

目錄結構:

./
├── Android.mk
├── Application.mk
├── include
│   └── sample.h
└── sample.cpp

使用編譯命令:

ndk-build NDK_APPLICATION_MK=./Application.mk  NDK_PROJECT_PATH=./

log如下:


>ndk-build NDK_APPLICATION_MK=./Application.mk  NDK_PROJECT_PATH=./
[arm64-v8a] Compile++      : sample_thirdparty <= sample.cpp
[arm64-v8a] SharedLibrary  : libsample_thirdparty.so
[arm64-v8a] Install        : libsample_thirdparty.so => libs/arm64-v8a/libsample_thirdparty.so
[x86_64] Compile++      : sample_thirdparty <= sample.cpp
[x86_64] SharedLibrary  : libsample_thirdparty.so
[x86_64] Install        : libsample_thirdparty.so => libs/x86_64/libsample_thirdparty.so
[armeabi-v7a] Compile++ thumb: sample_thirdparty <= sample.cpp
[armeabi-v7a] SharedLibrary  : libsample_thirdparty.so
[armeabi-v7a] Install        : libsample_thirdparty.so => libs/armeabi-v7a/libsample_thirdparty.so
[x86] Compile++      : sample_thirdparty <= sample.cpp
[x86] SharedLibrary  : libsample_thirdparty.so
[x86] Install        : libsample_thirdparty.so => libs/x86/libsample_thirdparty.so

查看庫生成:

$ tree libs/
libs/
├── arm64-v8a
│   └── libsample_thirdparty.so
├── armeabi-v7a
│   └── libsample_thirdparty.so
├── x86
│   └── libsample_thirdparty.so
└── x86_64
    └── libsample_thirdparty.so


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章