undefined

bokuweb.me

cocos2d-JSで作ったアプリにi-mobileの広告を表示させるまでの記録

cocos2d-JSで作ったアプリにi-mobileの広告を表示させるまでを記録しておきます。 アプリ起動時に最下部に表示させる最もシンプルな例です。

  • cocos2d-JS 3.3
  • Android Studio1.1.0
  • i-mobile Android SDK 2.08

i-mobileSDKのインストール

i-mobileのSDKダウンロードからAndroid用SDKをダウンロード

f:id:bokuweb:20150403195714p:plain

path_to_project/frameworks/runtime-src/proj.android/libsimobileSdkAds.jarを移動。

Google Play Services の設定

Android Studioの起動

SDKマネージャを開く

f:id:bokuweb:20150328093543p:plain

f:id:bokuweb:20150328093552p:plain

「Google Play Searvice」を選択->インストール

f:id:bokuweb:20150403211046p:plain

path_to_sdk/sdk/extras/google/google_play_services/libproject/google-play-services_libproj.android/libsに移動。

./libs/google-play-services_libへの参照をproject.propertiesに設定する。 以下設定例。

# This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must be checked in Version Control Systems.
#
# To customize properties used by the Ant build system use,
# "ant.properties", and override values to adapt the script to your
# project structure.

# Project target.
target=android-21

android.library.reference.1=../../js-bindings/cocos2d-x/cocos/platform/android/java
android.library.reference.2=./libs/google-play-services_lib

AndroidManifest.xmlに追記

AndroidManifest.xmlに以下追記

<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

以下設定例

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="org.cocos2dx.test"
      android:versionCode="1"
      android:versionName="1.0"
      android:installLocation="auto">

    <uses-sdk android:minSdkVersion="9"/>
    <uses-feature android:glEsVersion="0x00020000" />

    <application android:label="@string/app_name"
                 android:icon="@drawable/icon">

        <!-- Tell NativeActivity the name of our .so -->
        <meta-data android:name="android.app.lib_name" android:value="cocos2djs" />

        <activity android:name="org.cocos2dx.javascript.AppActivity"
                  android:label="@string/app_name"
                  android:screenOrientation="portrait"
                  android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
                  android:configChanges="orientation">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
    </application>

    <supports-screens android:anyDensity="true"
                      android:smallScreens="true"
                      android:normalScreens="true"
                      android:largeScreens="true"
                      android:xlargeScreens="true"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>  
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>  
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>  
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>  
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
</manifest> 

AppActivity.javaに広告表示処理を追記

path_to_project/frameworks/runtime-src/proj.android/src/org/cocos2dx/javascrpt/AppActivity.javaに広告表示処理を追記する。

以下例。 IMOBILE_BANNER_PIDIMOBILE_BANNER_MIDIMOBILE_BANNER_SIDはi-mobileから発行されるため、自身のIDを使用してください。

/****************************************************************************
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2011      Zynga Inc.
Copyright (c) 2013-2014 Chukong Technologies Inc.
 
http://www.cocos2d-x.org

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
package org.cocos2dx.javascript;

import org.cocos2dx.lib.Cocos2dxActivity;
import org.cocos2dx.lib.Cocos2dxGLSurfaceView;
import jp.co.imobile.sdkads.android.ImobileSdkAd;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.view.Gravity;
import android.R;

public class AppActivity extends Cocos2dxActivity {
    static final String IMOBILE_BANNER_PID="xxxx";
    static final String IMOBILE_BANNER_MID="xxxxx";
    static final String IMOBILE_BANNER_SID="xxxxx";
    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // スポット情報を設定します
        ImobileSdkAd.registerSpotInline(this, IMOBILE_BANNER_PID, IMOBILE_BANNER_MID, IMOBILE_BANNER_SID);
        // 広告の取得を開始します
        ImobileSdkAd.start(IMOBILE_BANNER_SID);
        
        // 広告を表示するViewを作成します
        FrameLayout imobileAdLayout = new FrameLayout(this);
        FrameLayout.LayoutParams imobileAdLayoutParam = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        // 広告の表示位置を指定
        imobileAdLayoutParam.gravity = (Gravity.BOTTOM | Gravity.CENTER);
        //広告を表示するLayoutをActivityに追加します
        addContentView(imobileAdLayout, imobileAdLayoutParam);
        // 広告を表示します
        ImobileSdkAd.showAd(this, IMOBILE_BANNER_SID, imobileAdLayout);
    }
    @Override
    protected void onDestroy() {
        // Activity廃棄時の後処理
        ImobileSdkAd.activityDestory();
        super.onDestroy();
    }

    @Override
    public Cocos2dxGLSurfaceView onCreateView() {
        Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this);
        // TestCpp should create stencil buffer
        glSurfaceView.setEGLConfigChooser(5, 6, 5, 0, 16, 8);

        return glSurfaceView;
    }
}

注意

表示まで時間がかかる場合があります。App承認メール後数時間で表示されるようになりました。

大変でした。