Android(安卓)Studio官方文档之添加URL和App索引支持
Android Studio官方文档之添加URL和App索引支持
本文由nyk翻译,jkYishon审校。
Android Studio可以帮你在App中添加对URLs,app索引,搜索功能的支持。这些功能可以帮你推动更多的流量到你的App、发现App中最被常用的内容,使用户更容易发现已安装App中内容,并以此来吸引新用户。
典型的工作流程
用Android Studio添加对URL支持,App索引,搜索功能到你的App中:
- 添加Intent过滤器和处理传入Intent的代码
- 关联网站和App
- 添加App Indexing API代码
Intent过滤器和App Indexing API是实现URL支持和索引的方式,但也有其他的可行方法。更过信息请查看替代Android索引方法。
支持URLs的Intent过滤器
Android Studio可以在清单中创建基本的Intent过滤器,你可以在其中定义URLs。在Activity中写Java代码来操作Intent。这个实现可以让用户通过点击一个URL来直接打开指定的App。用户可以在浏览器的google.com、Google Search App、Google Now On Tap中看到这个URLs。
和URLs关联的网站
在为你的应用设置了URL支持后,你可以使用Google Search Console和Google Play Developer Console把你的网站和App关联。在这之后,Google会通过你Intent过滤器中的URLs索引到你的App,并且开始将它们包含到搜索结果中。另外,你可以选择性的将App的内容屏蔽在Google搜索之外。在你将网站和App关联后,像Now On Tap的功能和提升搜索结果显示(相包含你的App图标)就会可用。
在Android 6.0(API 23)及更高版本中,你可以为URL添加默认的处理器和验证,以替代把你的网站和App相关联。
Chrome显示的google.com服务的搜索结果的URL对所有登录和未登录的用户都是可以访问的。对于Google Search App,用户必须登录才能看到搜索结果的URL。
添加索引API到Activity中
接下来,如果你想你的App支持搜索功能,比如自动填充,你需要添加App Indexing API代码到你Activity中。Android Studio可以在Activity添加构架代码,你可以优化它以支持App索引。App Indexing API可以能使你App索引,即使在GoogleBot无法得到你App内容的情况下。
测试URL支持和App Indexing API
Android Studio可以帮你测试以下功能:
- URL支持测试帮你验证指定的URL是否可以启动一个App
- logcat显示器帮你测试Activity中的App Indexing API调用情况
- Android Lint工具可以对URL支持和App Indexing API的一些问题发出警告。这些警告和错误显示在代码编辑器和Lint inspection结果中
- Google App Indexing测试来检查Google能否通过爬虫你的App页面或App Indexing API索引到你的URL
添加对URL支持和Google搜索的Intent过滤器
使用Android Studio添加一个定义URL的Intent过滤器。
- 在工程窗口的Android视图中,双击打开AndroidManifest.xml文件
按以下方式中的一种插入Intent过滤器:
- 点击activity元素的左侧,会出现灯泡,点击灯泡选择Create URL
- 右击元素activity,选择Generate>URL
- 把你的光标放在一个Activity中,选择Code > Generate>URL
代码编辑器使用Intention Action和生成代码机制来添加主要代码。
代码编辑器会生成与以下代码相似的Intent过滤器
修改
添加App Indexing API主代码到Activity
在添加了URL支持到你的App之后,你需要添加App Indexing API代码到Activity中以支持附加的搜索功能
添加App Indexing API到Activity中:
- 在Project窗口的Android视图中,双击Activity文件进入编辑界面
通过以下方式之一插入框架代码:
- 在Activity定义中,单击Java代码,出现灯泡图标,点击灯泡选择Insert App Indexing API Code
- 在Activity编辑界面,右击选择Generate > App Indexing API Code
- 光标停留在Activity编辑界面,选择Code > Generate > App Indexing API Code
代码编辑器使用Intention Action和生成代码机制来添加架构代码。
如果你看不到App Indexing API Code的菜单选项,确保你的光标在Activity内,且按App Indexing API的方法检查你的代码。代码编辑器会依据以下情形之一来插入架构代码到Activity中:
- 如果Activity中没有onStart()方法,或者onStart()没有包含AppIndexApi.start()或AppIndexApi.view()调用
- 如果Activity中没有onStop()方法,或者onStop()没有包含AppIndexApi.end()或AppIndexApi.end()调用
代码编辑器插入的Java代码与以下代码相似:
/** * ATTENTION: This was auto-generated to implement the App Indexing API. * See https://g.co/AppIndexing/AndroidStudio for more information. */ private GoogleApiClient client; // ATTENTION: This was auto-generated to implement the App Indexing API. // See https://g.co/AppIndexing/AndroidStudio for more information. client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); @Override public void onStart() { super.onStart(); // ATTENTION: This was auto-generated to implement the App Indexing API. // See https://g.co/AppIndexing/AndroidStudio for more information. client.connect(); Action viewAction = Action.newAction( Action.TYPE_VIEW, // TODO: choose an action type. "Main Page", // TODO: Define a title for the content shown. // TODO: If you have web page content that matches // this app activity's content, // make sure this auto-generated web page URL is correct. // Otherwise, set the URL to null. Uri.parse("http://www.example.com/gizmos"), // TODO: Make sure this auto-generated app URL is correct. Uri.parse("android-app://com.example/http/www.example.com/gizmos") ); AppIndex.AppIndexApi.start(client, viewAction); } @Override public void onStop() { super.onStop(); // ATTENTION: This was auto-generated to implement the App Indexing API. // See https://g.co/AppIndexing/AndroidStudio for more information. Action viewAction = Action.newAction( Action.TYPE_VIEW, // TODO: choose an action type. "Main Page", // TODO: Define a title for the content shown. // TODO: If you have web page content that matches // this app activity's content, // make sure this auto-generated web page URL is correct. // Otherwise, set the URL to null. Uri.parse("http://www.example.com/gizmos"), // TODO: Make sure this auto-generated app URL is correct. Uri.parse("android-app://com.example/http/www.example.com/gizmos") ); AppIndex.AppIndexApi.end(client, viewAction); client.disconnect(); } }
关于App Indexing API方法的更多信息,请查看Android API for App Indexing,关于Action类型的更多信息,请查看Action类常量总结。
如果你的App没有准备配置Google Play服务的App Indexing API,代码编辑器也会修改并包含在build.gradle和AndroidManifest.xml文件。如果你的App依赖Google Play服务,且版本低于8.1,那你的应该升级到8.1版本。更多信息请查看Google Play服务和设置Google Play服务。
按需优化架构代码
请仔细查看注释会帮你找到需要修改的地方,例如设置标题和URL,更多信息请查看添加App Indexing API。
用logcat显示器验证你的App Indexing代码是否运行。
为了运行和调试App Indexing API代码,你可以使用Android Studio以下功能:
- 检查logcat的监控信息。
- 启用Android Lint以下功能:Missing support for Google App Indexing API
- 测试Google App Indexing
另外,你可以在Google Search Console中预览你的APK
测试URL
当你用Android Studio运行App时,你可以指定一个URL来启动且测试。
要启动于Android Studio中的URL:
- 在Android Studio中打开Android视图
- 选中一个工程,选run > Edit Configurations
- 在Run/Debug Configurations对话框中,在Android Application下面,选择你要运行的应用
- 选择General标签
- 在Launch中,选择URL
- 在URL中,点击…从列表中选择一个URL
或者你输入一个URL,例如,http://example.com/gizmos - 点击OK
- 选择Run > Run app 或 Debug app
如果Select Deployment Target对话框出现,选择一个连接设备或模拟设备
如果链接是成功的,App会在设备或模拟设备启动,并且在指定的activity中显示App,否则的话,错误信息会显示在Run窗口
关于设置App的运行信息,请查看编译运行App
当App运行时,你可以查看App Indexing API的日志
在logcat中查看App Indexing API的信息
logcat Monitor可以显示应用的索引的日志信息以确定你应用的App Indexing API代码是否推送正确的数据到云端。例如,你可以查验App的标题和URL。logcat Monitor是Android Monitor的一部分。
在logcat Monitor中查看App Indexing API日志信息:
- 在Android Studio中运行你的App,它会启动URL
- 显示Android Monitor点击logcat标签
- 设置log的级别为Verbose
- 在过滤器菜单中,选择No Filters
用字符串“appindex”在log中搜索
App的索引日志信息就会显现,如果没有,请检查以下项目:
- 设备或模拟器上是否安装了 Google Play服务?可以通过检查设备的设置得知
- 设备或模拟器上的Google Play服务的版本是否低于build.gradle指定的版本?如果是的话,那可能版本已经太,必须升级到更高的版本。
更多信息请查看下载Google Play服务和设置Google Play服务。
访问App界面启动App Indexing API调用
配置Lint
你可以使用Android Studio内建的Lint工具来检查定义在清单文件中的URLs和已在Activity中实现的App Indexing API代码是否有效。
你可以通过两种方式看到URL和App Indexing的警告和错误信息。
- 代码编辑器中弹出的文本。当Lint发现问题时,他会用黄色高亮有问题代码,或者在更严重代码下标红
- 选择Analyze > Inspect Code ,弹出的Lint的Inspection Results窗口中
用默认的Lint来检查URLs和the App Indexing API:
- 在Android Studio中打开你的Android视图
- 选择File > Other Settings > Default Settings
- 在Default Preferences对话框中,选择Editor > Inspections
- 在Profile中,选择Default或Project Default来决定该设置的适用范围是整个Android Studio或本工程
展开Android Lint的目录,并根据需要更改Lint的设置:
- Missing support for Google App Indexing - 如果应用没有执行被Google Search使用的URLs会报告警告,默认是选中的
- Missing support for Google App Indexing API:当应用没有实现App Indexing API时会报告。默认不选中
- URL not supported by app for Google App Indexing:报告清单文件中的URL错误,默认选中
例如,以下是第一种设置中的Lint警告:
点击OK
执行Inspection Results窗口中一系列的Lint检查:
- 在Android Studio中打开工程的Android视图,选择你要测试的工程的一部分
- 选择Analyze > Inspect Code
在Specify Inspection Scope对话框中,设置inspection scope和profile
inspection scope指定了你想分析的文件,profile指定你想执行的Lint检查
如果你想改Lint的设置,点击Inspections对话框中的…。在Inspections对话框中,选择Manage定义一个新的配置文件,指定Lint设置,点击 OK
在Inspections对话框中,你可以搜索“app indexing”来找到URL和App Indexing API的Lint检查。注意在Inspections 对话框中改变配置的Lint设置不会改变默认设置,就像之前描述的那样。但是,它会改变配置文件的设置。
点击OK
结果显示在Inspection Results窗口中
执行Google App Indexing测试
你可以用Google App Indexing Test来检查Google能否通过爬虫你的应用页面或使用App Indexing API来索引到URL。Google可以索引到你的URL如果你的应用至少支持以下情形的一种:
- URL指定的页面可以由Googlebot打开或爬虫
- 你的应用通过 App Indexing API发送了正确的数据
指定Google App Indexing Test:
- 在应用中添加URL或App Indexing API
- 在Android Studio中打开你的应用,选择 Tools > Android > Google App Indexing Test
- 在Google App Indexing Test 对话框中,设置Module, URL和Language
- 如果你看到需要登录Google Cloud Platform帐号的信息的话,你需要登录
在Google App Indexing Test对话框中,点击OK
Android Studio会编译APK并且开始测试,需要几分钟才能完成,结果会显示在代码编辑器的新标签页中。
如果应用预览在屏幕右边,显示的和你测试的URL相关的界面,那么Googlebot就能找到该URL。
修改测试发现的问题,并且按需要重复测试
以下是一些常见的错误和警告提示:
警告或错误 | 描述 |
Error: Google cannot index this page. | 你的应用不能被Googlebot爬虫或无法使用App Indexing API,所以Google无法索引到的你的App |
Warning: The App URL you sent by using the App Indexing API doesn’t match the URL opened. | 当调用App Indexing API时,在App中指定的URL必须和打开的URL相匹配 |
Warning: Google cannot index this page using the App Indexing API because the title is empty. | 当调用 App Indexing API时,标题不能为空 |
Warning: Google can index this page using Googlebot crawling but identified blocked resources. | App参考了其他资源,其中一些被阻塞或暂时不可用。如果不是关键资源,可能无关紧要。检查右边预览到的内容是否正确显示。要解决此问题,要确保资源不被 robots.txt 拦截 |
Warning: Google cannot index this page using the App Indexing API. | 你的应用无法使用App Indexing API,推荐添加 App Indexing API到你的App中 |
更多相关文章
- 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
- Android(安卓)ListView实现单击item出现删除按钮以及滑动出现删
- Android快速实现地图功能(不仅快!而且小!)
- Android(安卓)Bottom Sheet详解
- Android应用反编译
- Android(安卓)API : SMS短信服务处理
- Android(安卓)开发架构——MVP(个人认为最适合新人了解MVP的文章)
- Android(安卓)最简单的导航栏实现
- Android的代码规范及阿里巴巴最新Java开发手册福利