Android のサンプルの構築

Android API の Sample

ファイル → 新規 → その他 → Android Sample Project があった。(Android用のなんか入れといたから?)

Instructions for the Calendar V3 Android Sample の簡易和訳

※ Android 向け Google Calendar API の事例を参考にしていますが、Android系のサンプルはほぼ同じと思われる。

Warning

This sample is for illustrative purposes. Please consider using the nativeCalendar Provider API.

Browse Online

必要システム

  • Java(requires 6 or higher)
  • Mercurial(requires 1.6 or higher).
  • Gradle 1.6
  • Android Studio(which bundles the Android SDK) orAndroid SDK(Android Studioを使わない場合). Set your ANDROID_HOME to the Android SDK installation directory, which for Android Studio is in the "sdk" subdirectory.
    • 私のマシンで

アプリケーションの登録

Google App Engine を使うサンプルのため、登録が必要

  • Visit theGoogle Cloud console flow for the Google Calendar API.
  • Google Accountへサインイン、project を作成(選択)。Click Continue.
  • Platformに "Android".
    • "Package name" には、他で(他の誰にも)使われていない名前を使う。例えば "org.example.calendar.janedoe20130911".
      • 注:Make sure to pick a legal Java package name since we will be using it in the Android project source code. パッケージ名は、2つ以上のパートで構成されていること。 ダメな例 "janedoe20130911"
      • Note:If you try to register a package name that has already been registered, you may get an error reading "An unexpected error has occurred." In particular, "com.google.api.services.samples.calendar.android" has already been registered, so you must change the package name.
    • "SHA1 fingerprint" には、ターミナルで下記の方法で値を取得し、値を入れる
      keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore | openssl sha1
      
      When asked, use "android" (without the quotes) as the keystore password.
      • keytool が動くかどうかは keytool -help で確認。見つからなければ(動作しなければ)、keytoolへのパスが通っていない。
    • Click Register.
  • ページ左の "APIs & auth" -> "APIs"  を選び、"Calendar API"がオンになっていることを確認

Checkout Instructions

Mercurial が入っていないと動きません

  cd[someDirectory]hg clone https://code.google.com/p/google-api-java-client.samples/ google-api-java-client-samples cd google-api-java-client-samples/calendar-android-sample

Android Studio の設定

  • Android SDK Managerを起動
    • ターミナルで動かす eclipse の Android SDK Manager も、結局コレを呼び出していました。
      $ANDROID_HOME/tools/android sdk
      
      • いきなり $ANDROID_HOME なんていう変数設定していないんですけど。
    • 以下にチェックを入れる
      • Tools/Android SDK Build-tools revision 18.0.1.
      • Android 2.2 (API 8)/SDK Platform, Google APIs
        • version はこれが必須なんだろうか、ドキュメントが古いんだろうか
      • Extras/Android Support Repository, Android Support Library, Google Repository
    • "Install X packages..." をクリック
  • Start Android Studio by running this command in a terminal:
    android-studio/bin/studio.sh
    
  • calendar-android-sampleのインポート
    • Select "Import Project..." or File > Import Project...
    • Select[someDirectory]/google-api-java-client-samples/build.gradleand click OK.
      • Note:it will not work if you try to import[someDirectory]/google-api-java-client-samples/calendar-android-sample/build.gradle.
    • Select "Use local gradle distribution" with "Gradle home" of[someDirectory]/gradle-1.6and click OK.
  • Open AndroidManifest.xml and change the package to match the name you registered in the Google Cloud Console, e.g. "org.example.calendar.janedoe20130911".
  • Rename the Java package: right-click on src/main/java, click New, click Package, and then enter the name you registered in the Google Cloud Console, e.g. "org.example.calendar.janedoe20130911". Next, select the Java classes in the package "com.google.api.services.samples.calendar.android", drag them into the new package you just created, and then click Refactor.
    • Note:If you get a compile error for "R", try right-clicking on the Java package and then clicking "Optimize Imports" to remove unused imports containing the "R". Another possibility is that the "package" in the AndroidManifest.xml file doesn't match the Java package name.
  • Run > Run 'calendar-android-sample'
    • Note:you must use a physical device for developing and testing because Google Play services cannot be installed on an emulator.Read this pagefor more information on how to set up a device.
    • To enable logging of HTTP requests and responses (highly recommended when developing):
      $ANDROID_HOME/platform-tools/adb shell setprop log.tag.HttpTransport DEBUG
      
      and then changeCalendarSampleActivity.LOGGING_LEVELtoLevel.CONFIG.
    • Note:If get "[Error] Access Not Configured" it usually means the Android app registration didn't work or doesn't match your app's package name and SHA1 fingerprint. It may also be because "Calendar API" isn't enabled for your project in the Google Cloud Console (see instructions above).
最終更新:2013年11月10日 21:20