Combuster Combuster - 2 months ago 17x
Android Question

Getting the Android SDK directory within a gradle task

Recently the gradle plugin for android got updated (with android studio), after which the previous way of getting to the SDK directory ceased to work. The expression


which worked in an older version now returns the error

Error:(42, 0) No such property: sdkDirectory for class:

What would be the proper way of getting the android SDK directory being used, preferably independent of the user's configuration such as plugin and gradle version? The script needs to be shareable with several users.


Since all the previous answers depend on the environment or specific user intervention on top of normal configuration, I'll just post my technically messy fix.

if (android.hasProperty('plugin')) {
    if (android.plugin.hasProperty('sdkHandler')) {
        androidPath = android.plugin.sdkHandler.sdkFolder
    } else {
        androidPath = android.plugin.sdkDirectory
} else {
    androidPath = android.sdkDirectory

Unlike all previous methods, this actually works, but it still looks hacky.