开发者控制台

如何使用Amazon Q开发者版提升Fire应用开发的生产力

Share:
Fire tablet How to Fire TV
Blog_Header_Post_Img

一段时间以来,AI一直是开发者的热门话题。但无论您的技能水平如何,该如何在实际运用中用它来帮助您创建应用?不妨了解一下Amazon Q开发者版。

Amazon Q是我们现已推出的最激动人心的亚马逊开发者工具之一。它是一个亚马逊AI编码助手,可以集成到最流行的集成开发环境 (IDE) 中(包括Android Studio),甚至可以直接在命令行界面 (CLI) 上使用。它旨在使用上下文感知建议来帮助您高效地编写、测试和理解代码。它可帮助您:

  • 生成代码
  • 编写测试
  • 审查和重构
  • 集成APIs
  • ... 等等

由于Q接受过亚马逊和开源代码的训练,因此对于为Fire平板电脑和Fire TV构建应用的团队来说尤其方便。无论您是刚接触Fire TV的React Native开发者,还是希望提高生产力的资深Fire开发者,Q都可以帮助您减少障碍,让您能够更加专注于构建。

下面我们来看看Q能做些什么,以及如何以最佳效果使用它吧。

在Android Studio中开始使用Q

Q可以帮助您大幅提升Android Studio的生产力水平。开始使用Q,请下载并安装最新版本的Android Studio。在本指南中,我们将使用 Meerkat Feature Drop (2024.3.2)
 

安装插件

打开Android Studio并导航至File(文件)➤ Settings(设置)(在Mac上为Android Studio ➤ Settings)。找到Plugins(插件)设置,然后单击顶部的Marketplace(市场)。搜索“Amazon Q”。单击Install(安装)。

Android Studio panel screenshot

安装插件后,您可能需要重新启动Android Studio。现在,Amazon Q工具菜单将位于应用的右下角。

单击它,然后选择Get Started(开始使用)。

Android Studio panel screenshot

插件将为您显示经由AWS Builder ID进行连接的选项。单击Connect(连接)。

Android Studio panel screenshot

您的浏览器将打开,系统将要求您用自己的AWS Builder ID登录以进行身份验证,或者如果您没有个人资料,则要求您创建一个新的个人资料。通过身份验证后,您将在Android Studio中拥有Q的完整访问权限。

现在,Amazon Q工具菜单将显示以下内容:

Android Studio panel screenshot

在Android Studio中启用聊天面板

聊天面板是Android Studio的Amazon Q插件的关键功能之一。要在Android Studio中启用它,您需要将启动运行时更改为支持JCEF的JetBrains Runtime JBR版本。

1. 导航至Help(帮助)➤ Find Action…(查找操作…)。

Android Studio panel screenshot

2. 搜索Choose Boot Java Runtime for the IDE(选择IDE的启动Java运行时)。

Android Studio panel screenshot

3. 您会注意到,当前的启动运行时是默认的JetBrains Runtime(JetBrains运行时)。从可用运行时列表中,选择一个标题为JetBrains Runtime JBR with JCEF的运行时。

Android Studio panel screenshot

4. 单击OK(确定)。​Android Studio将安装启动运行时,然后要求您重新启动IDE。

5.单击Restart now(马上重新启动)。

 

现在,当您单击Open Chat Panel(打开聊天面板)时,您将看到下图所示内容:

Android Studio panel screenshot

Amazon Q Android Studio现在应当可以完全正常地工作了!

实际使用示例

Q配备了一系列丰富的功能,旨在帮助您更智能、更快地编码,并让您高效地推进工作。下面来看看有关如何使用Amazon Q进行Fire应用开发的一些真实、实用的场景:

场景1:集成API

如果您对使用亚马逊适用于Fire OS应用开发的SDK还不太熟悉,您可以使用Q来帮助您快速入门,搭建功能脚手架,从而打下坚实的基础。

假设您正在构建一个Fire平板电脑应用,并且希望实现应用内购买,以便于订阅优质内容。.您将需要使用Appstore SDK IAP。借助Q,您可以从使用/dev命令开始,由此描述您需要什么。

Android Studio panel screenshot

Q将列出一组用于生成所需代码的步骤。

Android Studio panel screenshot

