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

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

引入 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);
        }
    }

Android.mk语法规范

目录
相关文章
|
4天前
|
机器学习/深度学习 Java Shell
[RK3568][Android12.0]--- 系统自带预置第三方APK方法
[RK3568][Android12.0]--- 系统自带预置第三方APK方法
22 0
|
1天前
|
移动开发 jenkins 持续交付
jenkins编译H5做的android端编译卫士app记录
jenkins编译H5做的android端编译卫士app记录
|
1天前
|
Android开发
在android源码中编译ADW_Launcher
在android源码中编译ADW_Launcher
10 2
|
1天前
|
Linux 编译器 开发工具
Android内核的编译过程
Android内核的编译过程
|
2天前
|
Ubuntu Android开发
Android Froyo基于32 bit ubuntu 10.10编译问题
Android Froyo基于32 bit ubuntu 10.10编译问题
|
4天前
|
Shell 开发工具 Android开发
android 修改kernel编译版本信息
android 修改kernel编译版本信息
13 0
|
4天前
|
程序员 Android开发
Android亮度调节的几种实现方法
Android亮度调节的几种实现方法
9 0
|
4天前
|
Shell Android开发
Android Activity重写dump方法实现通过adb调试代码
Android Activity重写dump方法实现通过adb调试代码
11 0
|
4天前
|
Android开发
Android APP 隐藏系统软键盘的方法
Android APP 隐藏系统软键盘的方法
11 0
|
4天前
|
Ubuntu Linux Shell
Android-NDK-clang 编译 FFmpeg
Android-NDK-clang 编译 FFmpeg
13 0