Detecting Android devices that are blocking ads

A friend of mine pointed me in the direction of an interesting app a few days ago. They had been using it for a while and just installed an ad-blocker, the type that just modifies your host file with a bunch of known ad providers. Then they’re new favorite application stopped working, complaining that the ads where blocked. This may not have been the first attempt to detect ad removal, but it was the first one that I’ve looked at or even heard of. It’s nothing fancy of course, but it’s actually a nifty idea. The way it has been implemented there are plenty of ways to get around it, but it’s definitely was a nifty piece of code. Quiet trivial to patch if someone wanted to, though I see no reason to not support the devs.

Anyway, I quickly just reversed the code, since most people don’t care for reading the output of dexdump;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public static boolean isAdmobDisabled()
{
if (admobDisabled == null)
admobDisabled = Boolean.FALSE;
try
{
FileReader localFileReader = new FileReader("/etc/hosts");
BufferedReader localBufferedReader = new BufferedReader(localFileReader);
String line;
while (null != (line = localBufferedReader.readLine()))
{
if (!line.toLowerCase().contains("admob")) {
admobDisabled = Boolean.TRUE;
break;
}
}
}
catch (Exception e)
{
Log.d("Anti-adremoval", "Something bad happened!");
}
return admobDisabled;
}

Maybe some other dev’s will find this useful.

UPDATE: It would appear the code I reversed was posted on StackOverFlow. No sense in deleting this page though - next time I’ll just have to Google a little better! :)