Reputation: 193
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]
Upvotes: 20
Views: 11375
Reputation: 51
This worked for me
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(getApplicationContext().WIFI_SERVICE);
Upvotes: 3
Reputation: 193
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 }
Upvotes: 0
Reputation: 389
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);
Upvotes: 38