LoginSignup
22
13

More than 3 years have passed since last update.

Android 11のPackage visibilityへの対応

Last updated at Posted at 2020-09-11

Android11からはPackageManagerを使ってqueryIntentActivitiesなどで他のアプリがインストールされているか調べるのに制限がかかります。
targetSdkVersionを30にすると、デフォルトでは一部のシステムアプリしか結果が返ってきません、どのようなアプリを調べるのかの宣言が必要になります。

要するに以下の内容です。
https://developer.android.com/preview/privacy/package-visibility?hl=ja

宣言方法は2種類あります。
以下のように、調べたいパッケージ名を指定するか、

AndroidManifest.xml
<manifest package="com.example.game">
    <queries>
        <package android:name="com.example.store" />
        <package android:name="com.example.services" />
    </queries>
    ...
</manifest>

以下のようにIntentFilterで指定する方法です。

AndroidManifest.xml
<manifest package="com.example.game">
    <queries>
        <intent>
            <action android:name="android.intent.action.SEND" />
            <data android:mimeType="image/jpeg" />
        </intent>
    </queries>
    ...
</manifest>

ここで指定するのは、アプリの中でどのようなクエリで調べるかではありません。
ここで指定した条件に合致するアプリが、アプリからのクエリで調べられるようになる、というだけです。

例えば、マニフェストの条件に該当するアプリが A,B で、アプリ内のクエリに合致する端末内アプリが A,C だったとすると、アプリへはAだけが結果として返るようになる。という動作になります。

CustomTabsを使う場合の指定

(Chrome限定ではなく)CustomTabsを使う場合、android.support.customtabs.action.CustomTabsServiceというActionに反応するサービスを持つアプリ、かつ、ブラウザであるパッケージを調べます。
なので、安直に設定するなら以下のようになるでしょう。

AndroidManifest.xml
<queries>
    <intent>
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.BROWSABLE"/>
        <data android:scheme="http"/>
    </intent>
    <intent>
        <action android:name="android.support.customtabs.action.CustomTabsService"/>
    </intent>
</queries>

しかし、android.support.customtabs.action.CustomTabsServiceというActionのクエリだけ書いておけば、ブラウザを調べたときにandroid.support.customtabs.action.CustomTabsServiceに反応し、かつ、ブラウザのアプリだけが返却されるようになるので以下の条件だけ書けばよいことになります。

AndroidManifest.xml
<queries>
    <intent>
        <action android:name="android.support.customtabs.action.CustomTabsService"/>
    </intent>
</queries>

ランチャーから起動可能なアプリの指定

以下のように書けば、ランチャーから起動可能なアプリすべてを調べられるようになります。
特殊なアプリを除けば、ランチャーから起動可能ですので以下の記述でAndroid11未満と同様にほぼすべてのアプリを調べられることになります。

AndroidManifest.xml
<queries>
    <intent>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent>
</queries>

SpeechRecognizerを使う

どうやら、SpeechRecognizerもPackage Visivilityの影響を受けるようです。

AndroidManifest.xml
<queries>
    <intent>
        <action android:name="android.speech.action.RECOGNIZE_SPEECH"/>
    </intent>
</queries>

パッケージを調べるという操作は、そんなにやることはなかろうと思いつつも、CustomTabsぐらいは使っているでしょうし、SDK内部でパッケージを調べている場合も影響を受けるため、targetSdkVersionを30にして動作がおかしい場合はこいつをチェックするようにしましょう。

22
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
22
13