Android 使用或覆盖了已过时的 API

编译时,出现类似提示:

: 某些输入文件使用或覆盖了已过时的 API。
注: 有关详细信息, 请使用 -Xlint:deprecation 重新编译。
注: /Volumes/MacSD/Android_Work_AS/sdk.v3new/AS/nBSDK/src/main/java/com/nbsdk/main/PassportGuestLogin.java使用了未经检查或不安全的操作。
注: 有关详细信息, 请使用 -Xlint:unchecked 重新编译。
解决:

1.在在Module的build.gradle中,android节点下,配置如下信息:

    //配置这个并不是让你隐藏过时api,而是让过时api或者不合规矩的语法展现出来
    allprojects {
        gradle.projectsEvaluated {
            tasks.withType(JavaCompile) {
                options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
            }
        }
    }

2.运行Moudle,找出过时api或者异常语句逐个修改吧!以下是我的一些总结:

警告: [deprecation] Resources中的getColor(int)已过时

过时:getResources().getColor(R.color.colorAccent)
改为:ContextCompat.getColor(Context,R.color.colorAccent)

警告: [deprecation] Resources中的getDrawable(int)已过时

过时:sContext.getResources().getDrawable(R.drawable.btn)
改为:ContextCompat.getDrawable(sContext,R.drawable.btn)
警告: [deprecation] WebViewClient中的shouldOverrideUrlLoading(WebView,String)已过时

过时:shouldOverrideUrlLoading(WebView view, String url)
改为:
//我们可以通过request.getUrl()来获取url,当前感觉不要改还是好一些
shouldOverrideUrlLoading(WebView view, WebResourceRequest request)
警告: [deprecation] PackageInfo中的versionCode已过时

过时:packageInfo.versionCode;
改为:packageInfo.getLongVersionCode();

需要注意,getLongVersionCode()是在API-28的时候被加入的。

所以要加版本判断,我这儿只是示例,自己的方法自己改哦:

    @SuppressWarnings("deprecation")
	public static int getVersionCode(Context context) {
		try {
			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
				return (int) context.getPackageManager().getPackageInfo(NBUtils.getBundleID(context), 0).getLongVersionCode();
			}
			return context.getPackageManager().getPackageInfo(NBUtils.getBundleID(context), 0).versionCode;
		} catch (Exception e) {
			return 0;
		}
	}

后续会逐渐补充~
欢迎打赏欢迎打赏欢迎打赏

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值