Cloud Endpoints Frameworks 2.0으로 Android 프로젝트 이전

이 페이지에서는 기존의 Android Cloud Endpoints 버전 1.0 앱을 App Engine용 Endpoints Frameworks로 이전하는 방법을 설명합니다. 이 페이지에서는 Endpoints 버전 1.0을 Endpoints Frameworks 버전 1.0으로, 새로운 App Engine용 Endpoints Frameworks를 Endpoints Frameworks 버전 2.0으로 지칭합니다.

장점

새로운 프레임워크에는 다음과 같은 여러 가지 장점이 있습니다.

  • 요청 지연 시간 감소
  • App Engine 기능(예: 커스텀 도메인)과의 통합 향상
  • Guice 구성 공식 지원
  • 새로운 API 관리 기능(선택사항)

Endpoints Frameworks 버전 2.0은 API에 대한 인터페이스에 영향을 주지 않습니다. 기존 클라이언트는 이전 후에도 클라이언트 측 코드 변경 없이 계속 작동합니다.

Endpoints Frameworks 버전 2.0으로 Android 멀티 모듈 프로젝트 이전

다음 단계에서는 Android 스튜디오 Endpoints Frameworks 버전 1.0 프로젝트를 Endpoints Frameworks 버전 2.0으로 이전하는 과정을 안내합니다. 가이드에서는 Endpoints 모듈이 있는 Android 스튜디오 프로젝트를 이전합니다.

작업 목록

마이그레이션 가이드를 진행할 때 아래의 개략적인 태스크 목록을 사용합니다. 이 마이그레이션 가이드에서는 Google Cloud 모듈을 사용하는 기존 Android 프로젝트가 있다고 가정합니다.

  1. 시작하기 전에
  2. Google Cloud CLI 설정
  3. 샘플 코드 다운로드(선택사항)
  4. Endpoints Frameworks 버전2.0으로 이전
  5. 백엔드 모듈 배포
  6. 클라이언트 라이브러리 생성

시작하기 전에

  1. Android 스튜디오를 설치합니다.
  2. Android SDK 26+를 설치합니다.
  3. gcloud CLI 설치

gcloud CLI 설정

gcloud CLI를 설정하려면 다음 안내를 따르세요.

  1. gcloud CLI를 초기화합니다.

     gcloud init
    
  2. 애플리케이션 기본 사용자 인증 정보를 사용합니다.

     gcloud auth application-default login
    
  3. app-engine-java 구성요소를 설치합니다.

     gcloud components install app-engine-java
    

샘플 코드 다운로드(선택사항)

GitHub에서 legacyv2 샘플 프로젝트를 클론하려면 다음 안내를 따르세요.

  1. 샘플 저장소를 로컬 머신에 클론합니다.

     git clone https://github.com/GoogleCloudPlatform/android-docs-samples
    
  2. legacyv2의 샘플 코드가 있는 디렉터리로 변경합니다.

     cd android-docs-samples/endpoints-frameworks/
    

Endpoints 프레임워크 버전 2.0으로 마이그레이션

build.gradle 파일을 업데이트합니다.

Endpoints Framework 버전 2.0 종속 항목은 Guava 19를 사용하고 Android Gradle 빌드 플러그인 com.android.tools.build:gradle:2.3.3은 Guava 18을 사용합니다. build.gradle의 빌드 스크립트 종속 항목 클로저에 Guava 19를 추가하여 이 임시 종속 항목을 재정의합니다.

기존

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'
    }
}

v2

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        // V2: Include this dependency when using Endpoints Framework v2
        classpath 'com.google.guava:guava:19.0'

        classpath 'com.android.tools.build:gradle:2.3.3'
    }
}

app/build.gradle 파일 업데이트

Endpoints 버전 1.0에서는 Gradle 플러그인(gradle-appengine-plugin)을 사용하여 API 검색 문서를 생성했습니다. Endpoints Frameworks 버전 2.0에서는 endpoints-framework-gradle-plugin을 사용하여 검색 문서를 생성합니다.

이 빌드 스크립트에서는 클라이언트 측 플러그인(com.google.cloud.tools.endpoints-framework-client)을 사용합니다.

