android – signed apk error [WifiManagerLeak]-ThrowExceptions

Exception or error:

I can create debug apk. But when I try to generate a signed apk, I receive the following message:

Error: The WIFI_SERVICE must be looked up on the Application
context or memory will leak on devices < Android N. Try changing to
.getApplicationContext() [WifiManagerLeak]

How to solve:

As the error suggests, it seems that WiFiManager must use the ApplicationContext, as opposed to the ActivityContext, otherwise a memory leak can occur. The error was triggered by following code:

WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);

To fix the issue I replaced the above line with:

WifiManager wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);

###

This worked for me

WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(getApplicationContext().WIFI_SERVICE);

###

i have added following code in build.gradel – module.app and now successfully build the app

( suggested by android studio itself )

lintOptions {
checkReleaseBuilds false
// Or, if you prefer, you can continue to check for errors in release builds,
// but continue the build even when errors are found:
abortOnError false
}

Leave a Reply

Your email address will not be published. Required fields are marked *