简体   繁体   中英

How do I add condition in Android.bp

I want to differentiate code between Android Q and Android R how do I achieve this in Android.bp? In Android.mk I did something like this

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

How to do above code in Android.bp?

Follow the instructions given here .

Replace this part in the my_defaults.go :

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

With:

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

Reference: link . In older versions this function was called PlatformVersion() ( link ), but for Android 9 or higher you should be fine.

GO

You can write a go script with your conditions. Follow

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?

Conditional compilation

If you just want to include a module conditionally you can define many Android.bp modules and include them based on conditions in Android.mk files. https://android.googlesource.com/platform/build/soong/+/HEAD/docs/best_practices.md#removing-conditionals

soong_config_modules

You will face parsing errors if any of your Android.bp files include libraries not supported by your AOSP.

To fix that you can use soong_config_modules . Note that this is supported only Android 11 R onwards.

Details are given in https://android.googlesource.com/platform/build/soong/+/refs/heads/master/android/soong_config_modules.go

However, I'll give you an example.

Here I am including special libraries when the AOSP is Android 12. Since these libraries might not be present on lower versions, I will not include iot-camera-system.mk in PRODUCT_PACKAGES so it will not be included as a preinstalled app.

What specifically I will achieve with soong_config_modules is removal of parsing errors when these libraries are not present (Android parses all Android.bp files to form a parse tree, it also checks for the existence of their dependencies and build fails when they are not present).

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,
  },
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM