2

After trying to update admob and api level to API 28, I'm getting this Lint warning:

Missing permissions required by MobileAds.initialize: android.permission.INTERNET

when calling these functions:

MobileAds.initialize(context, context.getString(R.string.app_id));
mInterstitialAd.loadAd(new AdRequest.Builder().build());

How is that possible?

I remember that android.permission.INTERNET was not required since a lot of years ago. Apps are able to access Internet without using that permission. Even in this app I have some connections to Internet and they are working successfully. I'm retrieving data and it's working, and also even ads are being displayed. And all without the permission declared on the manifest.

Why does Lint display that warning?

2
  • 4
    I am not aware of any doc indicating that Internet permission is not required .. You have to have android.permission.INTERNET in manifest if you are accessing internet .. If you have some doc indicating this then ad it here . May 2, 2019 at 11:31
  • 2
    If you are not having this permission and able to use internet this maybe because some library in your project already has it and since its not a runtime permission it will auto allowed.. May 2, 2019 at 11:38

4 Answers 4

5

The android.permission.INTERNET permission has been required ever since.

But it's possible that the uses-permission has been declared by a dependency you included, and now it's no longer there, either because you removed that dependency or an updated version of it does not require it anymore. So now you have to request the permission explicitly in your manifest.

1
  • 2
    In order to verify where the permission comes from you can select the "Merged Manifest" tab when editing your app's manifest. May 2, 2019 at 11:41
4

Apps are able to access internet without using that permission

No, they are not.

The permission has not been presented to the user in quite some time — it was automatically granted even before Android 6.0 automatically granted all normal permissions. However, it is still a required permission for network I/O originating from your process.

For example, I created a scrap Kotlin project with coroutines and adjusted MainActivity to be:

package com.commonsware.android.myapplication

import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.BufferedInputStream
import java.io.InputStreamReader
import java.net.HttpURLConnection
import java.net.URL

class MainActivity : AppCompatActivity() {

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    GlobalScope.launch(Dispatchers.Main) {
      doTheDownload()
      Toast.makeText(this@MainActivity, "Done!", Toast.LENGTH_LONG).show()
    }
  }

  private suspend fun doTheDownload() {
    withContext(Dispatchers.IO) {
      val url = URL("https://commonsware.com")
      val urlConnection = url.openConnection() as HttpURLConnection

      try {
        val content =
          InputStreamReader(BufferedInputStream(urlConnection.inputStream)).use {
            it.readText()
          }

        Log.d("This was fun!", content)
      } finally {
        urlConnection.disconnect()
      }
    }
  }
}

Without the INTERNET permission, I crash with:

java.lang.SecurityException: Permission denied (missing INTERNET permission?)
    at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:151)
    at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:105)
    at java.net.InetAddress.getAllByName(InetAddress.java:1154)
    at com.android.okhttp.Dns$1.lookup(Dns.java:39)
    at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:175)
    at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:141)
    at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:83)
    at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:174)
    at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:126)
    at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:95)
    at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:281)
    at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:224)
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:461)
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:407)
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:244)
    at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
    at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:26)
    at com.commonsware.android.myapplication.MainActivity$doTheDownload$2.invokeSuspend(MainActivity.kt:35)
    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
    at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:233)
    at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:594)
    at kotlinx.coroutines.scheduling.CoroutineScheduler.access$runSafely(CoroutineScheduler.kt:60)
    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:742)
 Caused by: android.system.GaiException: android_getaddrinfo failed: EAI_NODATA (No address associated with hostname)
    at libcore.io.Linux.android_getaddrinfo(Native Method)
    at libcore.io.BlockGuardOs.android_getaddrinfo(BlockGuardOs.java:172)
    at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:137)
    at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:105) 
    at java.net.InetAddress.getAllByName(InetAddress.java:1154) 
    at com.android.okhttp.Dns$1.lookup(Dns.java:39) 
    at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:175) 
    at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:141) 
    at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:83) 
    at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:174) 
    at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:126) 
    at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:95) 
    at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:281) 
    at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:224) 
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:461) 
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:407) 
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:244) 
    at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210) 
    at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:26) 
    at com.commonsware.android.myapplication.MainActivity$doTheDownload$2.invokeSuspend(MainActivity.kt:35) 
    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) 
    at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:233) 
    at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:594) 
    at kotlinx.coroutines.scheduling.CoroutineScheduler.access$runSafely(CoroutineScheduler.kt:60) 
    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:742) 
 Caused by: android.system.ErrnoException: android_getaddrinfo failed: EACCES (Permission denied)
    at libcore.io.Linux.android_getaddrinfo(Native Method) 
    at libcore.io.BlockGuardOs.android_getaddrinfo(BlockGuardOs.java:172) 
    at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:137) 
    at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:105) 
    at java.net.InetAddress.getAllByName(InetAddress.java:1154) 
    at com.android.okhttp.Dns$1.lookup(Dns.java:39) 
    at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:175) 
    at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:141) 
    at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:83) 
    at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:174) 
    at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:126) 
    at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:95) 
    at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:281) 
    at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:224) 
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:461) 
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:407) 
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:244) 
    at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210) 
    at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:26) 
    at com.commonsware.android.myapplication.MainActivity$doTheDownload$2.invokeSuspend(MainActivity.kt:35) 
    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) 
    at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:233) 
    at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:594) 
    at kotlinx.coroutines.scheduling.CoroutineScheduler.access$runSafely(CoroutineScheduler.kt:60) 
    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:742) 

With the INTERNET permission, the app downloads the content just fine. Both tests were on Android 9.0.

And all without the permission declared on the manifest.

Perhaps you are getting that permission from a library, though that would not explain AdMob's confusion.

3

android.permission.INTERNET needs to be declared in the manifest, but it has a protection level of normal (i.e. is not dangerous). Normal permissions don't need to request access to the user like dangerous ones, but need to be declared. See Permissions overview (emphasis added):

If an app declares in its manifest that it needs a normal permission, the system automatically grants the app that permission at install time. The system doesn't prompt the user to grant normal permissions, and users cannot revoke these permissions.

1

MobileAds.initialize() is annotated with @RequiresPermission("android.permission.INTERNET").

Lint is stupid and it does not really know about granted-by-default permissions like INTERNET. Instead it analyses your code and sees you're calling this method without a permission check. You can ignore this warning.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.