然后,它将列出它审查的所有文件,以及建议的新文件、更改和删除内容。

Android Studio panel screenshot

在列出所有提议的更改后,它会询问您是否准备好接受这些更改,或者您是否需要请求任何调整。

Android Studio panel screenshot

如果对Q确认继续操作,它将快速创建所有提议的文件,提供您可以用来开始使用SDK IAP的代码。

Android Studio panel screenshot

它甚至可以生成布局,帮助您在探索和学习IAP时学会解决用户界面/用户体验问题。

Android Studio panel screenshot

请不要忘记,这只是一个示例场景。如果您正在构建一个Fire TV应用,或者希望集成Login with Amazon而不是IAP,Q也可以在这些场景中提供帮助。

场景2:故障排除

Amazon Q不仅可以编写代码让您开始,还可以在您不知道如何继续或遭遇难题时,帮助您摆脱前进之路上的困境。由于Q接受过开源代码库以及与亚马逊属性和应用相关的代码的训练,因此它可以在Fire专用的SDK和API方面提供巨大帮助。

如果您遭遇错误或不知道该怎么办,可以向它提出问题,将它当做帮助您编写应用的良师益友。

在此场景中,假设您正在使用AWS Amplify来处理Fire TV应用开发的后端问题。

Android Studio panel screenshot

您已经使用Amplify为后端数据资源设置了配置,但您还需要为Java应用程序生成GraphQL客户端代码。Amplify (Gen2) CLI具有与此相关的一个命令,但您不知道具体的命令是什么。

可以询问Q,它可以根据您打开的文件和项目结构检测上下文,然后推荐用于生成客户端代码的命令。

Android Studio panel screenshot

场景3:比较库

也许您不确定需要将哪个库集成到自己的应用中。

例如,您正在构建一个Fire平板电脑应用,但不清楚Fire OS在支持Jetpack Compose与Android Leanback库方面的差异。Q可以提供有关这方面差异的详细解释:

Android Studio panel screenshot

它还能进一步提供最佳实践、指导和代码示例:

Android Studio panel screenshot

场景4:脚手架组件和用户界面布局文件

如果您在开发过程中受到用户界面问题的困扰,比如为布局和片段编写XML,可以通过让Q替您做繁重的工作来节省时间。

例如,假设您需要在应用中引入一个新的活动屏幕。您知道自己希望应用有怎样的行为和显示什么样的内容,但将其转换为正确的文件和代码可能是一项艰苦的工作。为此,您也可以使用/dev命令,借助Q来完成该工作。

Android Studio panel screenshot

Q将根据您的要求,概述助您生成所需代码的步骤。

Android Studio panel screenshot

Q能够执行的任务包括创建您需要的所有Activity类、片段和XML布局。

Android Studio panel screenshot

如果使用Amazon Q来构建这些组件和布局的代码脚手架,将帮助您避免重复的设置工作,让您在改进设计以满足需求时有一个良好的基础。

场景5:生成和调试单元测试

Q还可以在您使用/dev请求时生成单元测试代码。在下面的示例中,它为PurchasingListener实现生成了5个测试。

Android Studio panel screenshot

它甚至可以帮助您处理需要单元测试的遗留代码,就像在这个示例中使用Fire TV集成SDK示例应用一样。

Android Studio panel screenshot

Q检测到有一些测试依赖项,但没有测试文件夹或文件。

Android Studio panel screenshot

根据发现的不足,Q可以为项目的所有主要部分创建一套全面的测试。

Android Studio panel screenshot

Q甚至会创建一个摘要文件(以markdown格式),详细说明项目当前的测试覆盖率。

Copied to clipboard
# Test Coverage Summary

This document provides an overview of the test coverage for the Fire TV Integration SDK Sample App.

## Unit Tests

### SDK Package Tests
- **FireTvIntegrationSDKUtilsTest**
  - Tests the `hashProfileId` function to ensure it correctly hashes profile IDs
  - Tests the `isFTVIntegrationSDKSupportedOnDevice` function for both supported and unsupported devices

- **FireTvPlaybackReporterTest**
  - Tests reporting of different playback states (PLAYING, PAUSED, EXIT, INTERSTITIAL)
  - Tests that no events are reported when the feature is not supported

