旧. -開発日記-

〜とあるプログラマーのつぶやきや開発メモなど〜

外部ライブラリの読み込み設定方法(Google Maps APIを例に)

久々にブログのほう書きます。
Eclipseでは散々やってきたけど、IDEAのほうでいざやろうとしたら
異常に手間取って危うく諦めるところだったので、忘れないように
メモがてら書き書き...

まずIDEAを起動したら、「Create New Project」
プロジェクト本体を作成します。

注意するのがここで指定するSDKの部分はGoogle APIs」でないといけません。
いつも通りAndroid 4.x Platformとかしたら動かないですよー。

f:id:librastudio:20140129115303p:plain


  • Support Library の追加


起動しましたら、今度は「Project Structure」を開き左項目から「Libraries」を選択、上の「+」を押してnew Project LibraryでJavaを選びます。

あとはandroid-support-v4.jarまでのパスをたどってやって、
決定をするとどのモジュールにそれを追加するか聞かれる画面がでるので
そのまま先ほどの本体となるモジュールを選択します。
f:id:librastudio:20140129120112p:plain

※モジュールは自動で表示されるので追加したいとこを選択してやります
f:id:librastudio:20140129120123p:plain



次に細かい詳細ははしょりますが、Google Maps Android API v2は
Google Play Service APIの一つとして配布されているため、
このライブラリを参照してやる必要があります。

同じく「Project Structure」左側項目から「Module」を選択。
先ほどと同じく「+」Import Moduleを選択し、
google-play-services_libがおいてあるパスまでを指定してやります。


f:id:librastudio:20140129121726p:plain
これでインポート完了!


f:id:librastudio:20140129123520p:plain
ちゃんとインポートできていますね

最後に右項目「Libraries」にうつって、google-play-services
追加されているので、右クリック->Add to Modules...
本体のモジュールに追加してやります。

f:id:librastudio:20140129123747p:plain


そして最後仕上げは「Module」から本体モジュールを選択し、
右側タブ「Dependencies」を選択、google-play-services_libを追加してやります。
下の「+」からModule Dependencyを選択し上記を選んでやればOK。

f:id:librastudio:20140129124835p:plain



では自身で取得したMaps APIキーを追記してやってプロジェクトを作成します。

main.xmlは適当に以下でやってみました。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:map="http://schemas.android.com/apk/res-auto"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
        >
    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/button1"
                android:layout_weight="1">
        </Button>
        <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/button2"
                android:layout_weight="1">
        </Button>
        <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/button3"
                android:layout_weight="1">
        </Button>
    </LinearLayout>
    <fragment
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:name="com.google.android.gms.maps.SupportMapFragment"/>
</LinearLayout>

Activityのほうは下記で。

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;

public class MyActivity extends FragmentActivity {
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

f:id:librastudio:20140129144423p:plain
ちゃんと表示された!




※こちらも忘れそうなので併せてメモ。
Genymotionで実行する際、Maps API等使う時に
失敗するので、Project Structureで各モジュールで
libのパスを空にしてやる。一時的に...。

でないと
INSTALL_FAILED_CPU_ABI_INCOMPATIBLE on device using intellij
こんなのが出る...。
詳しくは、ココ