1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

AWS AmplifyでAndroid アプリを作るときの作業(Auth, S3, API->Lambda)[メモ]

Last updated at Posted at 2020-09-07

AWSのAmplifyを使ってモバイルアプリを作ることになったので、その環境構築手順をメモしておく。基本は自分のための備忘録だが、誰かの参考になれば幸い。

前提

amplifyはインストール済み
pipenvが入っている
python 3.8.5がインストールされている

参考

amplifyのインストール

npm install -g @aws-amplify/cli
amplify configure

pythonの設定

pip install pipenv
pipenv --python 3.8.5
pipenv shell 

Androidでの開発

Android Studioでプロジェクトを作成した後にamplify cliでバックエンドの設定を行う。1

Android Studioでのプロジェクト作成

一般的な方法と同じなため、省略。
ただし、.gitignoreの内容が上書きされてしまうのでプロジェクト作成後はどこかにコピーを退避させておく。(最近は上書きされなくなった??)

amplify cliでバックエンド設定

ここでは、チュートリアルに倣いProdとDevの2stageで開発する想定とする。

Prod作成

まずはProdの作成。
Androidのプロジェクトのrootで(配下にappがあるフォルダ)下記を実施。

$ amplify init
Note: It is recommended to run this command from the root of your app directory
? Enter a name for the project fxxxxxxx
? Enter a name for the environment prod
? Choose your default editor: IntelliJ IDEA
? Choose the type of app that you're building android
Please tell us about your project
? Where is your Res directory:  app/src/main/res
Using default provider  awscloudformation
...<snip>

auth設定

$ amplify add auth
Using service: Cognito, provided by: awscloudformation
 
 The current configured provider is Amazon Cognito. 
 
 Do you want to use the default authentication and security configuration? Default configuration with Social Provider (Federation)
 Warning: you will not be able to edit these selections. 
 How do you want users to be able to sign in? Username
 Do you want to configure advanced settings? No, I am done.
 What domain name prefix do you want to use? fxxxxxx796012f-9796012f
 Enter your redirect signin URI: myapp://callback/
? Do you want to add another redirect signin URI No
 Enter your redirect signout URI: myapp://signout/
? Do you want to add another redirect signout URI No
 Select the social providers you want to configure for your user pool: 
Successfully added resource fxxxxx96012f locally


S3設定

$ amplify add storage
? Please select from one of the below mentioned services: Content (Images, audio, video, etc.)
? Please provide a friendly name for your resource that will be used to label this category in the project: fxxxx
? Please provide bucket name: fxxxx028f5691d9734f96b9559b3c1fbba634
? Who should have access: Auth users only
? What kind of access do you want for Authenticated users? create/update, read, delete
? Do you want to add a Lambda Trigger for your S3 Bucket? No
Successfully added resource fxxxxx locally

API Gateway & Lambda設定

$ amplify add api
? Please select from one of the below mentioned services: REST
? Provide a friendly name for your resource to be used as a label for this category in the project: fxxxx
? Provide a path (e.g., /book/{isbn}): /markers/{id}
? Choose a Lambda source Create a new Lambda function
? Provide a friendly name for your resource to be used as a label for this category in the project: fxxx12c31e67
? Provide the AWS Lambda function name: fxxxx12c31e67
? Choose the runtime that you want to use: Python
... <snip>

Githubへ登録

ここで一度ProdブランチをGithubに登録

予め、amplifyにより作成された.gitignoreを退避させたAndroid Studioの.gitignoreをマージしておく。

$ git init 
$ git add <all project related files>
$ git commit -m "Creation of a prod amplify environment"
$ git checkout -b prod
$ curl -u xxxxx --header 'x-github-otp: xxxxx' https://api.github.com/user/repos -d '{"name":"fxxxx"}'
$ git remote add origin git@github.com:xxxx/xxxx.git
$ amplify push

Dev作成

$ amplify env add
? Do you want to use an existing environment? No
? Enter a name for the environment dev

Github

$ git add .
$ git commit -m "Creation of a dev amplify environment"
$ git push -u origin prod
$ git checkout -b dev
$ git push -u origin dev

環境切り替え

envをcheckoutすることで、それぞれ異なるバックエンドに接続するようにawsconfiguration.jsonamplifyconfiguration.jsonが書き換わってくれる。

$ amplify env checkout dev

Android側の設定

build.gradle(Module:app)に下記のセクションを追加

    compileOptions {
        // Support for Java 8 features
        coreLibraryDesugaringEnabled true
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
   
    }

dependenciesのセクションに下記を追加

    implementation 'com.amplifyframework:core:1.3.1'
    coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.0.10'
    implementation 'com.amplifyframework:aws-auth-cognito:1.3.1'
    implementation 'com.amplifyframework:aws-api:1.3.1'
    implementation 'com.amplifyframework:aws-storage-s3:1.3.1'
    implementation "com.amazonaws:aws-android-sdk-apigateway-core:2.18.0"
  1. 逆の順番、つまり、amplify cliで環境を作ってからAndroid studioで同じフォルダにプロジェクトを作成すると、「すでにemptyではないフォルダがある」といったニュアンスの警告が出て、appのbuild.gradleやその他resourceが作成されない。復旧の仕方がよくわからないので、やめたほうがいい。やり方を知っている方がいらっしゃったら教えてください。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?