- **FireTvWatchlistReporterTest**
  - Tests adding items to the watchlist
  - Tests removing items from the watchlist
  - Tests setting the entire watchlist
  - Tests that no actions are performed when the feature is not supported

- **MyAmznDataIntegrationServiceTest**
  - Tests retrieving and reporting recent playback events
  - Tests retrieving and reporting watchlist entries
  - Tests handling of non-watchlist types

### Util Package Tests
- **ExoPlayerUtilsTest**
  - Tests reporting of current player state for different scenarios (playing normal content, playing ads, paused, ended)
  - Tests reporting when the player exits playback

### Account Package Tests
- **AccountRepositoryTest**
  - Tests setting and getting login state
  - Tests setting and getting active profile
  - Tests the LiveData functionality for active profile

### Client Package Tests
- **MyCustomerDataApiClientTest**
  - Tests retrieving customer watch activity
  - Tests retrieving customer purchased content
  - Tests retrieving customer rented content
  - Tests retrieving customer recorded content
  - Tests retrieving customer subscriptions
  - Tests retrieving customer watchlist

### Model Tests
- **VideoModelTest**
  - Tests Video data class properties and equality
  - Tests WatchlistItem data class properties and equality

- **CustomerDataApiReturnTypesTest**
  - Tests conversion of CustomerPurchasedContent to AmazonContentEntitlement
  - Tests conversion of CustomerRentedContent to AmazonContentEntitlement
  - Tests conversion of CustomerRecordedContent to AmazonContentEntitlement
  - Tests conversion of CustomerListEntry to AmazonCustomerListEntry
  - Tests conversion of CustomerSubscriptionEntitlement to AmazonSubscriptionEntitlement
  - Tests conversion of CustomerTitleWatched to AmazonPlaybackEvent
  - Tests enum conversions for MyPlaybackState and MyEntitlementType

## Instrumentation Tests

- **MainActivityTest**
  - Tests that MainActivity shows SignInFragment when not logged in
  - Tests that MainActivity shows VideoBrowserFragment when logged in
  - Tests the FragmentReplacementListener functionality

## Infrastructure Changes

- Added necessary test directories (`test` and `androidTest`)
- Updated `build.gradle` with additional testing dependencies:
  - Mockito for mocking dependencies
  - PowerMock for mocking static methods
  - AndroidX Test libraries for instrumentation tests
  - Architecture Components testing for LiveData testing
- Modified `AccountRepository` to make it testable by adding a method to set the instance for testing

## Future Improvements

- Add more instrumentation tests for fragments and UI components
- Add integration tests for the interaction between components
- Implement code coverage reporting to identify areas that still need testing
- Add UI tests using Espresso for testing user interactions

测试在运行时可能会失败,请在调试和改进代码方面寻求帮助,或者请Q帮助您编写更多测试,如功能和集成测试。

Android Studio panel screenshot

只是要记住让您的提示明确且具体,以确保Q理解您的上下文和目标。

Android Studio panel screenshot

使用这样的明确提示将可让Q创建一组全面的测试,涵盖组件之间的交互。

Android Studio panel screenshot

场景6:用另一种编程语言重构代码

如果您的任务是将旧版Java应用重构为现代Kotlin应用,该怎么办?Q也可以提高您的生产力和效率。

可以考虑使用这个用Java编写的示例存储库,其中展示了如何在Android应用中集成Login with Amazon SDK。您可以要求Q帮助您重构代码,将其移植到Kotlin。

Android Studio panel screenshot

Q将审查该项目,以了解如何以最佳效果重构代码库,然后应用这些转换。

Android Studio panel screenshot

Q可以用Kotlin等效文件替换Java文件,如此示例中所示。

Android Studio panel screenshot

场景7:识别并修复潜在漏洞

Q还可以帮助您发现并修复代码中的潜在安全漏洞。使用聊天面板中的/review命令,或从应用程序右下角的Amazon Q工具菜单中单击Full Project Scan(完整项目扫描)。

Android Studio panel screenshot

Q将对代码库进行全面的安全审查,该审查部分由Amazon Q检测器库(仅提供英文版)提供支持,该库是一组精心策划的代码分析检测器,旨在发现常见的安全问题。

