「Android/非推奨コード」の版間の差分
(→webview系の非推奨コード) |
|||
| (同じ利用者による、間の9版が非表示) | |||
| 行1: | 行1: | ||
| − | == | + | ==非推奨(deprecation)のコードを可視化する== |
build.gradle | build.gradle | ||
allprojects { | allprojects { | ||
| 行23: | 行23: | ||
NetworkクラスがAPI23から使えるようになってるのでそちらで置き換える。 | NetworkクラスがAPI23から使えるようになってるのでそちらで置き換える。 | ||
<pre> | <pre> | ||
| − | ConnectivityManager manager = (ConnectivityManager) | + | private int getConnectionState() { |
| − | + | ConnectivityManager manager = (ConnectivityManager) | |
| − | NetworkInfo networkInfo = manager.getActiveNetworkInfo(); | + | context.getSystemService(Context.CONNECTIVITY_SERVICE); |
| − | if (networkInfo != null && networkInfo.isConnected()) { | + | NetworkInfo networkInfo = manager.getActiveNetworkInfo(); |
| − | + | if (networkInfo != null && networkInfo.isConnected()) { | |
| − | } else { | + | Log.i("tag", "connected"); |
| − | + | } else { | |
| − | + | Log.i("tag", "not connected"); | |
| − | } | + | return; |
| − | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | + | } |
| − | + | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | |
| − | + | Network network = manager.getActiveNetwork(); | |
| − | + | if (network != null) { | |
| − | + | NetworkCapabilities capabilities = manager.getNetworkCapabilities(network); | |
| − | + | if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) { | |
| − | + | Log.i("tag", "wifi m"); | |
| − | + | Toast.makeText(context, "wifi m", Toast.LENGTH_LONG).show(); | |
| − | + | } else { | |
| − | + | Log.i("tag", "mobile"); | |
| + | Toast.makeText(context, "mobile m", Toast.LENGTH_LONG).show(); | ||
| + | } | ||
} | } | ||
| + | } else { | ||
| + | return getConnectionStateLessThanVersionCode23(); | ||
} | } | ||
| − | } | + | } |
| + | @SuppressWarnings("deprecation") | ||
| + | private int getConnectionStateLessThanVersionCode23() { | ||
| + | ConnectivityManager manager = (ConnectivityManager) | ||
| + | context.getSystemService(Context.CONNECTIVITY_SERVICE); | ||
if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) { | if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) { | ||
Log.i("tag", "wifi"); | Log.i("tag", "wifi"); | ||
| 行54: | 行62: | ||
} | } | ||
</pre> | </pre> | ||
| + | |||
| + | Networkクラス公式:https://developer.android.com/reference/android/net/NetworkInfo | ||
| + | |||
| + | 参考:https://gist.github.com/mogimogitomato/aa0426fb0f8f9d3e42d64d3c4ff8c67f | ||
| + | |||
| + | ==警告: [unchecked] 無検査変換== | ||
| + | HashMap map = new HashMap(); | ||
| + | ↓ | ||
| + | HashMap<Integer, String> map = new HashMap<Integer, String>(); | ||
==Android7.0以降getConfiguration().localeは非推奨== | ==Android7.0以降getConfiguration().localeは非推奨== | ||
| 行68: | 行85: | ||
} | } | ||
参考:https://stackoverflow.com/questions/38267213/how-to-get-the-current-locale-api-level-24 | 参考:https://stackoverflow.com/questions/38267213/how-to-get-the-current-locale-api-level-24 | ||
| + | |||
| + | ==警告: [deprecation] AudioManagerのabandonAudioFocus(OnAudioFocusChangeListener)は非推奨になりました== | ||
| + | @SuppressWarnings("deprecation") | ||
| + | public void execAudioFocus() { | ||
| + | AudioManager am = (AudioManager) activity.getSystemService(Context.AUDIO_SERVICE); | ||
| + | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
| + | int result = am.abandonAudioFocusRequest(null); | ||
| + | } else { | ||
| + | int result = am.abandonAudioFocus(null); | ||
| + | } | ||
| + | } | ||
| + | 公式AudioManager:https://developer.android.com/reference/android/media/AudioManager | ||
==expected resource of type id ensures that resource ids passed to apis are of the right type警告== | ==expected resource of type id ensures that resource ids passed to apis are of the right type警告== | ||
| 行79: | 行108: | ||
==value must be same parameter are required to in a particular警告== | ==value must be same parameter are required to in a particular警告== | ||
変数に型の範囲外の数字を入れてる | 変数に型の範囲外の数字を入れてる | ||
| + | |||
| + | ==support:appcompatがmixing versions can lead to runtime crashesの警告== | ||
| + | build.gradle | ||
| + | implementation 'com.google.android.gms:play-services-ads:15.0.1' | ||
| + | implementation 'com.android.support:appcompat-v7:28.0.0-rc01' | ||
| + | play-services-ads:15.0.1がsupport26.1.0に依存してるっぽいので警告がでる | ||
| + | |||
| + | ==sonarで解析== | ||
| + | [[セキュリティ/静的解析/Sonar/android]] [ショートカット] | ||
2020年3月23日 (月) 17:35時点における最新版
目次
- 1 非推奨(deprecation)のコードを可視化する
- 2 非推奨コードを特定クラスのみ非表示とする
- 3 webview系の非推奨コード
- 4 警告: [deprecation] NetworkInfoのgetType()は非推奨になりました
- 5 警告: [unchecked] 無検査変換
- 6 Android7.0以降getConfiguration().localeは非推奨
- 7 警告: [deprecation] AudioManagerのabandonAudioFocus(OnAudioFocusChangeListener)は非推奨になりました
- 8 expected resource of type id ensures that resource ids passed to apis are of the right type警告
- 9 cannot resolve symbol警告
- 10 value must be same parameter are required to in a particular警告
- 11 support:appcompatがmixing versions can lead to runtime crashesの警告
- 12 sonarで解析
非推奨(deprecation)のコードを可視化する
build.gradle
allprojects {
gradle.projectsEvaluated {
tasks.withType(JavaCompile) {
options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
}
}
}
build/clear後 buildするとコンソールに非推奨コードが表示される
参考:https://qiita.com/nagasakulllo/items/48764c68e6b922c74f67
非推奨コードを特定クラスのみ非表示とする
クラスかメソッドの頭に以下アノテーションを付ける
@SuppressWarnings("deprecation")
webview系の非推奨コード
android/webview/非推奨コード [ショートカット]
警告: [deprecation] NetworkInfoのgetType()は非推奨になりました
NetworkクラスがAPI23から使えるようになってるのでそちらで置き換える。
private int getConnectionState() {
ConnectivityManager manager = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
Log.i("tag", "connected");
} else {
Log.i("tag", "not connected");
return;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Network network = manager.getActiveNetwork();
if (network != null) {
NetworkCapabilities capabilities = manager.getNetworkCapabilities(network);
if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
Log.i("tag", "wifi m");
Toast.makeText(context, "wifi m", Toast.LENGTH_LONG).show();
} else {
Log.i("tag", "mobile");
Toast.makeText(context, "mobile m", Toast.LENGTH_LONG).show();
}
}
} else {
return getConnectionStateLessThanVersionCode23();
}
}
@SuppressWarnings("deprecation")
private int getConnectionStateLessThanVersionCode23() {
ConnectivityManager manager = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
Log.i("tag", "wifi");
Toast.makeText(context, "wifi", Toast.LENGTH_LONG).show();
} else {
Log.i("tag", "mobile");
Toast.makeText(context, "mobile", Toast.LENGTH_LONG).show();
}
}
Networkクラス公式:https://developer.android.com/reference/android/net/NetworkInfo
参考:https://gist.github.com/mogimogitomato/aa0426fb0f8f9d3e42d64d3c4ff8c67f
警告: [unchecked] 無検査変換
HashMap map = new HashMap();
↓
HashMap<Integer, String> map = new HashMap<Integer, String>();
Android7.0以降getConfiguration().localeは非推奨
Locale locale = AppStatic.getLocale(context);
@SuppressWarnings("deprecation")
public static Locale getLocale(Context context) {
Locale locale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return context.getResources().getConfiguration().getLocales().get(0);
} else {
return context.getResources().getConfiguration().locale;
}
}
参考:https://stackoverflow.com/questions/38267213/how-to-get-the-current-locale-api-level-24
警告: [deprecation] AudioManagerのabandonAudioFocus(OnAudioFocusChangeListener)は非推奨になりました
@SuppressWarnings("deprecation")
public void execAudioFocus() {
AudioManager am = (AudioManager) activity.getSystemService(Context.AUDIO_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
int result = am.abandonAudioFocusRequest(null);
} else {
int result = am.abandonAudioFocus(null);
}
}
公式AudioManager:https://developer.android.com/reference/android/media/AudioManager
expected resource of type id ensures that resource ids passed to apis are of the right type警告
viewのresourceIdを直接入力すると出るエラーなのでView.generateViewId()にする。ただしbuildSDKLevel17以上から
cannot resolve symbol警告
@paramの変数や@seeのクラスが間違っているのでその部分を修正すればよい。
@paramであれば変数名を記載するようにし、@seeであればその変数のクラス名を書いてあげれば良い。
value must be same parameter are required to in a particular警告
変数に型の範囲外の数字を入れてる
support:appcompatがmixing versions can lead to runtime crashesの警告
build.gradle
implementation 'com.google.android.gms:play-services-ads:15.0.1' implementation 'com.android.support:appcompat-v7:28.0.0-rc01'
play-services-ads:15.0.1がsupport26.1.0に依存してるっぽいので警告がでる
sonarで解析
セキュリティ/静的解析/Sonar/android [ショートカット]
