Troubles with tasks order execution in Gradle

The project I’m working on force to me to test all the project’s libraries separately. Here I have a simplification of the tasks that I’ve defined to test those libraries.

tasks.register('prepareStuffForLibrary_A') {}
tasks.register('prepareStuffForLibrary_B') {}
tasks.register('testLibrary_A') {
    dependsOn prepareStuffForLibrary_A
    finalizedBy connectedAndroidTest
tasks.register('testLibrary_B') {
    dependsOn prepareStuffForLibrary_B
    finalizedBy connectedAndroidTest
tasks.register('allTests') {
    dependsOn testLibrary_A
    dependsOn testLibrary_B

As you can see, for each test, some preparation tasks have to be done before to call connectedAndroidTest . Calling the testLibrary_A and testLibrary_B separately works as expected. But calling allTests behaves not exactly as I would expect. So the order of the tasks that are executed is the following:

Tasks to be executed: [task ':app:prepareStuffForLibrary_A', task ':app:testLibrary_A', task ':app:prepareStuffForLibrary_B', task ':app:testLibrary_B', task ':app:connectedAndroidTest', task ':app:allTestsDemo']

Obviously, that’s not what I want to achieve. My expected execution order would be that connectedAndroidTest is executed at the end of each test.

Do you have some advice how to do that in Gradle?