android9.0-ndk开发(1)-第三方库的生成

文章目录

    • 一、流程图
    • 二、ndk库的生成(第三方库)
      • 2.1 ndk下载:
      • 2.2 sample.cpp
      • 2.3 sample.h
      • 2.4 创建Android.mk
      • 2.5 创建 Application.mk
      • 2.6 编译

注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 调用实例

一、流程图

流程如下:
android9.0-ndk开发(1)-第三方库的生成_第1张图片

二、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 
#include 
#include 
#include 
#include 
#include 
#include 

#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
android9.0-ndk开发(1)-第三方库的生成_第2张图片

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


你可能感兴趣的:(Android开发)