How to prevent ad blocker from blocking ads on an app How to prevent ad blocker from blocking ads on an app android android

How to prevent ad blocker from blocking ads on an app


I am aware of one way that ad blocking works (on any computer really), they edit the hosts file to point to localhost for all known ad servers. For android this is located in the "etc/hosts" file.

For example, I use admob ads and a host file that I have taken from custom rom lists the folowing admob entries:

127.0.0.1 analytics.admob.com127.0.0.1 mmv.admob.com127.0.0.1 mm.admob.com127.0.0.1 admob.com127.0.0.1 a.admob.com127.0.0.1 jp.admob.com127.0.0.1 c.admob.com127.0.0.1 p.admob.com127.0.0.1 mm1.vip.sc1.admob.com127.0.0.1 media.admob.com127.0.0.1 e.admob.com

Now anytime a process tries to resolve the above addresses they are routed to the address listed to the left of them (localhost) in this case.

What I do in my apps is check this host file and look for any admob entries, if I find any I notify the user that I've detected ad blocking and tell them to remove admob entries from there and do't allow them use of the app.

After all what good does it do me if they're not seeing ads? No point in letting them use the app for free.

Here is a code snippet of how I achieve that:

        BufferedReader in = null;    try     {        in = new BufferedReader(new InputStreamReader(                new FileInputStream("/etc/hosts")));        String line;        while ((line = in.readLine()) != null)        {            if (line.contains("admob"))            {                result = false;                break;            }        }    } 

I vow that all ad supported apps should check this file. You do not need to be root in order to access it, but writing to it might be a different story.

Also, not sure if there is any other files that act the same on a linux based OS, but at any rate we can always check all of those files.

Any suggestions on improving this are welcome.

Also the app called "Ad Free android" needs root access, meaning that it most likely changes the hosts file in order to achieve its goal.


My code for this issue is thusly: -

try {    if (InetAddress.getByName("a.admob.com").getHostAddress().equals("127.0.0.1") ||        InetAddress.getByName("mm.admob.com").getHostAddress().equals("127.0.0.1") ||        InetAddress.getByName("p.admob.com").getHostAddress().equals("127.0.0.1") ||        InetAddress.getByName("r.admob.com").getHostAddress().equals("127.0.0.1")) {        //Naughty Boy - Punishing code goes here.        // In my case its a dialog which goes to the pay-for version         // of my application in the market once the dialog is closed.    }} catch (UnknownHostException e) { } //no internet

Hope that helps.


As developers, we need to do the difficult job of empathizing with the users and find a middle ground between punishing the few who try to take advantage and the many who play by the rules. Mobile advertising is a reasonable way to allow someone to use a functional piece of software for free. The users who employ ad blocking techniques could be considered lost revenue, but if you take a look at the big picture, can also be those who spread the word about your application if they like it. A more gentle approach to running on systems with ads blocked is to display your own "house" ad. Create one or more banner images and display them in the same spot as your normal ad with an ImageView of the same height (e.g. 50dp). If you successfully receive an ad, then set your ImageView's visibility to View.GONE. You can even create a timer to cycle through several house ads to get the user's attention. Clicking on your ad can take the user to the market page to buy the full version.