Android ADBを利用する

Android

Android ADBを利用する

Android機に対して、シェルでいろんなことが実行できるのが、ADB(Android Debug Bridge
)というツールです。

Windows PCからの立ち上げ方法です。

  1. コマンドプロンプトを開く
  2. Android SDKの入っているフォルダの下、platform-toolsを指定します。
  3. そこにadb.exeが入ってますので、それをコマンドプロンプトから実行します。普通にダブルクリックだと、すぐ消えてしまいます。
  4. ADBのコマンド一覧がばーっと流れますけど、とりあえず
  5.  adb devices
    

    と入力してみて、USB接続しているスマートフォンが表示されれば、ADBで接続できたことになります。

    adb server is out of date. killingと毎回表示される

Android Google Map の APIキーが取得できない?

Android

この度、APIキーを取得しようとしたら、うまくいきませんでした。
理由としては、APIキーを取得するのに必要なのは COLOR(#993333){MD5} のフィンガプリントなのに、入力してたのは COLOR(#993333){SHA1} のものだったからでした。

以下、成功時の作業内容を記録。

1.フィンガープリントの取得

コマンドプロンプトで以下を入力。

 keytool -v -list -keystore ・・・/.android/debug.keystore

[ 結果 ]

 キーストアのパスワードを入力してください:
 
 *****************  WARNING WARNING WARNING  *****************
 *キーストアに保存された情報の整合性は*
 *検証されていません。整合性を検証するには*
 *キーストアのパスワードを入力する必要があります。*
 *****************  WARNING WARNING WARNING  *****************
 
 キーストアのタイプ: JKS
 キーストア・プロバイダ: SUN
 
 キーストアには1エントリが含まれます
 
 別名: androiddebugkey
 作成日: 2012/08/03
 エントリ・タイプ: PrivateKeyEntry
 証明書チェーンの長さ: 1
 証明書[1]:
 所有者: CN=Android Debug, O=Android, C=US
 発行者: CN=Android Debug, O=Android, C=US
 シリアル番号: 592d5468
 有効期間の開始日: Fri Aug 03 15:38:26 JST 2012終了日: Sun Jul 27 15:38:26 JST 2042
 証明書のフィンガプリント:
         MD5:  (伏せます)
         SHA1: (伏せます)
         SHA256: (伏せます)
          署名アルゴリズム名: (伏せます)
          バージョン: 3
 
 拡張:
 
 #1: ObjectId: 2.5.29.14 Criticality=false
 SubjectKeyIdentifier [
 KeyIdentifier [
 (伏せます)
 ]
 ]
 
 
 
 *******************************************
 *******************************************

2.APIキーの取得

以下のサイトの My certificate’s MD5 fingerprint:に、1の結果にあるMD5のフィンガプリントを入力して COLOR(#993333){Generate APIKey} を押せば取得できます。

https://developers.google.com/android/maps-api-signup?hl=ja

注記

2013年4月、新たに開発用のAPIキーを取得しようとしたところ、MD5のフィンガープリントは入力できず、APIキーが取得できなくなってしまいました。
Google Map APIがバージョン2に上がったので、できなくなったのですね。
開発やメンテナンスに困ります…。

https://developers.google.com/maps/documentation/android/v1/mapkey
Android Google Map Version1の APIキーが取得できない

Androi Espresso Intent.ACTION_GET_CONTENTのテスト

Android
Espresso

ファイルを選択して、その名称を表示するActivityのテストです。
Intent.ACTION_GET_CONTENTを使って、ファイル選択のダイアログを表示し、ファイルを選択して、その名称が表示されているかどうかというテストです。

※ちょっとしょっぱいことに、ファイル選択のダイアログ表示→そのあとファイルを選択する、というところまでは、自動化できませんでした(^_^;

この記事と
https://codelabs.developers.google.com/codelabs/android-testing/index.html?index=..%2F..%2Findex&hl=ja#7

こちらのStack Overflowを参考に
http://stackoverflow.com/questions/32142463/how-to-stub-select-images-intent-using-espresso-intents

作成しています。

 @RunWith(AndroidJUnit4.class)
 @LargeTest
 public class AddFileToRegisterTest {
 
    public static final String FACE_PHOTO_PATH = "/storage/emulated/0/Download/スクリーンショット-2015-07-16-23.37.54.png";
    public static final String FACE_PHOTO_NAME = "スクリーンショット-2015-07-16-23.37.54.png";
 
    @Rule
    public IntentsTestRule<RegisterActivity> mRegisterIntentsTestRule =
            new IntentsTestRule<>(RegisterActivity.class);
 
    @Before
    public void registerIdlingResource() {
        Espresso.registerIdlingResources(
                mRegisterIntentsTestRule.getActivity().getCountingIdlingResource());
    }
 
   @Test
    public void activityResult_IsHandledProperly() {
        // Build a result to return when a particular activity is launched.
        Bundle bundle = new Bundle();
        ArrayList<Parcelable> parcels = new ArrayList<>();
        Intent resultData = new Intent();
        Uri uri1 = Uri.parse("file:/" + FACE_PHOTO_PATH);
        Parcelable parcelable1 = (Parcelable) uri1;
        parcels.add(parcelable1);
        bundle.putParcelableArrayList(Intent.EXTRA_STREAM, parcels);
        // Create the Intent that will include the bundle.
        resultData.putExtras(bundle);
        Instrumentation.ActivityResult result = createImageCaptureActivityResultStub();
 
        intending(allOf(
                hasAction(equalTo(Intent.ACTION_GET_CONTENT))
         )).respondWith(result);
 
        // User action that results in "contacts" activity being launched.
        // Launching activity expects phoneNumber to be returned and displays it on the screen.
        onView(withId(R.id.last_name)).perform(closeSoftKeyboard());
        onView(withId(R.id.face_photo_upload)).perform(scrollTo()).perform(click());
 
 
        onView(withId(R.id.face_photo_title))
                .perform(scrollTo()) // Scroll to thumbnail
                .check(matches(withText(FACE_PHOTO_NAME)));
 
    }
 
      /**
     * Unregister your Idling Resource so it can be garbage collected and does not leak any memory.
     */
    @After
    public void unregisterIdlingResource() {
        Espresso.unregisterIdlingResources(
                mRegisterIntentsTestRule.getActivity().getCountingIdlingResource());
    }
 
    private Instrumentation.ActivityResult createImageCaptureActivityResultStub() {
        // Create the ActivityResult, with a null Intent since we do not want to return any data
        // back to the Activity.
        return new Instrumentation.ActivityResult(Activity.RESULT_OK, null);
    }
 }

ハマったことは

 intending(allOf(
                hasAction(equalTo(Intent.ACTION_GET_CONTENT))
         )).respondWith(result);

が、どうしても中身がNullになって、ヌルポで落ちてしまうことです。

答えは、Espressoさんの公式サイトに書いてありました!
Espresso-Intentsを利用するときは、IntentsTestRulを使えということだそうです。

いつものことですけどね、ちゃんとドキュメントは読むように!ってことですね。。。

IntentsTestRule

Use IntentsTestRule instead of ActivityTestRule when using Espresso-Intents. IntentsTestRule makes it easy to use Espresso-Intents APIs in functional UI tests. This class is an extension of ActivityTestRule, which initializes Espresso-Intents before each test annotated with @Test and releases Espresso-Intents after each test run. The activity will be terminated after each test and this rule can be used in the same way as ActivityTestRule.

    @Rule
    public IntentsTestRule<RegisterActivity> mRegisterIntentsTestRule =
            new IntentsTestRule<>(RegisterActivity.class);