外部ライブラリの読み込み設定方法(Google Maps APIを例に)
久々にブログのほう書きます。
Eclipseでは散々やってきたけど、IDEAのほうでいざやろうとしたら
異常に手間取って危うく諦めるところだったので、忘れないように
メモがてら書き書き...
まずIDEAを起動したら、「Create New Project」で
プロジェクト本体を作成します。
注意するのがここで指定するSDKの部分は「Google APIs」でないといけません。
いつも通りAndroid 4.x Platformとかしたら動かないですよー。
- Support Library の追加
起動しましたら、今度は「Project Structure」を開き左項目から「Libraries」を選択、上の「+」を押してnew Project LibraryでJavaを選びます。
あとはandroid-support-v4.jarまでのパスをたどってやって、
決定をするとどのモジュールにそれを追加するか聞かれる画面がでるので
そのまま先ほどの本体となるモジュールを選択します。
※モジュールは自動で表示されるので追加したいとこを選択してやります
- Google Play Service APIの参照
次に細かい詳細ははしょりますが、Google Maps Android API v2は
Google Play Service APIの一つとして配布されているため、
このライブラリを参照してやる必要があります。
同じく「Project Structure」左側項目から「Module」を選択。
先ほどと同じく「+」でImport Moduleを選択し、
google-play-services_libがおいてあるパスまでを指定してやります。
これでインポート完了!
ちゃんとインポートできていますね
最後に右項目「Libraries」にうつって、google-play-servicesが
追加されているので、右クリック->Add to Modules...で
本体のモジュールに追加してやります。
そして最後仕上げは「Module」から本体モジュールを選択し、
右側タブ「Dependencies」を選択、google-play-services_libを追加してやります。
下の「+」からModule Dependencyを選択し上記を選んでやればOK。
では自身で取得した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); } }
ちゃんと表示された!
※こちらも忘れそうなので併せてメモ。
Genymotionで実行する際、Maps API等使う時に
失敗するので、Project Structureで各モジュールで
libのパスを空にしてやる。一時的に...。
でないと
INSTALL_FAILED_CPU_ABI_INCOMPATIBLE on device using intellij
こんなのが出る...。
詳しくは、ココ