Android 12 适配升级小结 原创

阿策小和尚
发布于 2022-2-8 10:13
浏览
1收藏

    春节不停更,此文正在参加「星光计划-春节更帖活动」
    Android 12 发布已经有 4 个月了,而且随着各大市场对应用适配的要求逐渐提高,小菜也尝试将一个历史的应用简单升级适配 Android 12

Android 12

    Android 12 对应 Build.VERSION_CODES.S,使用 Material You 打造的全新系统界面,富有表现力、活力和个性。使用重新设计的微件、AppSearch、游戏模式和新的编解码器扩展您的应用。支持隐私信息中心和大致位置等新的保护功能。使用富媒体内容插入功能、更简便的模糊处理功能、经过改进的原生调试功能等提高工作效率。

    Android 12 相对我们的历史项目来说属于较大版本的更新,在适配过程中遇到一系列问题,小菜简单记录整理一下。

SDK 版本号升级

    小菜首先对 SDK 版本号进行升级,之后对升级后的应用逐步进行适配更新;

当前版本

minSdkVersion = 17
targetSdkVersion = 28
compileSdkVersion = 28
buildToolsVersion = '28.0.3'

升级后版本

minSdkVersion = 17
targetSdkVersion = 31
compileSdkVersion = 31
buildToolsVersion = '31.0.0'

Q1: Gradle 不匹配

    升级 SDKsync 后遇到第一个 Gradle 不匹配问题;

Installed Build Tools revision 31.0.0 is corrupted. Remove and install again using the SDK Manager.

Android 12 适配升级小结-鸿蒙开发者社区

A1: 升级 Android 12 对应 Gradle 版本

    历史版本 Gradle 对应版本是 3.3.3,升级到最新的 7.0.4

classpath 'com.android.tools.build:gradle:7.0.4'

Q2: distributionUrl 不匹配

Caused by: org.gradle.api.internal.plugins.PluginApplicationException: Failed to apply plugin [id 'com.android.internal.version-check']
    at com.android.build.gradle.BasePlugin.apply(BasePlugin.kt:33)
	at com.android.build.gradle.LibraryPlugin.apply(LibraryPlugin.kt:26)
	at build_21d4k8dpcp55f408j9ar3yifm.run(/Users/user/Documents/workspace/App/adlibrary/build.gradle:1)
Caused by: java.lang.RuntimeException: Minimum supported Gradle version is 7.0.2. Current version is 6.1.1. If using the gradle wrapper, try editing the distributionUrl in /Users/user/Documents/workspace/App/gradle/wrapper/gradle-wrapper.properties to gradle-7.0.2-all.zip
	at com.android.build.gradle.internal.plugins.VersionCheckPlugin.apply(VersionCheckPlugin.kt:59)
	at com.android.build.gradle.internal.plugins.VersionCheckPlugin.apply(VersionCheckPlugin.kt:33)
	...

Android 12 适配升级小结-鸿蒙开发者社区

A2: 升级 Android 12 对应 distributionUrl Gradle 版本

    小菜将本地 gradle-wrapper.properties 中升级到与 classpath 一致的 7.0.2-all 即可;

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-all.zip

Q3: Marven 仓库不支持 Http

    小菜历史项目中引入了很多公司内部仓库和部分非 Https 的线上库,在 Gradle 升级后,Marven 仓库建议使用 Https 安全的仓库;

Could not resolve all dependencies for configuration ':classpath'.
   > Using insecure protocols with repositories, without explicit opt-in, is unsupported. Switch Maven repository 'maven(http://0.0.0.0:80/xxx/App)' to redirect to a secure protocol (like HTTPS) or allow insecure protocols. 
   See https://docs.gradle.org/7.0.2/dsl/org.gradle.api.artifacts.repositories.UrlArtifactRepository.html#org.gradle.api.artifacts.repositories.UrlArtifactRepository:allowInsecureProtocol for more details. 

Android 12 适配升级小结-鸿蒙开发者社区

A3: 升级 Https 线上库或解决安全警告

    对于部分线上 Marven 仓库可以更新至 Https,对于不可更新的库可以通过添加 allowInsecureProtocol 属性解决 Gradle 仓库地址的不安全警告;

repositories {
    maven {
      url "http://0.0.0.0:80/xxx/App"
      allowInsecureProtocol = true
    }
}

Q4: compile 弃用

    小菜的历史项目中有个别 Module 中未及时修改 compile(),而 Gradle 升级之后已完全弃用 compile()

A problem occurred evaluating project ':lib'.
> Could not find method compile() for arguments [directory 'libs'] on object of type
    org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

Android 12 适配升级小结-鸿蒙开发者社区

A4: 更新为 api 或 implementation

    根据具体的业务需求将 compile() 更新为 api / implementation 即可;

api fileTree(dir: 'libs', include: ['*.jar'])

