Developer技术博客 DevEngineer:林建有

Android TargetSDK28 升级SDK版本后适配修改点


Upgrade to TagertSDK 28

前台服务需要添加 ForgroundService权限 否则系统会引发 SecurityException

  <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />	

打开前台服务,需要有通知栏体现:

  Intent intent = new Intent(this, ForegroundService.class);
  ContextCompat.startForegroundService(this, intent);

要请求让服务运行于前台,在ForegroundService.class里需要调用如下方法启动通知栏:

  startForeground(NOTIFICATION_ID, notification);

非SDK API的限制

对非Android SDK API的调用做了限制,通常会出现类似NoSuchClassExceptionNoSuchFieldException异常。

非SDK API包括Android未公开的底层内部实现细节的API反射调用。还有NDK里未公开的方法调用等。

需要排查有哪些非SDK API可以使用如下方法

  @TargetApi(Build.VERSION_CODES.P)
  @RequiresApi(api = Build.VERSION_CODES.P)
  private void setUpStrictMode() {
      if (BuildConfig.DEBUG && Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
          StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                  .detectAll()//检测其他所有问题:Detect everything that's potentially suspect.In the Honeycomb release this includes leaks of SQLite cursors, Activities, and other closable objects but will likely expand in future releases.
                  .detectFileUriExposure()//文件的URI格式为file://协议的错误检测
                  .detectContentUriWithoutPermission()//URI未授权访问检测
                  .detectNonSdkApiUsage()//非SDK API使用检测
                  .build());
  }

网络请求方面

  • 框架安全性变更

    在Android 9.0(28)以上,默认情况下启用网络传输层安全协议 (TLS),在此情况下不支持http明文协议了,网路请求数据将返回无数据。

    要改变该行为,可以AndroidManifest.xml做配置:

     <application
            ...
            android:usesCleartextTraffic="true">
    

    若要支持安全的密文传输,可按照Google的 网络安全配置 去实现

  • 在 Android 6.0 中,我们取消了对 Apache HTTP 客户端的支持

    如果还需要继续能够使用Apache HTTP的类库,按如下配置:

    要继续使用 Apache HTTP 客户端,以 Android 9 及更高版本为目标的应用可以向其 AndroidManifest.xml添加以下内容:

    <<application>
    	<uses-library android:name="org.apache.http.legacy" android:required="false"/>
    </application>
    
    android {//build.gradle
        useLibrary 'org.apache.http.legacy'
    }
    

使用了透明主题的Activity不可设置orientation配置,否则会崩溃。

解决办法是不设置orientation。或设置值为android:screenOrientation="unspecified"

现在强制执行 FLAG_ACTIVITY_NEW_TASK 要求

在 Android 9 中,您不能从非 Activity 环境中启动 Activity,除非您传递 Intent 标志 FLAG_ACTIVITY_NEW_TASK。 如果您尝试在不传递此标志的情况下启动 Activity,则该 Activity 不会启动,系统会在日志中输出一则消息。

注:在 Android 7.0(API 级别 24)之前,标志要求一直是期望的行为并被强制执行。 Android 7.0 中的一个错误会临时阻止实施标志要求。


Similar Posts

Comments