官方解决案例(节选)在这里插入图片描述

下面详细分析

一、 问题

使用Android R,在编写使用AIDL进行跨进程通信连接服务过程时,使用远端服务时报错。
在这里插入图片描述

2020-10-28 18:26:21.721 19025-19025/com.practice.ipcclient
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.practice.ipcclient, PID: 19025
java.lang.NullPointerException: Attempt to invoke interface method ‘void
com.practice.ipcserver.IStudentManager.addStudent(com.practice.ipcserver.entity.Student)’
on a null object reference

这个空对象让人摸不着头脑,猜测

  1. intent属性设置错误,即包名类名Action等信息。反复修改双方约定的对应属性仍无法解决
  2. 服务是否连接成功的影响。结果发现bindService()返回false,说明但是没有连接上服务,但是不会报错,朋友帮助跟踪日志才知道是新版本的功能限制(没点经验还真不知道AppsFilter: interaction: PackageSetting ... BLOCKED这个是错误)

二、分析

  • build.gradle:配置Android R最新版本
android {
    compileSdkVersion 30
    buildToolsVersion '30.0.2'

    defaultConfig {
        applicationId "com.practice.ipcclient"
        minSdkVersion 30
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"
		.....
    }
    .....
}
  • 日志:adb logcat -b all -vtime打印

10-28 15:57:53.922 I/AppsFilter( 1089): interaction: PackageSetting{1a80770 com.practice.ipcclient/10245} -> PackageSetting{d264a04 com.practice.ipcserver/10247} BLOCKED
10-28 15:57:53.922 W/ActivityManager( 1089): Unable to start service Intent { act=com.practice.ipcserver.action cmp=com.practice.ipcserver/.StudentManagerService } U=0: not found
10-28 15:57:53.922 I/MainActivity( 4674): [onClick]bind stu_service end false,intent info:ComponentInfo{com.practice.ipcserver/com.practice.ipcserver.StudentManagerService}

三、 解决

(一)最简单:降低SDK版本

  • 修改build.gradle,降低SDK版本为Android 29

(二)修改软件可见性

在AndroidManifest.xml中添加queries标签声明需要用到的应用包名(还可以通过intent或者provider——参考1)

  • 在跨进程调用方声明使用的对方即可,无需双方声明。
<!--IPCServer-->
<queries>
    <package android:name="com.practice.ipcclient" />
</queries>

<!--IPCClient-->
<queries>
    <package android:name="com.practice.ipcserver" />
</queries>

问题
  1. AndroidManifest.xml中无法使用queries标签。
  • 本质gradle编译插件太低级
  • AndroidStudio4.1之前还没有支持这个标签(参考2)

在这里插入图片描述

>  Task :ipcclientpractice:processDebugManifest FAILED ipcclientpractice\src\main\AndroidManifest.xml:5:9-58
> Error: 	Missing 'package' key attribute on element package at
> AndroidManifest.xml:5:9-58
> \ipcclientpractice\src\main\AndroidManifest.xml
> Error: 	Validation failed, exiting
> 
> See [http://g.co/androidstudio/manifest-merger](http://g.co/androidstudio/manifest-merger) for more information
> about the manifest merger.
> 
> 
> FAILURE: Build failed with an exception.
> 
> * What went wrong: Execution failed for task ':ipcclientpractice:processDebugManifest'.
>  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.
> 
> * Get more help at [https://help.gradle.org](https://help.gradle.org)
> 
> BUILD FAILED in 1s 9 actionable tasks: 1 executed, 8 up-to-date

在这里插入图片描述

  • 解决:升级AS到4.1+,并更新build gradle插件(缺一不可),否则报错如下

This version of the Android Support plugin for IntelliJ IDEA (or Android Studio) cannot open this project, please retry with version 4.1 or newer.

在这里插入图片描述
在这里插入图片描述

参考
  1. 官方文档:管理软件包可见性https://developer.android.google.cn/training/basics/intents/package-visibility
    (表明Android11对应用可见性的默认约定)

    If your app targets Android 11 (API level 30) or higher, the system makes some apps visible to your app automatically, but it hides other apps by default.
    如果您的应用定位到Android 11(API级别30)或更高版本,则系统会自动使某些应用对您的应用可见,但默认情况下会隐藏其他应用。

    If your app targets Android 11 (API level 30) or higher and needs to
    interact with apps other than the ones that are visible automatically,
    add the queries element in your app’s manifest file. Within the
    queries element, specify the other apps by package name, by intent
    signature, or by provider authority
    如果您的应用定位到Android 11(API级别30)或更高版本,并且需要与除自动显示的应用以外的其他应用进行交互,请queries在应用的清单文件中添加元素。在queries元素内,按照包名,intent签名或provider授权来指定其他应用

    【日志过滤工具】在Android Studio中开发测试应用程序或可调试应用程序,则将为您启用此功能
    在这里插入图片描述

    PS:developer.android.com无法访问时,可以用developer.android.google.cn替换

  2. Android Studio 4.1 的一些更新支持https://android-developers.googleblog.com/2020/10/android-studio-41.html(解决query标签在Android Studio不可用问题)

Logo

鸿蒙生态一站式服务平台。

更多推荐