Q5: Android 工程依赖的 Java 版本过低

A problem occurred evaluating project ':lib'.
> Failed to apply plugin 'com.android.internal.library'.
   > Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8.
     You can try some of the following options:
       - changing the IDE settings.
       - changing the JAVA_HOME environment variable.
       - changing `org.gradle.java.home` in `gradle.properties`.

Android 12 适配升级小结-鸿蒙开发者社区

A5: 更新 Android 依赖版本为 jdk 11.0.13

    通过 AndroidStudio -> Preferences… -> Gradle 更新 jdk 版本即可;
Android 12 适配升级小结-鸿蒙开发者社区

Q6: AGCPluginTask 中 randomEncryptComponent 属性不应使用 @Optional 进行注释

    小菜的历史项目中使用了 Huawei HMS 推送等,使用的 Marven 库版本较低,与升级后的 Gradle 不兼容;

1: Task failed with an exception.
-----------
* What went wrong:
Execution failed for task ':push:processDebugManifest'.
> A failure occurred while executing com.android.build.gradle.tasks.ProcessLibraryManifest$ProcessLibWorkAction
   > Manifest merger failed with multiple errors, see logs

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
==============================================================================

2: Task failed with an exception.
-----------
* What went wrong:
A problem was found with the configuration of task ':app:processDebugAGCPlugin' (type 'AGCPluginTask').
  - Type 'com.huawei.agconnect.agcp.AGCPluginTask' property 'randomEncryptComponent' of type boolean shouldn't be annotated with @Optional.
    
    Reason: Properties of primitive type cannot be optional.
    
    Possible solutions:
      1. Remove the @Optional annotation.
      2. Use the java.lang.Boolean type instead.
    
    Please refer to https://docs.gradle.org/7.0.2/userguide/validation_problems.html#cannot_use_optional_on_primitive_types for more details about this problem.

Android 12 适配升级小结-鸿蒙开发者社区

A6: 升级华为 HMS 库版本

classpath 'com.huawei.agconnect:agcp:1.5.2.300'

Q7: 使用 Intent 过滤器的 Service 需设置 exported 属性

    此元素设置 Activity 是否可由其他应用的组件启动 —“true”表示可以,“false”表示不可以。若为“false”,则 Activity 只能由同一应用的组件或使用同一用户 ID 的不同应用启动。
    如果您使用的是 Intent 过滤器,则不应将此元素设置为“false”。否则,在应用尝试调用 Activity 时,系统会抛出 ActivityNotFoundException 异常。相反,您不应为其设置 Intent 过滤器,以免其他应用调用 Activity。

    如果没有 Intent 过滤器,则此元素的默认值为“false”。如果您将元素设置为“true”,则任何知道其确切类名的应用均可访问 Activity,但在系统尝试匹配隐式 Intent 时,该 Activity 无法解析。

    此属性并非是限制 Activity 向其他应用公开的唯一方式。您还可使用权限来限制哪些外部实体能够调用 Activity

/.../src/main/AndroidManifest.xml Error:
	android:exported needs to be explicitly specified for <service>. Apps targeting Android 12 and higher are required to specify an explicit value for `android:exported` when the corresponding component has an intent filter defined. 
	See https://developer.android.com/guide/topics/manifest/activity-element#exported for details.
/.../src/main/AndroidManifest.xml Error:
	android:exported needs to be explicitly specified for <service>. Apps targeting Android 12 and higher are required to specify an explicit value for `android:exported` when the corresponding component has an intent filter defined. 
	See https://developer.android.com/guide/topics/manifest/activity-element#exported for details.

Android 12 适配升级小结-鸿蒙开发者社区

A7: 在所有 Module 中找到使用 Intent 过滤器的 Service 并按业务需求添加对应的 exported 属性

<service
    android:name="com.xxx.app.push.OPushMessageService"
    android:permission="com.coloros.mcs.permission.SEND_MCS_MESSAGE"
    android:exported="true">
  <intent-filter>
    <action android:name="com.coloros.mcs.action.RECEIVE_MCS_MESSAGE" />
  </intent-filter>
</service>

Q8: 使用 Intent 过滤器的 Receiver 需设置 exported 属性

/.../src/main/AndroidManifest.xml Error:
	android:exported needs to be explicitly specified for <receiver>. Apps targeting Android 12 and higher are required to specify an explicit value for `android:exported` when the corresponding component has an intent filter defined. See https://developer.android.com/guide/topics/manifest/activity-element#exported for details.
/.../src/main/AndroidManifest.xml Error:
	android:exported needs to be explicitly specified for <receiver>. Apps targeting Android 12 and higher are required to specify an explicit value for `android:exported` when the corresponding component has an intent filter defined. See https://developer.android.com/guide/topics/manifest/activity-element#exported for details.

Android 12 适配升级小结-鸿蒙开发者社区

A8: 在所有 Module 中找到使用 Intent 过滤器的 Receiver 并按业务需求添加对应的 exported 属性

<receiver android:name="com.xxx.app.SystemReceiver"
    android:exported="false">
  <intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED" />
    <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
    <action android:name="android.intent.action.USER_PRESENT" />
  </intent-filter>
  <intent-filter>
    <action android:name="android.intent.action.PACKAGE_REMOVED" />

    <data android:scheme="package" />
  </intent-filter>
</receiver>

Q9: 使用 Intent 过滤器的 Activity 需设置 exported 属性

/.../src/main/AndroidManifest.xml Error:
	android:exported needs to be explicitly specified for <activity>. Apps targeting Android 12 and higher are required to specify an explicit value for `android:exported` when the corresponding component has an intent filter defined. See https://developer.android.com/guide/topics/manifest/activity-element#exported for details.
/.../src/main/AndroidManifest.xml Error:
	android:exported needs to be explicitly specified for <activity>. Apps targeting Android 12 and higher are required to specify an explicit value for `android:exported` when the corresponding component has an intent filter defined. See https://developer.android.com/guide/topics/manifest/activity-element#exported for details.

Android 12 适配升级小结-鸿蒙开发者社区

A9: 在所有 Module 中找到使用 Intent 过滤器的 Activity 并按业务需求添加对应的 exported 属性

<activity
    android:name=".xxx.app.TestActivity"
    android:exported="false"
    android:theme="@style/Theme.notAnimation">
  <intent-filter>
    <action android:name="com.sogou.novel.reader.setting.clean" />

    <category android:name="android.intent.category.DEFAULT" />
  </intent-filter>
</activity>

Q10: PendingIntent 需声明可变性

    在 Android 12 中创建 PendingIntent 的时候,需要显示的声明是否可变,请分别使用 PendingIntent.FLAG_MUTABLEPendingIntent.FLAG_IMMUTABLE 标志,如果您的应用试图在不设置任何可变标志的情况下创建 PendingIntent 对象,系统会抛出 IllegalArgumentException 异常;

PACKAGE_NAME: Targeting S+ (version 10000 and above) requires that one of \
FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.

Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if \
some functionality depends on the PendingIntent being mutable, e.g. if \
it needs to be used with inline replies or bubbles.

A10: 根据业务设置 PendingIntent 可变性

    PendingIntent 是一个可以给另一个应用程序使用的 IntentPendingIntent 接收待处理意图的应用程序可以使用与产生待处理意图的应用程序相同的权限和身份执行待处理意图中指定的操作;

    在 Adnroid 12 之前,默认创建一个 PendingIntent 它是可变的,因此其他恶意应用程序可能会拦截,重定向或修改此 Intent

PendingIntent pendingIntent = PendingIntent.getBroadcast(getContext().getApplicationContext(), type, intent, PendingIntent.FLAG_IMMUTABLE);
            

W11: 避免使用 flatDirs 提醒

    Gradle 升级之后,提示避免使用 flatDir 提醒,因该方式不支持任何元数据方式;

Using flatDir should be avoided because it doesn't support any meta-data formats.
Using flatDir2 should be avoided because it doesn't support any meta-data formats.

Android 12 适配升级小结-鸿蒙开发者社区

A11: 使用 jniLibs.srcDirs 方式引入 libs 库

    Gradle 升级之后使用 jniLibs.srcDirs 方式替代 flatDirlibs 库引入,并更新 aar 引入方式;

当前版本

repositories {
    flatDir {
        dirs 'libs'
    }
}

implementation(name: 'test_name', ext: 'aar')

升级后版本

android {
    sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
        }
    }
}

implementation (files("libs/test_name.aar"))

W12: dexOptions 弃用提醒

DSL element 'dexOptions' is obsolete and should be removed.
It will be removed in version 8.0 of the Android Gradle plugin.
Using it has no effect, and the AndroidGradle plugin optimizes dexing automatically.

Android 12 适配升级小结-鸿蒙开发者社区

A12: Gradle 升级后 dexOptions 已弃用,删除即可

dexOptions {
    preDexLibraries = true
}

Tips:

    小菜在测试过程中,明明代码中所有涉及 intent-filter 过滤器的 Activity / Service / Receiver 都已经设置了 exported 属性,但依旧提示使用 Intent 过滤器的 XX 需设置 exported 属性;其原因在于引入了各类三方 SDK,在引入的各类三方库中可以存在对应的未设置 exported 属性的 Activity / Service / Receiver,单独设置处理一下即可;


    Android 12 的初步升级到此位置,还有很多特有的属性,小菜会在后续的适配中进行完善;如有错误,请多多指导!

阿策小和尚

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
2
收藏 1
回复
举报
回复
    相关推荐