Help us understand the problem. What is going on with this article?

cocos2d-x 3.4 Androidのビルド設定

More than 3 years have passed since last update.

メモ。
昔と比べると大分楽になった気がする。

いい加減、脱PluginWrapperしたかったので、Android.mkで直接plugin/protocols/を取り込むようにして、runOnUiThreadはAppActivityのシングルトンインスタンスで実行させるように変更した。

環境

  • Mac OS 10.10.2
  • cocos2d-x 3.4
  • Eclipse LUNA Service Release 1a (4.4.1)
  • Android NDK r10d
  • ADT 23.0.6.1720515
  • Android SDK 24.1.2

依存プロジェクトの参照追加

プロジェクト > properties > Android
Libraryの項目で下記プロジェクトの参照を追加。

  • libcocos2dx
  • google-play-service_lib(広告表示で使う)

Android サポートライブラリの追加

プロジェクト > Android Tools > Add Support Library...

Manifestの設定

下記記述を追加。

・広告関連ブロックとパーミッション
・android:minSdkVersionの指定
・オリエンテーションの指定

ant.propertiesの編集

鍵の設定。

and.properties
+key.alias.password=my_fantastic_app_key
+key.store.password=my_fantastic_app_key
+key.store=/Users/Smith/keys/apk/my_fantastic_app/my_fantastic_app_key
+key.alias=my_fantastic_app_key

鍵の作成はこんな感じで。

keytool -genkey -v -keystore /Users/Smith/keys/apk/my_fantastic_app/my_fantastic_app_key -alias my_fantastic_app_key -keyalg RSA -validity 10000

Android.mkの編集

使用するモジュールと必要なc++ファイルの注入。
今回のアプリはこんな感じ。
Application.mkの編集は不要だった。

Android.mk
-LOCAL_SRC_FILES := hellocpp/main.cpp \
-                   ../../Classes/AppDelegate.cpp \
-                   ../../Classes/HelloWorldScene.cpp
+JNI_CPP_FILES             := $(shell find $(LOCAL_PATH) -name *.cpp)
+APP_CPP_FILES             := $(shell find $(LOCAL_PATH)/../../Classes -name *.cpp)
+PLUGIN_PROTOCOL_CPP_FILES := $(shell find $(LOCAL_PATH)/../../cocos2d/plugin/protocols -name *.cpp)

-LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes
+LOCAL_SRC_FILES := sqlite3.c
+LOCAL_SRC_FILES += $(APP_CPP_FILES:$(LOCAL_PATH)/%=%)
+LOCAL_SRC_FILES += $(JNI_CPP_FILES:$(LOCAL_PATH)/%=%)
+LOCAL_SRC_FILES += $(PLUGIN_PROTOCOL_CPP_FILES:$(LOCAL_PATH)/%=%)
+
+LOCAL_C_INCLUDES := $(shell find $(LOCAL_PATH)/../../Classes -type d)
+LOCAL_C_INCLUDES += $(shell find $(LOCAL_PATH) -type d)
+LOCAL_C_INCLUDES += $(shell find $(LOCAL_PATH)/../../cocos2d/plugin/protocols -type d)

 # _COCOS_HEADER_ANDROID_BEGIN
 # _COCOS_HEADER_ANDROID_END


 LOCAL_STATIC_LIBRARIES := cocos2dx_static
+LOCAL_STATIC_LIBRARIES += android_native_app_glue
+LOCAL_WHOLE_STATIC_LIBRARIES += cocosdenshion_static
+LOCAL_WHOLE_STATIC_LIBRARIES += cocos2d_network_static
+LOCAL_WHOLE_STATIC_LIBRARIES += cocos2d_extension_static

 # _COCOS_LIB_ANDROID_BEGIN
 # _COCOS_LIB_ANDROID_END

 include $(BUILD_SHARED_LIBRARY)

 $(call import-module,.)
+$(call import-module,android/native_app_glue)
+$(call import-module,audio/android)
+$(call import-module,network)
+$(call import-module,extensions)

main.cppの編集

jniを利用するためにJavaVMを渡してあげる。

main.cpp
 #include "platform/android/jni/JniHelper.h"
 #include <jni.h>
 #include <android/log.h>
+#include "PluginJniHelper.h"

 #define  LOG_TAG    "main"
 #define  LOGD(...)  __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)

 using namespace cocos2d;

void cocos_android_app_init (JNIEnv* env, jobject thiz) {
    LOGD("cocos_android_app_init");
     AppDelegate *pAppDelegate = new AppDelegate();
+
+    JavaVM* vm;
+    env->GetJavaVM(&vm);
+    PluginJniHelper::setJavaVM(vm);
 }

AppActivity.javaの編集

AppActivity.java
+import org.cocos2dx.lib.Cocos2dxGLSurfaceView;
+import android.os.Bundle;
+import android.widget.RelativeLayout;

public class AppActivity extends Cocos2dxActivity {
+    // メインスレッド (runOnUiThread)の処理実行に使う
+    static AppActivity instance;
+    // 広告とかの箱
+    RelativeLayout contentLayout;
+    
+    @Override protected void onCreate(final Bundle savedInstanceState)
+    {
+        super.onCreate(savedInstanceState);
+        
+        instance = this;
+        contentLayout = new RelativeLayout(instance);
+        instance.addContentView(contentLayout, new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
+    }

+    // ClippingNodeとかで地味に効いてくる設定
+    @Override public Cocos2dxGLSurfaceView onCreateView()
+    {
+        Cocos2dxGLSurfaceView glsView = super.onCreateView();
+        glsView.setEGLConfigChooser(5, 6, 5, 0, 16, 8);
+        
+        return glsView;
+    }
}

以上。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした