Behavior changes: Apps targeting Android 12
文章推薦指數: 80 %
Warning: If an activity, service, or broadcast receiver uses intent filters and doesn't have an explicitly-declared value for android:exported , your app can't ...
Platform
AndroidStudio
GooglePlay
Jetpack
Kotlin
Docs
Games
English
BahasaIndonesia
Español–AméricaLatina
Português–Brasil
中文–简体
日本語
한국어
Signin
Platform
Overview
Releases
Devices
Wear
Technology
LargeScreens
MachineLearning
Privacy
Android5G
Platform
Overview
Releases
Devices
Wear
Technology
LargeScreens
MachineLearning
Privacy
Android5G
AndroidStudio
GooglePlay
Jetpack
Kotlin
Docs
Games
AllAndroidreleases
Android12
Home
Featuresandchangeslist
12Lfeaturedrop
Home12LfeaturesandchangesGet12LSetupthe12LSDKAPIdiffs-API31→API32
DeveloperSupportimages
Behaviorchanges
Behaviorchanges:allapps
Behaviorchanges:appstargetingAndroid12
Non-SDKrestrictions
Deprecations
NewfeaturesandAPIs
Overview
Widgetsimprovements
What'snewforenterprise
Getyourappsready
Migrateyourapps
GetAndroid12
SetuptheAndroid12SDK
Referenceupdates
APIdiffs-API30→API31
BroadcastIntents(API31)
CommonIntents(API31)
Compatibilityframeworkchanges
More
Dashboards
AndroidDevelopers
Platform
Releases
Behaviorchanges:AppstargetingAndroid12
Stayorganizedwithcollections
Saveandcategorizecontentbasedonyourpreferences.
Likeearlierreleases,Android 12includesbehaviorchangesthat
mayaffectyourapp.Thefollowingbehaviorchangesapplyexclusivelytoapps
thataretargetingAndroid 12orhigher.Ifyourappis
targetingAndroid12,youshouldmodifyyourapptosupportthesebehaviors
properly,whereapplicable.
Besuretoalsoreviewthelistofbehaviorchangesthataffectallapps
runningonAndroid 12.
Userexperience
Customnotifications
Android12changestheappearanceandbehavioroffullycustomnotifications.
Previously,customnotificationswereabletousetheentirenotificationarea
andprovidetheirownlayoutsandstyles.Thisresultedinanti-patternsthat
couldconfuseusersorcauselayoutcompatibilityissuesondifferentdevices.
ForappstargetingAndroid12,notificationswithcustomcontentviewswillno
longerusethefullnotificationarea;instead,thesystemappliesastandard
template.Thistemplateensuresthatcustomnotificationshavethesame
decorationasothernotificationsinallstates,suchasthenotification'sicon
andexpansionaffordances(inthecollapsedstate)andthenotification'sicon,
appname,andcollapseaffordance(intheexpansionstate).Thisbehavioris
nearlyidenticaltothebehaviorof
Notification.DecoratedCustomViewStyle.
Inthisway,Android12makesallnotificationsvisuallyconsistentandeasyto
scan,withadiscoverable,familiarnotificationexpansionforusers.
Thefollowingillustrationshowsacustomnotificationinthestandardtemplate:
Thefollowingexamplesshowhowcustomnotificationswouldrenderinacollapsed
andanexpandedstate:
ThechangeinAndroid12affectsappsthatdefinecustomsubclassesof
Notification.Style,orwhichuse
Notification.Builder’smethods
setCustomContentView(RemoteViews),
setCustomBigContentView(RemoteViews),
andsetCustomHeadsUpContentView(RemoteViews).
Ifyourappisusingfullycustomnotifications,werecommendtestingwiththe
newtemplateassoonaspossible.
Enablethecustomnotificationschange:
Changeyourapp'stargetSdkVersiontoStoenablethenewbehavior.
Recompile.
InstallyourapponadeviceoremulatorrunningAndroid12.
Testallnotificationsthatusecustomviews,ensuringtheylookasyou
expectintheshade.Whiletesting,taketheseconsiderationsintoaccount
andmakethenecessaryadjustments:
Thedimensionsofcustomviewshavechanged.Ingeneral,theheight
affordedtocustomnotificationsislessthanbefore.Inthecollapsed
state,themaximumheightofthecustomcontenthasdecreasedfrom106dp
to48dp.Also,thereislesshorizontalspace.
AllnotificationsareexpandableforappstargetingAndroid12.
Typically,thismeansifyou'reusingsetCustomContentView,
you'llalsowanttousesetBigCustomContentView
tomakesurecollapsedandexpandedstatesareconsistent.
Tomakesurethatthe"HeadsUp"statelooksasyouexpect,don’tforget
toraisetheimportanceofthenotificationchannelto"HIGH"(Popson
screen).
AndroidAppLinksverificationchanges
OnappsthattargetAndroid 12orhigher,thesystemmakes
severalchangestohowAndroidAppLinksare
verified.These
changesimprovethereliabilityoftheapp-linkingexperienceandprovidemore
controltoappdevelopersandendusers.
IfyourelyonAndroidAppLinkverificationtoopenweblinksinyourapp,
checkthatyouusethecorrectformatwhenyouaddintent
filtersfor
AndroidAppLinkverification.Inparticular,makesurethattheseintent
filtersincludetheBROWSABLEcategoryandsupportthehttpsscheme.
Youcanalsomanually
verifyyour
app'slinkstotestthereliabilityofyourdeclarations.
Picture-in-picturebehaviorimprovements
Android12introducesbehaviorimprovementsforpicture-in-picture(PiP)
mode.SeePicture-in-pictureimprovements
formoreinformation.
Toastredesign
InAndroid12,thetoastviewhasbeen
redesigned.Toastsarenowlimitedtotwolinesoftextandshowtheapplication
iconnexttothetext.
SeeToastsoverviewforfurtherdetails.
Securityandprivacy
Approximatelocation
OndevicesthatrunAndroid 12orhigher,userscanrequest
approximatelocation
accuracyforyourapp.
Note:OnsomereleasesofAndroid 12,thischangealwaysaffects
yourapp,
regardlessoftargetSDKversion.
ModernSameSitecookiesinWebView
Android’sWebViewcomponentisbasedonChromium,
theopensourceprojectthatpowersGoogle’sChromebrowser.Chromiumintroduced
changestothehandlingofthird-partycookiestoprovidemoresecurityand
privacyandofferusersmoretransparencyandcontrol.StartinginAndroid12,
thesechangesarealsoincludedinWebViewwhenappstarget
Android 12(APIlevel 31)orhigher.
TheSameSiteattributeofacookiecontrolswhetheritcanbesentwithany
requests,oronlywithsame-siterequests.Thefollowingprivacy-protecting
changesimprovethedefaulthandlingofthird-partycookiesandhelpprotect
againstunintendedcross-sitesharing:
CookieswithoutaSameSiteattributearetreatedasSameSite=Lax.
CookieswithSameSite=NonemustalsospecifytheSecureattribute,meaning
theyrequireasecurecontextandshouldbesentoverHTTPS.
LinksbetweenHTTPandHTTPSversionsofasitearenowtreatedascross-site
requests,socookiesarenotsentunlesstheyareappropriatelymarkedas
SameSite=None;Secure.
Fordevelopers,thegeneralguidanceistoidentifythecross-sitecookie
dependenciesinyourcriticaluserflowsandensurethattheSameSite
attributeisexplicitlysetwiththeappropriatevalueswhereneeded.Youmust
explicitlyspecifythecookiesthatareallowedtoworkacrosswebsitesor
acrosssame-sitenavigationsthatmovefromHTTPtoHTTPS.
Forcompleteguidanceforwebdevelopersonthesechanges,seeSameSiteCookies
ExplainedandSchemeful
SameSite.
TestSameSitebehaviorsinyourapp
IfyourappusesWebView,orifyoumanageawebsiteorservicethatuses
cookies,werecommendtestingyourflowsonAndroid 12WebView.
Ifyoufindissues,youmightneedtoupdateyourcookiestosupportthenew
SameSitebehaviors.
Watchforissuesinloginsandembeddedcontent,aswellassign-inflows,
purchasing,andotherauthenticationflowswheretheuserstartsonaninsecure
pageandtransitionstoasecurepage.
TotestanappwithWebView,youmustenablethenewSameSitebehaviorsforthe
appthatyouwanttotestbycompletingeitherofthefollowingsteps:
ManuallyenableSameSitebehaviorsonthetestdevicebytogglingtheUIflag
webview-enable-modern-cookie-same-site
intheWebViewdevtools.
ThisapproachletsyoutestonanydevicerunningAndroid5.0(APIlevel21)
orhigher—includingAndroid 12—andWebViewversion89.0.4385.0
orhigher.
CompileyourapptotargetAndroid 12(APIlevel 31)bytargetSdkVersion.
Ifyouusethisapproach,youmustuseadevicethatruns
Android 12.
ForinformationonremotedebuggingforWebViewonAndroid,seeGetStarted
withRemoteDebuggingAndroidDevices.
Otherresources
FormoreinformationabouttheSameSitemodernbehaviorsandrollouttoChrome
andWebView,visittheChromiumSameSiteUpdates
page.IfyoufindabuginWebView
orChromium,youcanreportitinthepublicChromiumissue
tracker.
Motionsensorsarerate-limited
Toprotectpotentiallysensitiveinformationaboutusers,ifyourapptargets
Android 12orhigher,thesystemplacesalimitontherefresh
rateofdatafromcertainmotionsensorsandpositionsensors.
Learnmoreaboutsensor
rate-limiting.
Apphibernation
Android 12expandsuponthepermissionsauto-reset
behavior
thatwasintroducedinAndroid 11(APIlevel 30).Ifyourapptargets
Android 12andtheuserdoesn'tinteractwithyourappforafew
months,thesystemauto-resetsanygrantedpermissionsandplacesyourappina
hibernationstate.
Learnmoreintheguideaboutapp
hibernation.
Attributiondeclarationindataaccessauditing
ThedataaccessauditingAPI,introducedinAndroid 11(APIlevel 30),allowsyou
tocreateattribution
tagsbasedonyour
app'susecases.Thesetagsmakeiteasierforyoutodeterminewhichpartof
yourappperformsaspecifictypeofdataaccess.
IfyourapptargetsAndroid 12orhigher,youmustdeclarethese
attributiontagsin
yourapp'smanifestfile.
ADBbackuprestriction
Tohelpprotectprivateappdata,Android12changesthedefaultbehaviorofthe
adbbackupcommand.ForappsthattargetAndroid 12(APIlevel 31)orhigher,
whenauserrunstheadbbackupcommand,appdataisexcludedfromanyother
systemdatathatisexportedfromthedevice.
Ifyourtestingordevelopmentworkflowsrelyonappdatausingadbbackup,
youcannowoptintoexportingyourapp'sdatabysetting
android:debuggable
totrueinyourapp'smanifestfile.
Caution:Tohelpprotectyourapp'sdata,remembertosetandroid:debuggable
tofalsebeforereleasingyourapp.
Safercomponentexporting
IfyourapptargetsAndroid 12orhigherandcontains
activities,
services,orbroadcast
receiversthatuseintent
filters,youmustexplicitly
declarethe
android:exportedattribute
fortheseappcomponents.
Warning:Ifanactivity,service,orbroadcastreceiverusesintentfiltersand
doesn'thaveanexplicitly-declaredvalueforandroid:exported,yourappcan't
beinstalledonadevicethatrunsAndroid 12orhigher.
Iftheappcomponentincludesthe
LAUNCHERcategory,set
android:exportedtotrue.Inmostothercases,setandroid:exportedto
false.
Thefollowingcodesnippetshowsanexampleofaservicethatcontainsanintent
filterwhoseandroid:exportedattributeissettofalse:
延伸文章資訊
- 1Android 12 行为变更,对应用产生的影响 - 知乎专栏
在Android 12 中包含 <intent-filter> 的 activity 、 service 或 receiver 必须为这些应用组件显示声明 android:exported 属性...
- 2Question - Unity android 12 with android:exported attribute issue
I would like to build the game in android 12. I know I need to add the android:exported="true" in...
- 3Android:exported needs to be explicitly specified for <service ...
The reason is because some of the dependency libraries that you're using have elements which do n...
- 4You uploaded an APK or Android App Bundle which has an ...
This file can't be installed on Android 12 or higher. See: developer.android.com/about/versions/1...
- 5Android 12 自动适配exported 深入解析避坑 - 掘金
众所周知,从Android 12开始,也就是使用了TargetSDK 31 之后,四大组件如果使用了 intent-filter, 但是没显性质配置 exported ,App 将会无法安装。