AWS の開始方法
Android アプリケーションをビルドする
AWS Amplify を使用してシンプルな Android アプリケーションを作成する
モジュール 2: Amplify を初期化する
このモジュールでは、Amplify CLI をインストール、設定します。
はじめに
Android アプリケーションが作成できました。開発を継続して新機能を追加していきましょう。
アプリケーションで AWS Amplify の使用を開始するには、Amplify コマンドラインをインストールして Amplify プロジェクトディレクトリを初期化し、Amplify ライブラリを使用するようにプロジェクトを構成し、実行時に Amplify ライブラリを初期化する必要があります。
学習内容
- 新しい Amplify プロジェクトを初期化する
- プロジェクトに Amplify ライブラリを追加する
- 実行時に Amplify ライブラリを初期化する
主要な概念
Amplify CLI – Amplify CLI により、ターミナルから直接 AWS のサービスを作成、管理、削除できます。
Amplify ライブラリ – Amplify ライブラリにより、ウェブまたはモバイルアプリケーションから AWS のサービスとやり取りできます。
所要時間
10 分
使用するサービス
実装
-
Amplify CLI をインストールする
AWS Amplify CLI は Node.js に依存します。Node.js のインストールについては、「はじめに」の前提条件のセクションを参照してください。
AWS Amplify CLI をインストールするには、ターミナルを開き、下記のコマンドを入力します。
## Install Amplify CLI npm install -g @aws-amplify/cli ## Verify installation and version amplify --version # Scanning for plugins... # Plugin scan successful # 4.29.4
-
Amplify バックエンドを初期化する
バックエンドのベーシックな構造を作成するには、最初に Amplify プロジェクトディレクトリを初期化し、クラウドのバックエンドを作成する必要があります。
ターミナルを開き、プロジェクトのディレクトリに変更します。たとえば、プロジェクトを ~/AndroidStudioProjects/android-getting-started フォルダに作成したのであれば、下記のように入力します。
cd ~/AndroidStudioProjects/android-getting-started
正しいディレクトリを開いていることを確認してください。次のように表示されます。
➜ android-getting-started git:(main) ✗ ls -al total 32 drwxr-xr-x 14 stormacq admin 448 Oct 6 14:06 . drwxr-xr-x 16 stormacq admin 512 Oct 6 11:28 .. -rw-r--r-- 1 stormacq admin 208 Oct 6 14:04 .gitignore drwxr-xr-x 6 stormacq admin 192 Oct 6 14:04 .gradle drwxr-xr-x 13 stormacq admin 416 Oct 6 15:19 .idea drwxr-xr-x 8 stormacq admin 256 Oct 6 14:06 app drwxr-xr-x 3 stormacq admin 96 Oct 6 14:06 build -rw-r--r-- 1 stormacq admin 642 Oct 6 14:04 build.gradle drwxr-xr-x 3 stormacq admin 96 Oct 6 14:04 gradle -rw-r--r-- 1 stormacq admin 1162 Oct 6 14:04 gradle.properties -rwxr--r-- 1 stormacq admin 5296 Oct 6 14:04 gradlew -rw-r--r-- 1 stormacq admin 2260 Oct 6 14:04 gradlew.bat -rw-r--r-- 1 stormacq admin 437 Oct 6 14:04 local.properties -rw-r--r-- 1 stormacq admin 59 Oct 6 14:04 settings.gradle
Amplify プロジェクトの構造と設定ファイルを初期化します。次のコマンドを実行します。
amplify init ? Enter a name for your project (androidgettingstarted): accept the default, press enter ? Enter a name for the environment (dev): accept the default, press enter ? Choose your default editor: use the arrow key to select your favorite text editor an press enter ? Choose the type of app that you're building: android is already selected, press enter ? Where is your Res directory: accept the default, press enter ? Do you want to use an AWS profile?, Y, press enter ? Please choose the profile you want to use: use the arrow keys to select your profile and press enter.
まだプロフィールを持っていない場合は、AWS CLI で aws configure --profile <name> を使用して作成することができます。
Amplify は、クラウドでプロジェクトを初期化します。これには数分かかる場合があります。数分後、下記のようなメッセージが表示されます。
✔ Successfully created initial AWS cloud resources for deployments. ✔ Initialized provider successfully. Initialized your environment successfully. Your project has been successfully initialized and connected to the cloud!
-
プロジェクトに Amplify ライブラリを追加する
Amplify for Android は、Apache Maven パッケージとしてディストリビューションされています。このセクションでは、パッケージおよびさまざまな必須指示をビルド設定に追加します。
Android Studio に戻り、[Gradle Scripts (Gradle スクリプト)] を展開して、build.gradle (Gradle を構築) (Project: Android_Getting_Started) を開きます。buildscript ブロックおよび allprojects ブロックにあるリポジトリブロック内に mavenCentral() の行を追加します。
buildscript { ext.kotlin_version = "1.4.10" repositories { google() jcenter() // Add this line into `repositories` in `buildscript` mavenCentral() } dependencies { classpath "com.android.tools.build:gradle:4.0.1" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { google() jcenter() // Add this line into `repositories` in `buildscript` mavenCentral() } }
[Gradle Scripts (Gradle スクリプト)] から、build.gradle (Gradle を構築) (Module:app) を開き、implementations ブロックに Amplify framework core dependency を追加します。
dependencies { implementation fileTree(dir: "libs", include: ["*.jar"]) implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" implementation 'androidx.core:core-ktx:1.3.2' implementation 'androidx.appcompat:appcompat:1.2.0' implementation 'com.google.android.material:material:1.2.1' implementation 'androidx.constraintlayout:constraintlayout:2.0.1' implementation 'androidx.navigation:navigation-fragment-ktx:2.3.0' implementation 'androidx.navigation:navigation-ui-ktx:2.3.0' testImplementation 'junit:junit:4.13' androidTestImplementation 'androidx.test.ext:junit:1.1.2' androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' // Amplify core dependency implementation 'com.amplifyframework:core:1.4.0' }
Java かターゲット Android SDK 21 以前で開発する場合の追加的な設定変更は、ドキュメントを参照してください。
Gradle の同期を実行します。
しばらくすると、下記が表示されます。
BUILD SUCCESSFUL in 1s
-
実行時に Amplify を初期化する
バックエンドとインタラクションするコードをグループ化するためのバックエンドクラスを作成してみます。Singleton デザインパターンを使用するとアプリケーションで簡単に利用できます。Amplify ライブラリが 1 回だけ初期化されるようにします。
クラス初期化子は、Amplify ライブラリの初期化を処理します。
java/com.example.androidgettingstarted の直下で、新しい kotlin ファイルの Backend.kt を作成して開き、このコードを追加します。
package com.example.androidgettingstarted import android.content.Context import android.util.Log import com.amplifyframework.AmplifyException import com.amplifyframework.core.Amplify object Backend { private const val TAG = "Backend" fun initialize(applicationContext: Context) : Backend { try { Amplify.configure(applicationContext) Log.i(TAG, "Initialized Amplify") } catch (e: AmplifyException) { Log.e(TAG, "Could not initialize Amplify", e) } return this } }
アプリケーションの起動が完了すると、Singleton のバックエンドオブジェクトを初期化します。
java/com.example.androidgettingstarted の直下で、新しい kotlin ファイルの Application.kt を作成して開き、このコードを追加します。
package com.example.androidgettingstarted import android.app.Application class AndroidGettingStartedApplication : Application() { override fun onCreate() { super.onCreate() // initialize Amplify when application is starting Backend.initialize(applicationContext) } }
manifest の直下で、AndroidManifest.xml を開き、<application> エレメントにアプリケーションクラスの名前を追加します。
<!-- add the android:name attribute to the application node --> <application android:name="AndroidGettingStartedApplication" 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/Theme.GettingStartedAndroid"> ...
このファイルを開いているあいだに、このチュートリアルでこのあとアプリケーションで必要になるアクセス許可をいくつか追加します。
<!-- add these nodes between manifest and application --> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
-
設定を検証する
すべてが想定どおりに動作することを検証するには、プロジェクトをビルドして実行します。ツールバーの [Run (実行)] アイコン ▶️ をクリックするか、または「^ R」と入力します。
すべてが想定どおりに動作することを確認するには、プロジェクトを構築して実行します。ツールバーの [ Run (実行) ] アイコン▶️ をクリックするか、または「 ^ R」と入力します。操作エラーがないようにします。
BUILD SUCCESSFUL in 6s 23 actionable tasks: 8 executed, 15 up-to-date