Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

signed apk error [WifiManagerLeak]

Tags:

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]

like image 742
Dhruv Shah Avatar asked Mar 06 '17 08:03

Dhruv Shah


2 Answers

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);
like image 171
acorn sprout Avatar answered Sep 21 '22 03:09

acorn sprout


This worked for me

WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(getApplicationContext().WIFI_SERVICE);
like image 40
Amarnath Avatar answered Sep 21 '22 03:09

Amarnath