当前位置:   article > 正文

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

android_library_import

 

 

引入 aar

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

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

  1. android_library_import {
  2. name: "lib-lottie",
  3. aars: ["lottie-2.8.0.aar"],
  4. sdk_version: "current",
  5. }

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

  1. android_app {
  2. name: "LiveTv",
  3. srcs: ["src/**/*.java"],
  4. static_libs: [
  5. "lib-lottie",
  6. "android-support-annotations",
  7. "android-support-compat",
  8. "android-support-core-ui",
  9. "androidx.tvprovider_tvprovider",
  10. "android-support-v4",
  11. ....

这样编译就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: [] 引入

  1. android_app {
  2. name: "LiveTv",
  3. srcs: ["src/**/*.java"],
  4. resource_dirs: [
  5. "res",
  6. "res_ext",
  7. "res-lottie",
  8. ],
  9. static_libs: [
  10. "lib-lottie",
  11. "android-support-annotations",
  12. "android-support-compat",
  13. "android-support-core-ui",
  14. "androidx.tvprovider_tvprovider",
  15. "android-support-v4",
  16. ....
  17. ],
  18. aaptflags: [
  19. "--extra-packages",
  20. "com.airbnb.lottie",
  21. ],

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

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

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

引入 jar

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

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

  1. java_import {
  2. name: "face-opencv-jar",
  3. jars: ["opencv.jar"],
  4. sdk_version: "current",
  5. }

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

  1. android_app {
  2. name: "LiveTv",
  3. libs: [
  4. "telephony-common",
  5. "mediatek-framework",
  6. "ims-common",
  7. "face-opencv-jar",
  8. ],

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

引入 so

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

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

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

  1. cc_prebuilt_library_shared {
  2. name: "libjniopencv_face",
  3. arch: {
  4. arm: {
  5. srcs: ["armeabi/libjniopencv_face.so"],
  6. },
  7. arm64: {
  8. srcs: ["armeabi/libjniopencv_face.so"],
  9. },
  10. },
  11. }

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

  1. android_app {
  2. name: "LiveTv",
  3. jni_libs: [
  4. "libjniopencv_face",
  5. ]
  6. ]

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

libs

  1. ----Android.bp
  2. ----lottie-2.8.0.aar
  3. ----face-opencv-jar
  4. ----armeabi
  5. -----libjniopencv_face.so
  6. -----libopencv_text.so

libs/Android.bp

  1. android_library_import {
  2. name: "lib-lottie",
  3. aars: ["lottie-2.8.0.aar"],
  4. sdk_version: "current",
  5. }
  6. java_import {
  7. name: "face-opencv-jar",
  8. jars: ["opencv.jar"],
  9. sdk_version: "current",
  10. }
  11. cc_prebuilt_library_shared {
  12. name: "libjniopencv_face",
  13. arch: {
  14. arm: {
  15. srcs: ["armeabi/libjniopencv_face.so"],
  16. },
  17. arm64: {
  18. srcs: ["armeabi/libjniopencv_face.so"],
  19. },
  20. },
  21. }
  22. cc_prebuilt_library_shared {
  23. name: "libopencv_text",
  24. arch: {
  25. arm: {
  26. srcs: ["armeabi/libopencv_text.so"],
  27. },
  28. arm64: {
  29. srcs: ["armeabi/libopencv_text.so"],
  30. },
  31. },
  32. }

模块根路径Android.bp

  1. android_app {
  2. name: "LiveTv",
  3. srcs: ["src/**/*.java"],
  4. // TODO(b/122608868) turn proguard back on
  5. optimize: {
  6. enabled: false,
  7. },
  8. // It is required for com.android.providers.tv.permission.ALL_EPG_DATA
  9. privileged: true,
  10. sdk_version: "system_current",
  11. min_sdk_version: "23", // M
  12. resource_dirs: [
  13. "res",
  14. "material_res",
  15. "res-lottie",
  16. ],
  17. libs: [
  18. "face-opencv-jar",
  19. ],
  20. static_libs: [
  21. "android-support-compat",
  22. "android-support-core-ui",
  23. "androidx.tvprovider_tvprovider",
  24. "android-support-v4",
  25. "android-support-v7-appcompat",
  26. "android-support-v7-palette",
  27. "android-support-v7-preference",
  28. "android-support-v7-recyclerview",
  29. "android-support-v14-preference",
  30. "android-support-v17-leanback",
  31. "android-support-v17-preference-leanback",
  32. "lib-lottie",
  33. ],
  34. jni_libs: [
  35. "libjniopencv_face",
  36. "libopencv_text",
  37. ]
  38. javacflags: [
  39. "-Xlint:deprecation",
  40. "-Xlint:unchecked",
  41. ],
  42. aaptflags: [
  43. "--version-name",
  44. version_name,
  45. "--version-code",
  46. version_code,
  47. "--extra-packages",
  48. "com.android.tv.tuner",
  49. "--extra-packages",
  50. "com.airbnb.lottie",
  51. ],
  52. }

小技巧

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

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

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

  1. private void getSoJson() {
  2. String fileAbsolutePath = Environment.getExternalStorageDirectory().getPath() + "/Android/armeabi/";
  3. Log.e("eee", "fileAbsolutePath : " + fileAbsolutePath);
  4. File file = new File(fileAbsolutePath);
  5. File[] subFile = file.listFiles();
  6. String json = "";
  7. String soName = "";
  8. for (int iFileLength = 0; iFileLength < subFile.length; iFileLength++) {
  9. if (!subFile[iFileLength].isDirectory()) {
  10. String filename = subFile[iFileLength].getName();
  11. String name = filename.split("\\.")[0];
  12. Log.e("eee", "filename : " + filename + " name=" + name);
  13. json += "cc_prebuilt_library_shared {\n" +
  14. " name: \"" + name + "\",\n" +
  15. " arch: {\n" +
  16. " arm: {\n" +
  17. " srcs: [\"armeabi/" + filename + "\"],\n" +
  18. " },\n" +
  19. " arm64: {\n" +
  20. " srcs: [\"armeabi/" + filename + "\"],\n" +
  21. " },\n" +
  22. " },\n" +
  23. "}" + "\r\n";
  24. soName += "\""+name+"\"," + "\r\n";
  25. }
  26. }
  27. Log.e("eee", "soJson =" + json);
  28. write2File("so.txt", json);
  29. write2File("libs.txt", soName);
  30. }
  31. private void write2File(String fileName, String data) {
  32. String strFilePath = Environment.getExternalStorageDirectory().getPath() + "/Android/" + fileName;
  33. String strContent = data + "\r\n";
  34. try {
  35. File sfile = new File(strFilePath);
  36. if (!sfile.exists()) {
  37. Log.d("TestFile", "Create the file:" + strFilePath);
  38. sfile.getParentFile().mkdirs();
  39. sfile.createNewFile();
  40. }
  41. RandomAccessFile raf = new RandomAccessFile(sfile, "rwd");
  42. raf.seek(sfile.length());
  43. raf.write(strContent.getBytes());
  44. raf.close();
  45. } catch (Exception e) {
  46. Log.e("TestFile", "Error on write File:" + e);
  47. }
  48. }

Android.mk语法规范

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

闽ICP备14008679号