Android Lint error
Opened this issue · 3 comments
I generated a simple content provider and I get this when i run Android Lint on the newly generated classes:
Do not place Android context classes in static fields (static reference to MyDataStoreProviderSQLiteOpenHelper which has field mContext pointing to Context); this is a memory leak (and also breaks Instant Run)
This is located in the generated ...SQLiteOpenHelper extends SQLiteOpenHelper
class.
This warning can safely be ignored.
You can add @SuppressLint("StaticFieldLeak")
manually for now, and I will add it to the generated code in the next version.
If lint says it's a problem, then I would think that it cannot be ignored.
Sorry, you are wrong about this. The problems reported by lint have different severities (errors, warnings, info, etc.). This one is a warning.
Moreover, static code analyzers are not bullet proof, sometimes they are overzealous - at times they are even plain wrong. A developer should always think and make sure that the reported problem really is one, and not blindly try to "fix" it. That's why they have a mechanism to ignore reported problems (@SuppressLint).
Can you elaborate on why it is safe to ignore that error?
In this particular case, the generated code already has something to say about this (look at the comment inside getInstance
).
As you can see, we only keep a reference to the Application Context. Which is a singleton.