How do I identify the device that Android tests are currently running on?

I have multiple devices connected and I run the gradle 'connectedCheck' command which runs tests on multiple devices.

ConnectedCheck runs tests on all devices in an order. I want to get the serial number of the device that the tests are currently running on.

Does android provide a way to do this?


source to share

2 answers

You can get detailed information about your Android device like:

//Get the instance of TelephonyManager  
    TelephonyManager  tm=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);  

    //Calling the methods of TelephonyManager the returns the information  
    String IMEINumber=tm.getDeviceId();  
    String subscriberID=tm.getDeviceId();  
    String SIMSerialNumber=tm.getSimSerialNumber();  
    String networkCountryISO=tm.getNetworkCountryIso();  
    String SIMCountryISO=tm.getSimCountryIso();  
    String softwareVersion=tm.getDeviceSoftwareVersion();  
    String voiceMailNumber=tm.getVoiceMailNumber();  

    //Get the phone type  
    String strphoneType="";  

    int phoneType=tm.getPhoneType();  

    switch (phoneType)   
            case (TelephonyManager.PHONE_TYPE_CDMA):  
            case (TelephonyManager.PHONE_TYPE_GSM):   
            case (TelephonyManager.PHONE_TYPE_NONE):  


And to get the device model name we use:

public String getDeviceName() {
String manufacturer = Build.MANUFACTURER;
String model = Build.MODEL;
if (model.startsWith(manufacturer)) {
    return capitalize(model);
} else {
    return capitalize(manufacturer) + " " + model;

private String capitalize(String s) {
if (s == null || s.length() == 0) {
    return "";
char first = s.charAt(0);
if (Character.isUpperCase(first)) {
    return s;
} else {
    return Character.toUpperCase(first) + s.substring(1);




I found a solution! In fact, there was no problem in the first place.

Problem: Gradle Android plugin task for running tests is 'connectedCheck'. It performs tests on how many Android devices have ever been connected to the host. Now when I @Ignore the test will be ignored on all those devices where I wanted to do it, only for the faulty device / devices

Solution: Add a custom jUnit annotation that accepts an array of String values โ€‹โ€‹for sequential device IDs. Write a custom test runner by extending BlockJUnit4ClassRunner override runChild to check if Build.Device is the same as in annotation if method is annotated.

If it is fireTestIgnored Ignore so that it is skipped only for the device specified in the annotation.



All Articles