기존

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion '26.0.1'
    defaultConfig {
        applicationId 'com.example.migration.endpoints.app'
        minSdkVersion 25
        targetSdkVersion 26
        versionCode 1
        versionName '1.0'
        testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    configurations.all {
        resolutionStrategy.force 'com.google.code.findbugs:jsr305:2.0.1'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    // androidTestCompile compiles instrumentation tests written using Espresso
    // used by Firebase Test Lab
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })

    compile 'com.android.support:appcompat-v7:26.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.1'
    compile 'com.google.code.findbugs:jsr305:2.0.1'
    compile 'com.google.http-client:google-http-client-android:1.22.0'
    compile 'com.google.api-client:google-api-client:+'
    testCompile 'junit:junit:4.12'

    // LEGACY: Legacy compile :backend project
    compile project(path: ':backend', configuration: 'android-endpoints')
}

v2

apply plugin: 'com.android.application'

// V2: Apply the new Endpoints Framework client plugin
apply plugin: 'com.google.cloud.tools.endpoints-framework-client'

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        // V2: Add the new Endpoints Framework plugin dependencies
        classpath 'com.google.cloud.tools:endpoints-framework-gradle-plugin:1.0.2'
    }
}

android {
    compileSdkVersion 26
    buildToolsVersion '26.0.1'
    defaultConfig {
        applicationId 'com.example.migration.endpoints.app'
        minSdkVersion 25
        targetSdkVersion 26
        versionCode 1
        versionName '1.0'
        testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    configurations.all {
        resolutionStrategy.force 'com.google.code.findbugs:jsr305:2.0.1'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    // androidTestCompile compiles instrumentation tests written using Espresso
    // used by Firebase Test Lab
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })

    compile 'com.android.support:appcompat-v7:26.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.1'
    compile 'com.google.code.findbugs:jsr305:2.0.1'
    testCompile 'junit:junit:4.12'

    // V2: Endpoints Framework v2 migration
    endpointsServer project(path: ':backend', configuration: 'endpoints')
    compile 'com.google.api-client:google-api-client:+'
    compile 'com.google.http-client:google-http-client-android:1.22.0'
}

backend/build.gradle 파일 업데이트

App Engine 표준 환경 Gradle 도구가 업데이트되었습니다. Endpoints Framework 버전 1.0 프로젝트는 com.google.appenginegradle-appengine-plugin을 사용했지만 Endpoints Framework 버전 2.0 프로젝트는 appengine-gradle-plugin을 사용합니다.

이 빌드 스크립트에서는 서버 측 플러그인(com.google.cloud.tools.endpoints-framework-server)을 사용합니다.

기존

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        // LEGACY: Deprecated App Engine plugin dependency
        classpath 'com.google.appengine:gradle-appengine-plugin:1.9.59'
    }
}

repositories {
    jcenter();
}

apply plugin: 'java'
apply plugin: 'war'

// LEGACY: Deprecated Apply App Engine plugin dependency
apply plugin: 'appengine'

sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7

dependencies {
    // LEGACY: Deprecated App Engine plugin dependency
    appengineSdk 'com.google.appengine:appengine-java-sdk:1.9.59'

    // LEGACY: Legacy Endpoints Framework dependencies
    compile 'com.google.appengine:appengine-endpoints:1.9.59'
    compile 'com.google.appengine:appengine-endpoints-deps:1.9.59'

    compile 'javax.servlet:servlet-api:2.5'
}

appengine { // LEGACY: Deprecated App Engine plugin Tasks configuration
    downloadSdk = true
    appcfg {
        oauth2 = true

        // extraOptions is used for acceptance test and
        // is not required.
        def application = findProperty("appengine.deploy.application")
        def version = findProperty("appengine.deploy.version")
        def serviceAccount = findProperty("appengine.deploy.serviceAccount")

        extraOptions = ["--application=" + application, "--version=" + version,
            '--service_account_json_key_file=' + serviceAccount]
    }
    endpoints {
        getClientLibsOnBuild = true
        getDiscoveryDocsOnBuild = true
    }
}

