העברת פרויקטים של Android אל Cloud Endpoints Frameworks 2.0

בדף הזה מוסבר איך להעביר אפליקציה קיימת של Android Cloud Endpoints בגרסה 1.0 אל Endpoints Frameworks ל-App Engine. שימו לב שבדף הזה, גרסה 1.0 של Endpoints נקראת גרסה 1.0 של Endpoints Frameworks, ו-Endpoints Frameworks החדש ל-App Engine נקרא גרסה 2.0 של Endpoints Frameworks.

יתרונות

למסגרת החדשה יש כמה יתרונות, למשל:

  • זמן אחזור קצר יותר של בקשות
  • שילוב טוב יותר עם תכונות של App Engine, כמו דומיינים מותאמים אישית.
  • תמיכה רשמית בהגדרות אישיות של Guice
  • תכונות חדשות לניהול ממשקי API (אופציונלי)

גרסה 2.0 של Endpoints Frameworks לא משפיעה על הממשקים של ה-API. הלקוחות הקיימים ממשיכים לעבוד אחרי ההעברה בלי שינויים בקוד בצד הלקוח.

העברת פרויקטים עם כמה מודולים ב-Android ל-Endpoints Frameworks בגרסה 2.0

בשלבים הבאים מוסבר איך להעביר פרויקטים של Endpoints Frameworks בגרסה 1.0 ב-Android Studio ל-Endpoints Frameworks בגרסה 2.0. המדריך מעביר פרויקט של Android Studio עם מודול של Endpoints.

רשימת משימות

השתמשו ברשימת המשימות הכללית הבאה בזמן שאתם עובדים עם המדריך להעברת נתונים (מיגרציה). המדריך הזה להעברת נתונים (מיגרציה) מבוסס על ההנחה שיש לכם פרויקט Android קיים שמשתמש במודולGoogle Cloud .

  1. לפני שמתחילים.
  2. מגדירים את Google Cloud CLI.
  3. אופציונלי: מורידים את הקוד לדוגמה.
  4. מעבר לגרסה 2.0 של Endpoints Frameworks.
  5. פורסים את מודול הקצה העורפי.
  6. יצירה של ספריות לקוח.

לפני שמתחילים

  1. מתקינים את Android Studio.
  2. מתקינים את Android SDK בגרסה 26 ואילך.
  3. מתקינים את ה-CLI של gcloud.

הגדרת ה-CLI של gcloud

כדי להגדיר את ה-CLI של gcloud:

  1. מאתחלים את ה-CLI של gcloud:

     gcloud init
    
  2. שימוש ב-Application Default Credentials:

     gcloud auth application-default login
    
  3. מתקינים את הרכיב app-engine-java:

     gcloud components install app-engine-java
    

אופציונלי: הורדה של הקוד לדוגמה

כדי לשכפל את הפרויקטים לדוגמה legacy ו-v2 מ-GitHub:

  1. משכפלים את המאגר לדוגמה ומעבירים אותו למכונה המקומית:

     git clone https://github.com/GoogleCloudPlatform/android-docs-samples
    
  2. עוברים לספרייה שמכילה את הקוד לדוגמה של legacy ו-v2:

     cd android-docs-samples/endpoints-frameworks/
    

מעבר לגרסה 2.0 של Endpoints Frameworks

עדכון הקובץ build.gradle

יחסי התלות של Endpoints Frameworks בגרסה 2.0 משתמשים ב-Guava 19, והפלאגין של Android Gradle build com.android.tools.build:gradle:2.3.3 משתמש ב-Guava 18. כדי לשנות את התלות הטרנזיטיבית הזו, צריך להוסיף את Guava 19 לסגירת יחסי התלות של סקריפט ה-build של build.gradle.

מדור קודם

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, כדי ליצור מסמך Discovery של API. בגרסה 2.0 של Endpoints Frameworks, משתמשים ב-endpoints-framework-gradle-plugin כדי ליצור מסמך Discovery.

סקריפט ה-build הזה משתמש בפלאגין בצד הלקוח, 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

כלי Gradle של סביבת ברירת המחדל ב-App Engine עודכנו. בפרויקטים של Endpoints Frameworks בגרסה 1.0 נעשה שימוש ב-gradle-appengine-plugin ב-com.google.appengine, ובפרויקטים של Endpoints Frameworks בגרסה 2.0 נעשה שימוש ב-appengine-gradle-plugin.

סקריפט ה-build הזה משתמש בפלאגין בצד השרת, 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

בגרסה 2.0 של Endpoints Frameworks, מחלקת ה-servlet השתנתה מ-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>

בנייה מחדש של הפרויקט

לבסוף, משתמשים ברשימת ה-build של Android Studio כדי לנקות את הפרויקט ב-Android Studio ולבנות אותו מחדש עם יחסי התלות החדשים.

בנייה מחדש של Android Studio

פריסת המודול של הקצה העורפי

הפלאגין החדש של Gradle App Engine משמש עכשיו לפריסת המודול לקצה העורפי, באמצעות משימת Gradle הבאה במודול backend:

gradle appengineDeploy

יצירה של ספריות לקוח

כדי ליצור ספריות לקוח, משתמשים במשימה הבאה של Gradle במודול backend:

gradle endpointsClientLibs

מידע נוסף על המשימות שאפשר לבצע באמצעות הפלאגין של Endpoints Frameworks ל-Gradle

הוספת ניהול ממשקי API של Endpoints

בנוסף, בגרסה 2.0 של Endpoints Frameworks אפשר להפעיל תכונות של ניהול ממשקי API, כולל:

  • ניהול מפתחות API
  • שיתוף API
  • אימות משתמשים
  • מדדי API
  • יומני API

כדי להתחיל להשתמש בתכונות האלה ובתכונות אחרות של Cloud Endpoints Frameworks 2.0, עברו לדף Java בנושא Endpoints Frameworks ל-App Engine.