当前位置:   article > 正文

android bp文件_Android.bp 文件中引入aar、jar、so库正确编译方法(值得收藏)

android12系統預置有so庫的apk android.bp

引入 aar

在模块源码根文件下新建文件夹 libs,复制要引入的 arr文件至此,新建 Android.bp

新增如下语句,这里以 lottie.arr 为例

android_library_import {

name: "lib-lottie",

aars: ["lottie-2.8.0.aar"],

sdk_version: "current",

}

然后在模块目录下 Android.bp 文件中的 android_app {} 中 static_libs 引入 “lib-lottie”,

android_app {

name: "LiveTv",

srcs: ["src/**/*.java"],

static_libs: [

"lib-lottie",

"android-support-annotations",

"android-support-compat",

"android-support-core-ui",

"androidx.tvprovider_tvprovider",

"android-support-v4",

....

这样编译就ok了,如果编译报错,

Error: Compilation can’t be completed because some library classes are missing.

Compilation failed

可以尝试将 “lib-lottie”, 移动到 libs: [] 中再次尝试

如果aar中带资源文件,需要将aar解压拷贝资源文件,不然编译时会提示找不到资源,运行时会报错

解压 lottie-2.8.0.aar,在模块源码根文件夹下新建 res-lottie 文件夹,将资源文件拷贝到此目录

模块目录下 Android.bp 文件中的 resource_dirs: [] 引入

android_app {

name: "LiveTv",

srcs: ["src/**/*.java"],

resource_dirs: [

"res",

"res_ext",

"res-lottie",

],

static_libs: [

"lib-lottie",

"android-support-annotations",

"android-support-compat",

"android-support-core-ui",

"androidx.tvprovider_tvprovider",

"android-support-v4",

....

],

aaptflags: [

"--extra-packages",

"com.airbnb.lottie",

],

同时增加 aar 对应的包名 aaptflags,以便生成对应包名 R 文件

aaptflags --extra-packages com.airbnb.lottie, 编译时会生成 com.airbnb.lottie.R

多个aar增按照如上操作配置多个即可

引入 jar

在模块源码根文件下新建文件夹 libs,复制要引入的 jar 包至此,新建 Android.bp

新增如下语句,这里以 opencv.jar 为例

java_import {

name: "face-opencv-jar",

jars: ["opencv.jar"],

sdk_version: "current",

}

然后在模块目录下 Android.bp 文件中的 android_app {} 中 libs 引入 “face-opencv-jar”,

android_app {

name: "LiveTv",

libs: [

"telephony-common",

"mediatek-framework",

"ims-common",

"face-opencv-jar",

],

这样编译就ok了, jar包相对简单一些

引入 so

在模块源码根文件下新建文件夹 armeabi,复制要引入的 so 至此,在libs中新建 Android.bp

新增如下语句,这里以 libjniopencv_face.so 为例, arm 和 arm64 分别对应32/64的so库,针对源码环境

位数都是确定的,所以我们就写成一样了

cc_prebuilt_library_shared {

name: "libjniopencv_face",

arch: {

arm: {

srcs: ["armeabi/libjniopencv_face.so"],

},

arm64: {

srcs: ["armeabi/libjniopencv_face.so"],

},

},

}

然后在模块目录下 Android.bp 文件中的 android_app {} 中 jni_libs 引入 “libjniopencv_face”,

android_app {

name: "LiveTv",

jni_libs: [

"libjniopencv_face",

]

]

一个完整的包含 aar/jar/so Android.bp

libs

----Android.bp

----lottie-2.8.0.aar

----face-opencv-jar

----armeabi

-----libjniopencv_face.so

-----libopencv_text.so

libs/Android.bp

android_library_import {

name: "lib-lottie",

aars: ["lottie-2.8.0.aar"],

sdk_version: "current",

}

java_import {

name: "face-opencv-jar",

jars: ["opencv.jar"],

sdk_version: "current",

}

cc_prebuilt_library_shared {

name: "libjniopencv_face",

arch: {

arm: {

srcs: ["armeabi/libjniopencv_face.so"],

},

arm64: {

srcs: ["armeabi/libjniopencv_face.so"],

},

},

}

cc_prebuilt_library_shared {

name: "libopencv_text",

arch: {

arm: {

srcs: ["armeabi/libopencv_text.so"],

},

arm64: {

srcs: ["armeabi/libopencv_text.so"],

},

},

}

模块根路径Android.bp

android_app {

name: "LiveTv",

srcs: ["src/**/*.java"],

// TODO(b/122608868) turn proguard back on

optimize: {

enabled: false,

},

// It is required for com.android.providers.tv.permission.ALL_EPG_DATA

privileged: true,

sdk_version: "system_current",

min_sdk_version: "23", // M

resource_dirs: [

"res",

"material_res",

"res-lottie",

],

libs: [

"face-opencv-jar",

],

static_libs: [

"android-support-compat",

"android-support-core-ui",

"androidx.tvprovider_tvprovider",

"android-support-v4",

"android-support-v7-appcompat",

"android-support-v7-palette",

"android-support-v7-preference",

"android-support-v7-recyclerview",

"android-support-v14-preference",

"android-support-v17-leanback",

"android-support-v17-preference-leanback",

"lib-lottie",

],

jni_libs: [

"libjniopencv_face",

"libopencv_text",

]

javacflags: [

"-Xlint:deprecation",

"-Xlint:unchecked",

],

aaptflags: [

"--version-name",

version_name,

"--version-code",

version_code,

"--extra-packages",

"com.android.tv.tuner",

"--extra-packages",

"com.airbnb.lottie",

],

}

小技巧

一般需要引入的so库都会是几十个,每一个都需要在Android.bp配置对应的 cc_prebuilt_library_shared

挨个复制会很浪费时间,自信观察格式都是固定的,我们可以通过遍历文件夹来生成这个json串

将所有 so 库文件拷贝至 sdcard/Android/armeabi/,遍历读取文件名,按默认格式写入txt文件即可

private void getSoJson() {

String fileAbsolutePath = Environment.getExternalStorageDirectory().getPath() + "/Android/armeabi/";

Log.e("eee", "fileAbsolutePath : " + fileAbsolutePath);

File file = new File(fileAbsolutePath);

File[] subFile = file.listFiles();

String json = "";

String soName = "";

for (int iFileLength = 0; iFileLength < subFile.length; iFileLength++) {

if (!subFile[iFileLength].isDirectory()) {

String filename = subFile[iFileLength].getName();

String name = filename.split("\\.")[0];

Log.e("eee", "filename : " + filename + " name=" + name);

json += "cc_prebuilt_library_shared {\n" +

" name: \"" + name + "\",\n" +

" arch: {\n" +

" arm: {\n" +

" srcs: [\"armeabi/" + filename + "\"],\n" +

" },\n" +

" arm64: {\n" +

" srcs: [\"armeabi/" + filename + "\"],\n" +

" },\n" +

" },\n" +

"}" + "\r\n";

soName += "\""+name+"\"," + "\r\n";

}

}

Log.e("eee", "soJson =" + json);

write2File("so.txt", json);

write2File("libs.txt", soName);

}

private void write2File(String fileName, String data) {

String strFilePath = Environment.getExternalStorageDirectory().getPath() + "/Android/" + fileName;

String strContent = data + "\r\n";

try {

File sfile = new File(strFilePath);

if (!sfile.exists()) {

Log.d("TestFile", "Create the file:" + strFilePath);

sfile.getParentFile().mkdirs();

sfile.createNewFile();

}

RandomAccessFile raf = new RandomAccessFile(sfile, "rwd");

raf.seek(sfile.length());

raf.write(strContent.getBytes());

raf.close();

} catch (Exception e) {

Log.e("TestFile", "Error on write File:" + e);

}

}

本文地址:https://blog.csdn.net/u012932409/article/details/108119443

希望与广大网友互动??

点此进行留言吧!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/331690?site
推荐阅读
相关标签
  

闽ICP备14008679号