Android Studio 編譯時,提示程式使用到已棄用的API,但沒指出哪邊使用到,另外提示加上 -Xlint:deprecation 後重新編譯,可取得更詳盡訊息。
Note: ... MainActivity.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details.
[解決方式]
Android Studio 編譯加 -Xlint:deprecation 的方式,
開啟 build.gradle,加上
allprojects { gradle.projectsEvaluated { tasks.withType(JavaCompile) { options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation" } } }修改 build.gradle 後,會詢問將專案同步「Sync Now」,按「Sync Now」同步。
重新編譯,這次的提示訊息,明確指出哪邊使用到已棄用的API了
... MainActivity.java:16: warning: [deprecation] Handler() in Handler has been deprecated new Handler().postDelayed(new Runnable() { ^ 1 warning
原來是 Handler() 這邊有問題
new Handler().postDelayed(new Runnable() { @Override public void run() { ..... } }, 2000);
查了一下 Handler() 用法,新版寫法須顯性指定 Looper
加上 Looper.getMainLooper() 即可:new Handler(Looper.getMainLooper()).postDelayed(new Runnable() { @Override public void run() { ..... } }, 2000);
參考:
- https://www.learnfk.com/question/android/18689365.html
如何将 -Xlint:unchecked 添加到我的基于 Android Gradle 的项目中? - 无涯教程 - https://discuss.gradle.org/t/what-is-xlint-deprecation-and-how-to-use-it/40270
What is -Xlint:deprecation and how to use it - #3 by ArcherEmiya05 - Help/Discuss - Gradle Forums - https://iter01.com/323866.html
Android進階:十四、熟悉Android打包編譯的流程 | IT人 - https://ithelp.ithome.com.tw/articles/10204791
[Android API] [Day 13] Gradle (2) - iT 邦幫忙::一起幫忙解決難題,拯救 IT 人的一天 - https://www.796t.com/content/1546895526.html
buildscript和allprojects的作用和區別是什麼? - 程式人生 - https://stackoverflow.com/questions/28295933/difference-between-build-gradle-project-and-build-gradle-module
android - Difference between build.gradle (Project) and build.gradle (Module) - Stack Overflow - https://stackoverflow.com/questions/61023968/what-do-i-use-now-that-handler-is-deprecated
java - What do I use now that Handler() is deprecated? - Stack Overflow - https://developer.android.com/reference/android/os/Handler#Handler(android.os.Handler.Callback)
Handler | Android Developers
沒有留言:
張貼留言