v2

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        // V2: Add the new App Engine and Endpoints Frameworks plugin dependencies
        classpath 'com.google.cloud.tools:endpoints-framework-gradle-plugin:1.0.2'
        classpath 'com.google.cloud.tools:appengine-gradle-plugin:1.3.3'
    }
}

repositories {
    jcenter();
}

apply plugin: 'java'
apply plugin: 'war'

// V2: Apply new App Engine and Endpoints Framework server plugins
apply plugin: 'com.google.cloud.tools.appengine'
apply plugin: 'com.google.cloud.tools.endpoints-framework-server'

sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7

dependencies {
    // V2: Endpoints Framework v2 migration
    compile 'com.google.endpoints:endpoints-framework:2.0.9'
    compile 'javax.inject:javax.inject:1'

    compile 'javax.servlet:servlet-api:2.5'
}

// V2: Define deployment configuration using the new App Engine plugin
// with the appengine closure
appengine {  // App Engine tasks configuration
    deploy {   // deploy configuration

        // The following is used for acceptance tests and
        // is not required for a migration.
        project = findProperty("appengine.deploy.project")
        version = findProperty("appengine.deploy.version")
        def promoteProp = findProperty("appengine.deploy.promote")
        if (promoteProp != null) {
            promote = new Boolean(promoteProp)
        }
    }
}

web.xml 파일 업데이트

Endpoints Frameworks 버전 2.0에서는 서블릿 클래스가 SystemServiceServlet에서 EndpointsServlet으로 변경되었습니다.

URL 패턴은 /_ah/spi/*에서 /_ah/api/*로 업데이트되었습니다.

기존

<servlet>
    <!-- LEGACY: Start of Legacy section -->
    <servlet-name>SystemServiceServlet</servlet-name>
    <servlet-class>com.google.api.server.spi.SystemServiceServlet</servlet-class>
    <!-- LEGACY: End of Legacy section -->
    <init-param>
        <param-name>services</param-name>
        <param-value>com.example.migration.endpoints.backend.MyEndpoint</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <!-- LEGACY: Start of legacy section -->
    <servlet-name>SystemServiceServlet</servlet-name>
    <url-pattern>/_ah/spi/*</url-pattern>
    <!-- LEGACY: End of legacy section -->
</servlet-mapping>

v2

<servlet>
    <!-- V2: Start of v2 section -->
    <servlet-name>EndpointsServlet</servlet-name>
    <servlet-class>com.google.api.server.spi.EndpointsServlet</servlet-class>
    <!-- V2: End of v2 section -->
    <init-param>
        <param-name>services</param-name>
        <param-value>com.example.migration.endpoints.backend.MyEndpoint</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <!-- V2: Start of v2 section -->
    <servlet-name>EndpointsServlet</servlet-name>
    <url-pattern>/_ah/api/*</url-pattern>
    <!-- V2: End of v2 section -->
</servlet-mapping>

프로젝트 다시 빌드

마지막으로, Android 스튜디오 빌드 목록을 사용하여 Android 스튜디오 프로젝트를 정리하고 새 종속 항목으로 다시 빌드합니다.

Android 스튜디오 다시 빌드

백엔드 모듈 배포

이제 backend 모듈에서 다음 Gradle 태스크를 사용하여 새 Gradle App Engine 플러그인을 통해 백엔드 모듈을 배포합니다.

gradle appengineDeploy

클라이언트 라이브러리 생성

클라이언트 라이브러리를 생성하려면 backend 모듈에서 다음 Gradle 태스크를 사용합니다.

gradle endpointsClientLibs

Endpoints Frameworks Gradle 플러그인에 사용 가능한 작업에 대해 자세히 알아보세요.

Endpoints API 관리 추가

Endpoints Frameworks 버전 2.0에서는 다음을 비롯한 API 관리 기능도 사용할 수 있습니다.

  • API 키 관리
  • API 공유
  • 사용자 인증
  • API 측정항목
  • API 로그

이러한 기능과 기타 Cloud Endpoints 프레임워크 2.0 기능을 사용하려면 App Engine용 Endpoints 프레임워크에 대한 자바 페이지를 참조하세요.