Android Studio panel screenshot

Q将扫描代码库,检测潜在问题,然后根据关键度对问题进行分类。对于它发现的每个问题,它都会提供详细的解释,指出有风险的代码块并提供修复。

Android Studio panel screenshot

在命令行上使用Amazon Q

Amazon Q开发者版CLI可以扩展您的生产力,从而超越IDE范畴。可以就像在IDE中一样使用它,例如生成代码、帮助使用SDK、编写测试,并且可以方便快捷地在终端内工作。

Copied to clipboard
$ q

To learn more about MCP safety, see https://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/command-line-mcp-security.html

Welcome to 

 █████╗ ███╗   ███╗ █████╗ ███████╗ ██████╗ ███╗   ██╗     ██████╗ 
██╔══██╗████╗ ████║██╔══██╗╚══███╔╝██╔═══██╗████╗  ██║    ██╔═══██╗
███████║██╔████╔██║███████║  ███╔╝ ██║   ██║██╔██╗ ██║    ██║   ██║
██╔══██║██║╚██╔╝██║██╔══██║ ███╔╝  ██║   ██║██║╚██╗██║    ██║▄▄ ██║
██║  ██║██║ ╚═╝ ██║██║  ██║███████╗╚██████╔╝██║ ╚████║    ╚██████╔╝

╭───────────────────────── Did you know? ─────────────────────────╮
│                                                                 │
│         You can use /editor to edit your prompt with a          │
│                       vim-like experience                       │
│                                                                 │
╰─────────────────────────────────────────────────────────────────╯


/help all commands  •  ctrl + j new lines  •  ctrl + k fuzzy search
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
> I need to work with the Leanback Library for an Amazon FireTV App that I'm building. Where should I start?

To start working with the Leanback Library for your Amazon FireTV App, I'll guide you through the essential steps:

The Leanback Library is part of the Android TV support libraries specifically designed for creating TV-optimized interfaces, which is perfect for your FireTV app.

Here's how to get started:

1. Set up your development environment:
   • Make sure you have Android Studio installed
   • Install the necessary SDK components for TV development

2. Create a new Android TV project:
   • In Android Studio, create a new project with the "Android TV Activity" template
   • This automatically includes the Leanback dependencies

3. Add Leanback Library dependencies to your existing project:
   • Open your app's build.gradle file and add:

gradle
dependencies {
    // Leanback library
    implementation 'androidx.leanback:leanback:1.0.0'
    
    // Optional but recommended for Leanback apps
    implementation 'androidx.leanback:leanback-preference:1.0.0'
    implementation 'androidx.leanback:leanback-paging:1.0.0'
}

4. Configure your AndroidManifest.xml:
   • Ensure your app is marked as a TV app
   • Add required Leanback theme

xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.yourapp">
    
    <uses-feature
        android:name="android.hardware.touchscreen"
        android:required="false" />
        
    <uses-feature
        android:name="android.software.leanback"
        android:required="true" />
        
    <application
        android:banner="@drawable/app_banner"
        android:theme="@style/Theme.Leanback">
        
        <!-- Activities -->
        <activity
            android:name=".MainActivity"
            android:theme="@style/Theme.Leanback">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LEANBACK_LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

5. Implement key Leanback components:
   • BrowseFragment: Main navigation interface
   • DetailsFragment: For showing detailed content
   • PlaybackFragment: For media playback
   • SearchFragment: For search functionality

6. Follow Fire TV specific guidelines:
   • Optimize for D-pad navigation
   • Implement proper focus handling
   • Design for 10-foot UI experience

Would you like me to provide a simple code example for implementing a basic BrowseFragment to get you started with the UI structure?

总结: 以更智能的方式开发Fire应用

Amazon Q开发者版旨在减少开发障碍,帮助您更有效地从想法进展到实施。

如果您正在为Fire平板电脑或Fire TV开发应用,Q是很不错的入门途径,可以提高代码质量,自动处理重复性任务,并简化您的整体工作流程。可以在Android Studio IDE中或者直接在命令行中试着使用它,看看它如何在您的开发过程中发挥作用。

相关文章

最新文章

 

查看有关亚马逊应用商店、应用开发与盈利、亚马逊服务以及更多主题的最新消息。