APP 引用第三方aar包和jar包 Android.bp 和 Android.mk 编写

APP 引用第三方aar包和jar包 Android.bp 和 Android.mk 编写

项目目录结构:

 Android.bp
 AndroidManifest.xml
 Android.mk
 assets
 libs
 res
 src
第三方 aar 包 :phg_lib.aar
第三方 jar 包 : phg_test.jar

Android.bp 编写:

android_library_import {  // 预编译 aar 包
   name: "phglib",
   aars: ["libs/phg_lib.aar"],
   sdk_version: "current",
}

java_import {  // 预编译 jar 包
    name: "phgtest",
    jars: ["libs/phg_test.jar"],
    sdk_version: "current",
}

android_app {
    name: "PHG_APP",
    certificate: "platform",

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

    static_libs: [
        "phglib",  // 引用 aar 包
        "phgtest", //引用 jar 包
        "android-support-v7-appcompat",
        "android-support-constraint-layout",
        "android-support-design",        
        "android.hidl.base-V1.0-java",
    ],

    resource_dirs: ["res"],
    asset_dirs: ["assets"],

    optimize: {  // 有时候编译会出错,需要加这个变量
        enabled: false,
    },

    dex_preopt: {  // 有时候编译会出错,需要加这个变量
        enabled: false,
    },

    platform_apis: true,

    aaptflags: [
       "--auto-add-overlay",
       "--extra-packages",  
       "com.test.phg.service", // 这里要加上 aar 包里面的包名,可以多个
    ],
}

Android.mk

LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)
// 预编译 aar 包 和 jar 包
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := phglib:libs/phg_lib.aar \ 
                                       phgtest:libs/phg_test.jar

include $(BUILD_MULTI_PREBUILT)


include $(CLEAR_VARS)

LOCAL_PACKAGE_NAME = CP_GFspmt

LOCAL_SRC_FILES := $(call all-java-files-under, src)
LOCAL_SRC_FILES += $(call all-Iaidl-files-under, src)
LOCAL_ASSET_DIR := $(LOCAL_PATH)/assets

LOCAL_CERTIFICATE := platform

LOCAL_STATIC_JAVA_LIBRARIES := \
    android-support-v7-appcompat \
    android-support-constraint-layout \
    android-support-design \
    android.hidl.base-V1.0-java \
    phgtest   // 引用jar包
   

LOCAL_STATIC_JAVA_AAR_LIBRARIES += \
    phglib  //引用aar 包

LOCAL_AAPT_FLAGS := \
    --auto-add-overlay \
    --extra-packages com.test.phg.service \  // aar包里的包名,可以多个

#LOCAL_SDK_VERSION := current
LOCAL_PRIVATE_PLATFORM_APIS := true
LOCAL_MIN_SDK_VERSION := 25
#LOCAL_MODULE_TAGS := optional

include $(BUILD_PACKAGE)

Android.mk 和Android.bp 的对应关系,可以查看 Android 源码下
/build/soong/androidmk/cmd/androidmk/android.go 文件

"LOCAL_SHARED_LIBRARIES":            "shared_libs",     【动态 引用 C/C++ 库】
"LOCAL_STATIC_LIBRARIES":            "static_libs",     【静态 引用 C/C++ 库】
"LOCAL_JAVA_LIBRARIES":              "libs",            【动态引用JAVA 库】
"LOCAL_STATIC_JAVA_LIBRARIES":       "static_libs",     【静态引用JAVA库】 

java_library                 会把aidl  java 等文件编译成  .jar 库
android_library              会把 xml 资源文件, aidl  java 等文件 编译成 .aar 库

java_import                   预编译 .jar 库 (引用 第三方 jar 库)
android_library_import       这是预编译 .aar 库 (引用第三方aar库)
android_app_import      这是 预编译 apk,相当于 BUILD_PREBUILT

默认模块可用于在多个模块中重复使用相同的属性 :
defaults  
cc_defaults
java_defaults
如:
cc_defaults {
    name: "gzip_defaults",
    shared_libs: ["libz"],
    stl: "none",
}
cc_binary {
    name: "gzip",
    **defaults**: ["gzip_defaults"],
    srcs: ["src/test/minigzip.c"],
}

hostdex: true                for hiddenapi check . Hostdex is only for ART testing on host: 

privileged: true,    // this needs to be a privileged application
dex_preopt: {     //Make sure the build system doesn't try to resign the APK
       enabled: false,
},

你可能感兴趣的:(Android,android,java)