【问题标题】:How do I add condition in Android.bp如何在 Android.bp 中添加条件
【发布时间】:2020-04-14 06:47:03
【问题描述】:

我想区分 Android Q 和 Android R 之间的代码如何在 Android.bp 中实现这一点? 在 Android.mk 我做了这样的事情

ifeq ($(PLATFORM_VERSION), R)
LOCAL_CFLAGS += -DANDROID_R_AOSP
else
LOCAL_CFLAGS += -DANDROID_Q_AOSP

上面的代码在Android.bp中怎么做?

【问题讨论】:

标签: android compilation android-source android-soong


【解决方案1】:

按照here 给出的说明进行操作。

替换my_defaults.go中的这部分:

    if ctx.AConfig().Getenv("SOME_ENV_VAR") == "some_value" {
        cflags = append(cflags, "-DCONDITIONAL")
    }

与:

    if ctx.AConfig().PlatformVersionName() == "R" {
        cflags = append(cflags, "-DANDROID_R_AOSP")
    } else {
        cflags = append(cflags, "-DANDROID_Q_AOSP")
    }

参考:link。在旧版本中,此函数称为 PlatformVersion() (link),但对于 Android 9 或更高版本应该没问题。

【讨论】:

    【解决方案2】:

    你可以根据你的条件编写一个 go 脚本。关注

    Is there a way to add/remove module in Android.bp?

    What is art.go? And why is it considered a way to write conditionals in bp files?

    条件编译

    如果您只想有条件地包含一个模块,您可以定义许多Android.bp 模块并根据Android.mk 文件中的条件包含它们。 https://android.googlesource.com/platform/build/soong/+/HEAD/docs/best_practices.md#removing-conditionals

    soong_config_modules

    如果您的任何 Android.bp 文件包含您的 AOSP 不支持的库,您将面临解析错误。

    要解决此问题,您可以使用 soong_config_modules。请注意,这仅在 Android 11 R 之后受支持。

    详情见https://android.googlesource.com/platform/build/soong/+/refs/heads/master/android/soong_config_modules.go

    不过,我给你举个例子。

    在这里,当 AOSP 为 Android 12 时,我将包含特殊库。由于这些库可能不存在于较低版本中,因此我不会在 PRODUCT_PACKAGES 中包含 iot-camera-system.mk,因此它不会作为预安装的应用程序包含在内。

    我将使用soong_config_modules 实现的具体目标是在这些库不存在时消除解析错误(Android 解析所有Android.bp 文件以形成解析树,它还会检查它们的依赖关系是否存在,并且构建失败时它们不存在)。

    iot-camera-system.mk

    # CameraApp Android Application Package
    
    ifeq ($(PLATFORM_VERSION), $(filter $(PLATFORM_VERSION),S 12))
      PRODUCT_PACKAGES += CameraApp
      SOONG_CONFIG_NAMESPACES += camera
      SOONG_CONFIG_camera += camtargets
      SOONG_CONFIG_camera_camtargets := newCameraTarget
    else
      SOONG_CONFIG_NAMESPACES += camera
      SOONG_CONFIG_camera += camtargets
      SOONG_CONFIG_camera_camtargets := oldCameraTarget
    endif
    

    Android.bp

    // This introduces the module type camera_cc_defaults
    // If target.mk file contained:
    //
    // SOONG_CONFIG_NAMESPACES += camera
    // SOONG_CONFIG_camera += camtargets
    // SOONG_CONFIG_camera_camtargets := newCameraTarget
    //
    // Then our libs would build with static_libs
    
    soong_config_module_type {
        name: "camera_cc_defaults",
        module_type: "cc_defaults",
        config_namespace: "camera",
        variables: ["camtargets"],
        properties: ["static_libs"],
    }
    
    soong_config_string_variable {
        name: "camtargets",
        values: ["oldCameraTarget", "newCameraTarget"],
    }
    
    camera_cc_defaults {
        name: "camera_defaults",
        soong_config_variables: {
            camtargets: {
                oldCameraTarget: {
                    static_libs: [
                    ],
                },
                newCameraTarget: {
                    static_libs: [
                        "androidx.core_core-ktx",
                        "androidx.fragment_fragment-ktx",
                        "androidx.navigation_navigation-fragment-ktx",
                        "androidx.navigation_navigation-ui-ktx",
                        "androidx.lifecycle_lifecycle-runtime-ktx",
                        "kotlinx_coroutines",
                        "kotlinx_coroutines_android",
                    ],
                },
            },
        },
    }
    
    android_library {
      name: "utils",
      defaults: ["camera_defaults"],
      manifest: "utils/src/main/AndroidManifest.xml",
      platform_apis: true,
    
      srcs: [
        "utils/src/main/java/com/example/android/camera/utils/*.kt",
      ],
    
      resource_dirs: [
        "utils/src/main/res/",
      ],
    
      static_libs: [
        "androidx-constraintlayout_constraintlayout",
        "androidx.appcompat_appcompat",
        "androidx.localbroadcastmanager_localbroadcastmanager",
        "com.google.android.material_material",
        "androidx.exifinterface_exifinterface",
        "androidx.core_core",
        "androidx.preference_preference",
        "androidx.fragment_fragment",
        "androidx.recyclerview_recyclerview",
        "androidx.lifecycle_lifecycle-runtime",
        "androidx.lifecycle_lifecycle-extensions",
        "kotlin-stdlib",
        "kotlin-reflect",
        "gson-prebuilt-jar",
      ],
    
      optimize: {
        enabled: false,
      },
      dex_preopt: {
        enabled: false,
      },
    }
    
    android_app {
      name: "CameraApp",
      defaults: ["camera_defaults"],
      manifest: "app/src/main/AndroidManifest.xml",
      privileged: true,
      platform_apis: true,
      certificate: "platform",
    
      srcs: [
        "app/src/main/java/com/example/android/camera2/video/*.kt",
        "app/src/main/java/com/example/android/camera2/video/fragments/*.kt",
        "app/src/main/java/com/example/android/camera2/video/overlay/*.kt",
      ],
    
      resource_dirs: [
        "app/src/main/res/",
      ],
    
      static_libs: [
        "androidx-constraintlayout_constraintlayout",
        "androidx.appcompat_appcompat",
        "androidx.localbroadcastmanager_localbroadcastmanager",
        "com.google.android.material_material",
        "androidx.exifinterface_exifinterface",
        "androidx.core_core",
        "androidx.preference_preference",
        "androidx.fragment_fragment",
        "androidx.recyclerview_recyclerview",
        "androidx.lifecycle_lifecycle-runtime",
        "androidx.lifecycle_lifecycle-extensions",
        "kotlin-stdlib",
        "kotlin-reflect",
        "gson-prebuilt-jar",
        "utils",
      ],
    
      optimize: {
        enabled: false,
      },
      dex_preopt: {
        enabled: false,
      },
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-12
      • 1970-01-01
      • 2017-11-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多