You’ll want the task that stops the server to finalize the connectedCheck task. However, Android builds are a little tricky since there can be any number of those tasks (at least two, for debug and release build types) so you’ll have to do something like this:
Ok, now I’m confused, I have 2 build.gradle files, one global, the other one in the app folder, so probably the file to only build the app that is running the tests. I don’t know how these files are connected, or what are their dependencies. Anyway, the global build.gradle doesn’t know “android” , and when I use the app build.gradle, the stopHttpServer is well called, but…at the beginning.
I feel like I’m too much a beginner in Gradle to ask questions about that
Yes, this configuration should go in the one in the app folder. That is the build script for the app project itself. Since Android projects are by default multi-project builds, the other build.gradle file exists to contain configuration applicable to all projects.
Can you share the bit of your build script that is defining the ‘stopHttpServer’ task?
I’m not sure in Android Studio a task connectedCheck is called, I see nothing in output about that :
Executing tasks: [adbConnect]
Configuration on demand is an incubating feature.
stop Http Server
start Http Server
Server started in directory D:\src\64bits\products, port 8001
already connected to 192.168.200.92:5555
Total time: 2.292 secs
The first issue is the configuration vs execution ordering issue that nearly everyone new to Gradle gets messed up on. Your println statements are executing during project configuration not necessarily when the particular task is run, which is why the output you are getting is not in the order you would expect. Instead, that logic should go in a task action.
Second, it’s unclear to me what task is responsible for actually running your tests. The code snippet I shared before is for the tests that would live under ‘src/androidTest’. In this case you are running the ‘adbConnect’ task, which from what I can tell won’t actually run any tests.
And it’s also unclear for me. In Android Studio, I’ve just made an Android Tests configuration, it runs Gradle-aware Make, then I added the adbConnect task, and then tests are launched I don’t know how.
Otherwise, in TeamCity, or in command line, I call connectedCheck task. Which make me think I could try in a command line
You probably don’t want to use static variables to store this stuff. It could be something as simple as def server in the root of your build script. The reason for this is the Gradle Daemon reuses classloaders in version 2.4 and newer, so you’ll run into problems with static stuff persisting across multiple build invocations.
I see. Ok, with a def in the root, it works well.
Thank you very much for your help, I’ll keep working on that to have something fully working, but I already have a server starting and stopping, that’s a big step