仕事で検証したのを忘れそうなのでメモ。
「手順の保存」が目的なのでコンポーネントの説明とかは端折っていきます。

長くなったので「Backend 準備編」と「アプリコーディング編」に分けました。
Amplify の使い方はいいよ!という方はアプリコーディング編をどうぞ。

記録を残す意味でログとか載せましたが必要な部分はマスクしたりダミー入れたりしてます。

環境・言語

Android Studio 3.3.2
Kotlin 1.3.21

あと使える AWS アカウントを用意します。

1. AWS Amplify CLI をインストール、設定

AWS Amplicy CLI とは、各種 AWS サービスを利用したアプリをカンタンに作るための CLI ツール。

まずはここから Node.js をインストール。
https://nodejs.org/ja/

npm で amplify をインストール。

$ npm install -g @aws-amplify/cli

インストールされたか確認。

$ amplify -v
1.6.4

amplify の基本設定をする。
なんかいろいろ聞かれます。ブラウザ開いて AWS アカウント確認したり、コンソール上でリージョン選んだりユーザ名とかキー入力したりします。

$ amplify configure
Follow these steps to set up access to your AWS account:

Sign in to your AWS administrator account:
https://console.aws.amazon.com/
Press Enter to continue

Specify the AWS Region
? region:  us-east-1
Specify the username of the new IAM user:
? user name:  <IAMユーザ名>
Complete the user creation using the AWS console
https://console.aws.amazon.com/iam/home?region=undefined#/users$new?step=final&accessKey&userNames=ideno-test&permissionType=policies&policies=arn:aws:iam::aws:policy%2FAdministratorAccess
Press Enter to continue

Enter the access key of the newly created user:
? accessKeyId:  <アクセスキー>
? secretAccessKey:  <シークレットアクセスキー>
This would update/create the AWS Profile in your local machine
? Profile Name:  <ユーザー名>

Successfully set up the new user.

2. Android プロジェクトを作る

Android Studio で普通に新しいプロジェクト作ります。

3. gradle と manifest の設定

project/build.gradle, app/build.gradle, AndroidManifest にそれぞれ以下の設定を入れる。

project/build.gradle

classpath 'com.amazonaws:aws-android-sdk-appsync-gradle-plugin:2.7.+'

app/build.gradle

apply plugin: 'com.amazonaws.appsync'

dependencies {
    //Base SDK
    implementation 'com.amazonaws:aws-android-sdk-core:2.13.+'
    //AppSync SDK
    implementation 'com.amazonaws:aws-android-sdk-appsync:2.7.+'
    implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.0'
    implementation 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1'
}

AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

        <!--other code-->

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <service android:name="org.eclipse.paho.android.service.MqttService" />

        <!--other code-->
    </application>

4. Backend のセットアップ

インストールした AWS amplify CLI を使って Backend のセットアップをする。

$ cd <Android アプリのプロジェクトフォルダ>
$ amplify init
Note: It is recommended to run this command from the root of your app directory
? Enter a name for the project <プロジェクト名を入力>
? Enter a name for the environment <環境名を入力>
? Choose your default editor: <デフォルトエディタを選ぶ>
? Choose the type of app that youre building android
Please tell us about your project
? Where is your Res directory:  app/src/main/res
Using default provider  awscloudformation

For more information on AWS Profiles, see:
https://docs.aws.amazon.com/cli/latest/userguide/cli-multiple-profiles.html

? Do you want to use an AWS profile? Yes
? Please choose the profile you want to use <プロファイル名>
⠏ Initializing project in the cloud...

CREATE_IN_PROGRESS ***-********AWS::CloudFormation::Stack Wed Apr 24 2019 12:32:13 GMT+0900 (GMT+09:00) User Initiated             
CREATE_IN_PROGRESS DeploymentBucket            AWS::S3::Bucket            Wed Apr 24 2019 12:32:18 GMT+0900 (GMT+09:00)                            
  : 
  :
以下ログ、省略

$ amplify push
Current Environment: develop

| Category | Resource name | Operation | Provider plugin |
| -------- | ------------- | --------- | --------------- |

この手順で、app/src/main/res に awsconfiguration.json が作られます。

ここまでで基本的な Backend の準備は完了。

5. Backend に analytics を追加

ここから analytics の設定に入っていきます。

$ cd <Android アプリのプロジェクトフォルダ>
$ amplify add analytics
? Provide your pinpoint resource name: <pinpoint のリソース名を入力>
Adding analytics would add the Auth category to the project if not already added.
? Apps need authorization to send analytics events. Do you want to allow guests and unauthenticated users to send anal
ytics events? (we recommend you allow this when getting started) Yes <guest での analytics 情報送信を許可する場合は Yes で>
Successfully added auth resource locally.
Successfully added resource <リソース名> locally

Some next steps:
"amplify push" builds all of your local backend resources and provisions them in the cloud
"amplify publish" builds all your local backend and front-end resources (if you have hosting category added) and provisions them in the cloud

完了後に以下のコマンドで、Analytics のリソースが追加されたことが確認できます。
cognito も追加されてますが、これは自動で行われます。
(フェデレーティッドアイデンティティです)

$ amplify status

Current Environment: develop

| Category  | Resource name   | Operation | Provider plugin   |
| --------- | --------------- | --------- | ----------------- |
| Auth      | cognito1e*****  | Create    | awscloudformation |
| Analytics | pinpointresource| Create    | awscloudformation |

完了後、AWS へ push する。

$ amplify push
Current Environment: develop

| Category  | Resource name   | Operation | Provider plugin   |
| --------- | --------------- | --------- | ----------------- |
| Auth      | cognito1e*****  | Create    | awscloudformation |
| Analytics | pinpointresource| Create    | awscloudformation |
? Are you sure you want to continue? Yes
⠇ Updating resources in the cloud. This may take a few minutes...
  :
  :
ログ省略

次回、アプリケーションのコードから Analytics 情報を送信!!

長くなったのでアプリのコードは別記事にします。
Android アプリで AWS pinpoint を使って Analytics 情報を収集する
〜アプリコーディング編

参考文献

Amplify Framework
https://aws-amplify.github.io/docs/

Amplify Android SDK – Getting Started
https://aws-amplify.github.io/docs/android/start

Amplify Android SDK – Analytics
https://aws-amplify.github.io/docs/android/analytics

元記事はこちら

Android アプリで AWS pinpoint を使って Analytics 情報を収集する 〜Backend 準備編