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를 펼치고 build.gradle (Project: Android_Getting_Started)를 엽니다. buildscript 및 allprojects 블록의 repositories 블록 내에 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에서 build.gradle (Module:app)을 열고 implementations 블록에 Amplify 프레임워크 코어 종속성을 추가합니다.
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 Sync를 실행합니다.
잠시 후 다음 메시지가 표시됩니다.
BUILD SUCCESSFUL in 1s
-
런타임 시 Amplify 초기화
백엔드 상호 작용을 위한 코드를 그룹화할 백엔드 클래스를 생성합니다. 싱글톤 설계 패턴을 사용합니다. 이렇게 하면 애플리케이션을 통해 손쉽게 사용할 수 있고 Amplify 라이브러리가 한 번만 초기화됩니다.
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 } }
애플리케이션이 시작되면 싱글톤 백엔드 객체를 초기화합니다.
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) } }
매니페스트에서 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