Android AAR形式ライブラリ作成
Androidのライブラリモジュール(AAR)を作成する手順をサンプルを交えて説明します。 AARはJARと異なり、Androidリソースやマニフェストファイルを含めることができます。
今回は、サンプルとして動画ランダムに再生される動画広告ライブラリのようなものを作ります。
Android プロジェクトの新規作成
Android Projectを新しく作成します。
Android ライブラリモジュールの作成
1. メニューのFile
> New
> New Module
を選択します。
2. Create New Module
画面のAndroid Library
を選択し、Next
を押します。
3. Application/Library name
とModule name
を入力し、Finish
を押します。
4. モジュールが新しくプロジェクトに追加されます。
コードの記述
必要なコードを記述してください。今回は、RewardVideoAd#show()
を実行すると、ダイアログが表示され動画が再生されるというモジュールを作成しました。
下記コードを試す場合は、videoUrl
に動画のURLを入力してください。
- RewardVideoAd.java
package com.example.test.moviead; import android.app.Activity; import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.media.MediaPlayer; import android.net.Uri; import android.view.ViewGroup; import android.view.Window; import android.widget.LinearLayout; import android.widget.VideoView; import java.util.Calendar; public class RewardVideoAd { private VideoAdListener videoAdListener; private Dialog dialog; private VideoView videoView; /** * 動画URL。 */ private static final String[] videoUrl = { "ここにhttpから始まる動画のURLを入力してください。" }; /** * コンストラクタ */ public RewardVideoAd(Activity activity) { initDialog(activity); } /** * VideoAdListenerを設定する */ public void setVideoAdLisener(VideoAdListener listener) { this.videoAdListener = listener; } /** * 動画を再生する */ public void show() { dialog.show(); } /** * Dialogの初期化 * * @param context コンテキスト */ private void initDialog(Context context) { dialog = new Dialog(context); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); if (dialog.getWindow() != null) { dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); } initVideoView(dialog.getContext()); dialog.setContentView(videoView); dialog.setOnShowListener(new DialogInterface.OnShowListener() { @Override public void onShow(DialogInterface dialog) { // VideoViewの特性上、Viewが表示されてからロードが始まるため、 // Dialogが表示されてからロードを始めることとする。 videoView.setVideoURI(Uri.parse(videoUrl[(int) (Math.random() * videoUrl.length)])); } }); } private void initVideoView(Context context) { videoView = new VideoView(context); videoView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)); videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { videoView.start(); if (videoAdListener != null) { // リスナーを呼び出す videoAdListener.rewardVideoAdDidStartPlaying(new VideoRewardData("start video", Calendar.getInstance().getTimeInMillis())); } } }); videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { if (videoAdListener != null) { // リスナーを呼び出す videoAdListener.didRewardUserWithReward(new VideoRewardData("end video", Calendar.getInstance().getTimeInMillis())); } dialog.dismiss(); } }); } }
- VideoAdListener.java
package com.example.test.moviead; public interface VideoAdListener { void rewardVideoAdDidStartPlaying(VideoRewardData data); void didRewardUserWithReward(VideoRewardData data); }
- VideoRewardData.java
package com.example.test.moviead; public class VideoRewardData { public VideoRewardData(String name, long time) { this.name = name; this.time = time; } public String name; public long time; }
AARの作成
1. メニューのBuild
> Make Project
を選択します。
2.ビルドが成功すると、プロジェクトのbuild
ディレクトリのoputputs
にaar
ファイルが作成されます。例えば、今回のサンプルプロジェクトであれば、AndroidAarSample > movieadd > build > outputs > aar
に作成されます。
AARの使用
1.使用したいプロジェクトを開き、メニューのFile
> New
> New Module
を選択します。(今回は、UseAarExampleという新しいプロジェクトを作成しました。)
2. Create New Module
画面のImport .JAR/.AAR Package
を選択し、Next
を押します。
3.先程作成したaar
ファイルをを選択し、Finish
を押します。
4.aarを使用するプロジェクトを右クリックし、Open Module Settings
を選択します。
5.Dependencies
タブのリストの下にある+
ボタンをクリックし、Module dependency
を選択します。
6.関連付けるModuleを選択し、OK
を押します。
7.取り込んだModuleを使用して、アプリケーションを作成してください。下記サンプルコードでは、ボタンを作成して、ボタンを押したら動画が流れるようにしました。
- MainActivity.java
package com.example.aar.use.kurisankaku.xyz.useaarexample; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import com.example.test.moviead.RewardVideoAd; import com.example.test.moviead.VideoAdListener; import com.example.test.moviead.VideoRewardData; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final RewardVideoAd rewardVideoAd = new RewardVideoAd(this); findViewById(R.id.show_button).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { rewardVideoAd.show(); } }); rewardVideoAd.setVideoAdLisener(new VideoAdListener() { @Override public void rewardVideoAdDidStartPlaying(VideoRewardData data) { Log.i("tag",data.name + " start!!!!!!!!!!!!!!!!!!!!"); } @Override public void didRewardUserWithReward(VideoRewardData data) { Log.i("tag",data.name + " end!!!!!!!!!!!!!!!!!!!!"); } }); } }
- activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.aar.use.kurisankaku.xyz.useaarexample.MainActivity"> <Button android:id="@+id/show_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Show" /> </android.support.constraint.ConstraintLayout>
- AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.aar.use.kurisankaku.xyz.useaarexample"> <uses-permission android:name="android.permission.INTERNET" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
サンプルコードと合わせて試してみてください。