1. 安装依赖库
# sudo apt-get install make automake autoconf libtool pkg-config gawk libwayland-dev
libegl1-mesa-dev wayland-protocols libwayland-bin
2.下载 android-headers [仓库地址](https://github.com/ubports/android-headers)
# git clone https://github.com/ubports/android-headers.git
在usr/include 下创建一个 android-headers文件夹
# mkdir android-headers
拷贝headers的一个版本 到 /usr/include/android-headers
比如拷贝 headers 23 :
# sudo cp -rf android-headers/23/* /usr/include/android-headers
3. 下载libhybris库 [仓库地址](https://github.com/libhybris/libhybris)
# git clone https://github.com/libhybris/libhybris.git
切换到hybris下
# cd libhybris/hybris
执行
# ./autogen.sh --with-android-headers=/usr/include/android-headers --prefix=/opt/libhybris --enable-wayland
./autogen.sh --with-android-headers=/usr/include/android-headers --prefix=/opt/libhybris --enable-arch=arm64 --enable-wayland
然后
# make
再然后
# sudo make install
完成之后,会在/opt/libhybris 下生成 bin、 include 、 lib
然后把编译后生成的bin、 include 、 lib
lib拷贝置/usr/lib文件目录下,include文件拷贝到/usr/include目录下 ,bin拷贝到/usr/bin
----------------------配置libhybris完----------------------
----------------------测试----------------------
二、配置NDK 的环境 可参考 :https://blog.csdn.net/qiantanlong/article/details/80901193
ndk下载地址: https://developer.android.google.cn/ndk/downloads/
1、 运行命令行执行sudo vim /etc/profile
2、在文档的结尾增加以下三行:
#setNDK env
NDKROOT=/home/zhen/Downloads/ndk/android-ndk-r17b
export PATH=$NDKROOT:$PATH
3、重启电脑,在命令行窗口验证NDK 环境配置是否正确,输入ndk-build
三、编译、生产SO库文件
1、在合适的目录下新建jni目录,在jni目录下新建Android.mk和Application.mk两个文件,
foo.cpp //通过ndk把foo.cpp编译成android的.so文件。
Android.mk 里面的代码:
-------------------------------------------
LOCAL_PATH:=$(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE:= libfoo
LOCAL_SRC_FILES:= foo.cpp
include $(BUILD_SHARED_LIBRARY)
-------------------------------------------
Application.mk 里面的代码:
-------------------------------------------
APP_ABI := armeabi-v7a x86 arm64-v8a x86_64
APP_PLATFORM := android-23
-------------------------------------------
foo.cpp 里面的代码:
-------------------------------------------
#include <stdio.h>
#include <stdlib.h>
void foo(void){
cout << "Hello, world!" << endl;
printf("foo\n");
printf("%s\n", getenv("PATH"));
}
void bar(void){
foo();
printf("bar\n");
}
-------------------------------------------
2、在jni下,执行ndk-build命令,就可以进行编译、生产so文件了
3、完成后,会生成libs和obj两个目录,libs下放置生成的so文件。
四、在根目录创建 system/lib64 文件夹
# cd /system
# sudo mkdir lib64
把libs/x86_64 下的文件复制到 system/lib64里面
# sudo cp ~/libs/x86_64/libfoo.so lib64/libfoo.so
然后 创建测试文件
# cd
# vim ly.c
ly.c文件里面内容: 通过libhybris调用libfoo.so里面的bar();
----------------------------------------------
#define _GNU_SOURCE
#include <dlfcn.h>
#include <stdio.h>
#include <hybris/common/dlfcn.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
int main(void){
printf("xxx----------------------------->%s(), line = %d\n",__FUNCTION__,__LINE__);
void *handle;
void (*bar)(void);
printf("xxx----------------------------->%s(), line = %d\n",__FUNCTION__,__LINE__);
handle = hybris_dlopen("libfoo.so", RTLD_NOW);
printf("xxx----------------------------->%s(), line = %d\n",__FUNCTION__,__LINE__);
if (NULL == handle){
fprintf(stderr, "android_dlopen failed: %s\n", strerror(errno));
return -1;
}
bar = (void (*)(void))hybris_dlsym(handle, "_Z3barv");
if (NULL == bar){
fprintf(stderr, "fail to dlsym: %s\n", strerror(errno));
return -1;
}
bar();
printf("xxx----------------------------->%s(), line = %d\n",__FUNCTION__,__LINE__);
return 0;
}
----------------------------------------------
生成可执行的文件
# gcc ly.c -lhybris-common -landroid-properties -o ly
# ./ly
报错 library "libc.so" not found 找不到库 可以在ndk下找到 比如:
/home/ws/ndk/android-ndk-r21d/platforms/android-23/arch-x86_64/usr/lib64/libc.so
可以用命令查找系统中叫 libc.so的库
# sudo find / -name libc.so 或者用 locate libc.so 都能查到。
然后把他放在 system/lib64/ 文件夹下就可以了
# gcc ly.c -lhybris-common -landroid-properties -o ly
# ./ly
最后打印结果 :
foo
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin
bar
在这里插入代码片
libhybris 编译和配置
于 2021-03-22 11:41:15 首次发布