Forum Discussion

Christophe's avatar
Christophe
Level 1.5: Cupcake
3 months ago

AMAPI prepareEnvironment() failing with ApiLevelException on Android 8 despite using DEVICE_POLICY_CONTROLLER role

Hi everyone,

 

I’m implementing a custom DPC (device owner) and integrating AMAPI locally on the device.

 

 On Android 8 / 8.1, calling:

val request = PrepareEnvironmentRequest.builder()
    .setRoles(
        listOf(
            Role.builder()
                .setRoleType(Role.RoleType.DEVICE_POLICY_CONTROLLER)
                .build()
        )
    )
    .setAdmin(admin)
    .build()

 

immediately fails with:

com.google.android.managementapi.common.exceptions.ApiLevelException

 

On Android 10 and above, I don't have this exception.

 

According to the AMAPI documentation:

If the device's SDK API level is insufficient for certain requested roles (this may be in addition to a general minimum API level requirement for the call itself).{@code Role.RoleType.DEVICE_POLICY_CONTROLLER} requires API level 23 or above. Any other roles require API level 28 or above. 

 

I am using the latest AMAPI client library:

com.google.android.libraries.enterprise.amapi:amapi:1.7.0

 

Questions

Is AMAPI (EnvironmentClient + Device Policy Controller role) still officially supported on Android 8/8.1?

 

Any clarification on the real minimum supported API level for AMAPI prepareEnvironment() would be greatly appreciated, as the documentation suggests Android 8 should work, but the behavior indicates otherwise.

 

Thanks!

2 Replies

  • Emilie_B's avatar
    Emilie_B
    Google Community Manager
    3 months ago

    Hi Christophe​ and happy Friday to you!

     

    I've consulted our team and they said that, while the Android Management API cloud service supports older OS versions, the AMAPI Local SDK (v1.7.0) has a stricter requirement.

    Using prepareEnvironment() with the DEVICE_POLICY_CONTROLLER role requires Android 10 (API 29) or higher.


    The ApiLevelException you are seeing on Android 8 is expected and intentional. The SDK relies on backend features that only arrived with Android 10, so it can't find them on older devices.

     

    Some workaround options that might help you:
    Drop Android 8/9 support for this feature (Set min SDK to 29).
    Fork your logic: Use the AMAPI SDK for Android 10+, and implement standard native DevicePolicyManager APIs for Android 8.

     

    I hope this is useful? 

     

    Please do let us know how you get on and if we can help any further 🙂

     

    Chat soon, 

    Emilie 

    • Christophe's avatar
      Christophe
      Level 1.5: Cupcake
      3 months ago

      Hi Emilie,

       

      Thanks a lot for your detailed answer and for checking with the team!

       

      I just have one follow-up because I think there is a mismatch between the current behaviour and the public documentation. According to the AMAPI docs for EnvironmentClient.prepareEnvironment() https://developers.google.com/android/management/reference/amapi/com/google/android/managementapi/environment/EnvironmentClient#prepareEnvironment(com.google.android.managementapi.environment.model.PrepareEnvironmentRequest,android.content.ComponentName)

       

      it says:

      If the device's SDK API level is insufficient for certain requested roles… Role.RoleType.DEVICE_POLICY_CONTROLLER requires API level 23 or above. Any other roles require API level 28 or above.

       

      Based on this, I would expect ApiLevelException to be thrown only for devices below API 23 when using the DEVICE_POLICY_CONTROLLER role.

      In reality, with the Local SDK 1.7.0, the exception is thrown on Android 8 / 8.1 (and apparently 9), and the effective minimum seems to be Android 10 (API 29).

       

      If that’s the intended behaviour going forward, it might be worth updating the documentation to make this clearer (e.g. explicitly stating that prepareEnvironment() + DEVICE_POLICY_CONTROLLER via the Local SDK requires API 29+).

       

      I also have a couple of technical follow-up questions to help me implement the suggested workaround:

       

      1. Implementing native DevicePolicyManager logic on Android 8/9
        Do you have any guidance or examples on how to reproduce the equivalent behaviour using the standard DevicePolicyManager APIs?
        Concretely: which parts of what AMAPI performs during prepareEnvironment() should I replicate manually on those older OS versions?
      2. What exactly does prepareEnvironment() do under the hood?
        On a high level, when using prepareEnvironment() with the DEVICE_POLICY_CONTROLLER role, does the AMAPI Local SDK:
        - install or update the CloudDPC app?
        - configure/update Google Play / Play Store?
        - perform additional provisioning or policy setup that I need to replicate in the fallback path
      3.  Account provisioning on Android 8/9
        If reproducing the logic of prepareEnvironment() manually is not realistically feasible, should I continue using the old androidForWorkAccountSupport flow from dpcsupport to add a Google for Work account on Android 8 and 9?
        Is this still the recommended approach for legacy devices?
      4. Source access
        Is the AMAPI Local SDK source code (or at least the parts related to prepareEnvironment()) available anywhere, or is it fully closed-source? Any insight would help align the native fallback.

       

       Thanks again for your help and for clarifying the API level requirements — knowing that the ApiLevelException on Android 8 is intentional is very helpful.

      Any additional detail you can share on the above points would also be greatly appreciated.

       

      Best regards,

      Christophe