It turns out that it was bug in Gradle. I used 8.0 version and after updating to 8.13 it works without problems. Gradle version is set in gradle-wrapper.properties file. So to sum it up: it works ok with Android Gradle Plugin version 8.1.4 and Gradle version 8.13