Problem with plugin integration

I followed the tutorial of [[Plugin-X Integration Guide for Android]],and had problem at the step “Manual modified”(pre-steps finished succesfully).
I added ${cocos2d-x_ROOT}/plugin/publish to build-cfg.json(“ndk_module_path” element),modified JNI_OnLoad method, modified AppActivity.java,compiled using “cocos compile -s *** -p android”,when problem occured.

[javac] D:\Software\AndroidDevelopment\cocos2d-x-3.0\MyCompany\MyGame\proj.a

ndroid\src\org\cocos2dx\cpp\AppActivity.java:31: error: package org.cocos2dx.plu
gin does not exist
[javac] import org.cocos2dx.plugin.PluginWrapper;
[javac] ^

I checked that the jar file was included in “.classpath”,and don’t know why org.cocos2dx.plugin couldn’t be found,anyone could help?

Someone else mentioned in another post that this document is broken. Let me get in front of an engineer to have it looked at. This is going to take a couple days.

I’ve had issues with this as well. My issue is in the main.cpp though
When I try to add
#include "PluginJniHelper.h"
It says it cannot be found.
The Android.mk file was edited correctly, and I managed to get the java file working correctly by initiating the PluginWrapper

This is the only place I’m stuck at. Anyone got any ideas?

I have an engineer working on an answer…

@csimon, Could you check the file .classpath again? Is there a line like this:
<classpathentry exported="true" kind="lib" path="..../publish/protocols/android/libPluginProtocol.jar"/>
Because the java class org.cocos2dx.plugin.PluginWrapper is defined in the libPluginProtocol.jar.
The line mentioned should be added in step Game-developer-guide-tool.

@slackmoehrle, Could you tell me which post mentioned the document is broken? I will check it. Thank you!

@GhostTrigger, I think the problem you mentioned is: The NDK_MODULE_PATH is not set correctly. You should add the directory publish into NDK_MODULE_PATH as mentioned in step : Manual-modified.

Is this wiki entry still relevant with Cocos2d-x v.3.2?
http://www.cocos2d-x.org/wiki/Plugin-X_Integration_Guide_for_Android

I’m having trouble once I reach the “Manual modified” section. Before I continue trying to make this work I’d like confirmation that the Plugin-X_Integration_Guide_for_Android is still relevant. I’m suspicious that it is no longer relevant because it suggests many configuration settings and code changes I don’t find implemented in the HelloPlugins project from GitHub (https://github.com/cocos2d-x/plugin-x/tree/develop/samples/HelloPlugins). Also, are the HelloPlugins samples current for 3.2?

Just a few of the problems I’ve found with the wiki entry:

Modify the ndk-build command parameter : add the publish directory into parameter NDK_MODULE_PATH , such as : NDK_MODULE_PATH=${PLUGIN_ROOT}/publish

— Is this change to be made in Android.mk? (i.e., … /Cocos2d-x/plugin/samples/HelloPlugins/proj.android/jni/Android.mk)
— This change is not seen in Android.mk of HelloPlugins

Add code into the method JNI_OnLoad, like this : …

— To which file is this change made? HelloWorldScene.cpp?
— Because it’s returning a jint I infer that JNI_OnLoad should be added to a JNI area of the code wrapped in an extern “C” { } block. Correct?
#include “PluginJniHelper.h” must be the full path (and not curly quotes, but straight quotes): #include “platform/android/jni/JniHelper.h”
— Where is the const JNI_VERSION_1_4 defined?
— This change is not seen in HelloWorldScene.cpp of HelloPlugins nor in a couple of the Scenes (TestAnalyticsScene.cpp, TestAdsScene.cpp…)

Invoke method PluginWrapper.init() when the main activity of the game is created…

— I infer that this change is made in this file: … /Cocos2d-x/plugin/samples/HelloPlugins/proj.android/src/org/cocos2dx/HelloPlugins/AppActivity.java
— It seems like PluginManager is used in the HelloPlugins project instead of PluginWrapper.

At this point in my efforts there seems to be enough evidence that the wiki page on Project-X is outdated. Can anyone tell me where to find the best current sample project or tutorial for using Plugin-X for things like Google Ads, Analytics, IAP, etc.?

My system is properly configured to build Cocos2d-x projects (I can build a basic project and play it on my Android phone), however I’m unable to build the HelloPlugins project. I can post the errors I get if needed. I have even followed the steps in Plugin-X_Integration_Guide_for_Android up to the “Manual modified” section. I wonder if the HelloPlugins project may also be outdated and I’d like some input from the community.

BTW:
I cloned the wiki GitHub repository so I could make notes and update the errors I found: https://github.com/chukong/cocos-docs

According to this doc the protocol is to clone the wiki github repo, make changes, then submit a pull request:
http://www.cocos2d-x.org/docs/README

As near as I can determine, here is the github location of the doc I want to edit, however I only find the zh version not the en version:
https://github.com/chukong/cocos-docs/tree/master/manual/framework/native/wiki/plugin-x-integration-guide-for-android

Are the wiki docs on GitHub current? Where can I find the en version of the wiki entry for “plugin-x-integration-guide-for-android”?

  1. Is that path correct or is it just …/publish or …/…/publish? I’m not familiar with what 4 dots would do in a path.

  2. Here’s what I find in .classpath:

But, there’s no plugin-x folder in my directory structure, so I wonder if this is an error. Should it be this instead?:

I think csimon was referring to a different version of Cocos2d. In the document describing Plugin-X integration for Cocos2d-x there is no mention of the build-cfg.json change. However, it is discussed in the Cocos2d-js Plugin-X guide here:
http://www.cocos2d-x.org/docs/manual/framework/html5/jsb/plugin-x/how-to-use-plugin-x-on-android/en

  1. Other users have reported that no changes are required to the build_native.py script with 3.3rc0. The documentation is not clear about exactly what changes are required. If changes to build_native.py are still required, would you provide clear instructions?

Thank You,

You said you’re editing main.cpp, but I’ve been trying to edit the JNI_OnLoad method in /cocos2d/cocos/platform/android/javaactivity.cpp.

main.cpp does not include a JNI_OnLoad method. did you add one?

javaactivity.cpp does include a JNI_OnLoad method, but I can’t include PluginJniHelper.h in it.

The documentation about this only says the following. It doesn’t say which of the 2 files to edit:
“Add code into the method JNI_OnLoad, like this :”

This is how I’ve been setting up Plugin-X JNI_OnLoad for the last few versions (3.1 -> 3.3.rc0) . It works:

#include "platform/CCPlatformConfig.h"
#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID

#include "CCApplication-android.h"
#include "CCGLViewImpl-android.h"
#include "base/CCDirector.h"
#include "base/CCEventCustom.h"
#include "base/CCEventType.h"
#include "base/CCEventDispatcher.h"
#include "renderer/CCGLProgramCache.h"
#include "renderer/CCTextureCache.h"
#include "renderer/ccGLStateCache.h"
#include "2d/CCDrawingPrimitives.h"
#include "platform/android/jni/JniHelper.h"

// Have to include this way, otherwise file not found?
#include "../../../plugin/protocols/platform/android/PluginJniHelper.h"

#include <android/log.h>
#include <jni.h>

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

void cocos_android_app_init(JNIEnv* env, jobject thiz) __attribute__((weak));

using namespace cocos2d;

extern "C"
{

jint JNI_OnLoad(JavaVM *vm, void *reserved)
{
    JniHelper::setJavaVM(vm);
    PluginJniHelper::setJavaVM(vm); // For plugins, notice passing the param.
    return JNI_VERSION_1_4;
}

I can confirm import org.cocos2dx.plugin.PluginWrapper; is the proper import used in your src/com/yourcompany/yourgame/YourGame.java.

If it’s not being imported, make sure you have a plugin-x linked resource which points to cocos/plugin. gameDevGuide.sh does this for you, but you need to run publish.sh first.

After a LOT of struggle I am now able to prepare a project with Plugin-X integration and Flurry. Below are all my steps. This is an aggregation of the instructions found in these 2 wiki pages (slightly different, one for Cocos2d-JS and the other for Cocos2d-X) as well as the HelloPlugins project on GitHub.

http://cocos2d-x.org/docs/manual/framework/html5/jsb/plugin-x/how-to-use-plugin-x-on-android/en
http://www.cocos2d-x.org/wiki/Plugin-X_Integration_Guide_for_Android

System:
Cocos2d-x v3.2rc0 (C++)
Mac 10.9.4
Eclipse Luna

STEPS:

================
Create new project: 

cocos new pluginXDemo -p com.mydomain.pluginx -l cpp -d /Users/username/Documents/Cocos2Dx_33rc0_pluginX_Demo

================

From Eclipse, launch Window > Android SDK Manager. 
Install Android 2.1 (API 7) and Android 4.2.2 (API 17) in order to avoid errors when running publish.sh in the next step. 
reference thread: http://discuss.cocos2d-x.org/t/publish-plugins-build-successful-then-build-failed/18693

================

Run publish.sh
/pluginx/cocos2d/plugin/tools/publish.sh

It will take a couple minutes to compile all the plugin JAR files. Verify that after each plugin is compiled you see 2 "BUILD SUCCESSFUL" comments. 

(I assume you're using android-ndk-r9d because the newer v.10 from Google has known problems with the newest Cocos2d-x)

================

I vaguely remember having to install Homebrew before I could run the following gameDevGuide.sh

http://brew.sh/

================

Run gameDevGuide.sh
/pluginx/cocos2d/plugin/tools/gameDevGuide.sh

Select only flurry from the list of plugins to configure

Note, Flurry plugin is currently outdated and sending Events from Android will not work. 
Reference thread: http://discuss.cocos2d-x.org/t/plugin-x-error-and-flurry-android-logevent-problem/17340

================

Open project in Eclipse. 

import the project and libcocos2dx library project as you'd do for any Cocos2d-x project. 

Confirm Project > Build Automatically is selected. 

App will take a couple of minutes to compile when it is first imported. 

================

You will see 1 error in file Android.mk: 
/pluginXDemo/jni/Android.mk

error: 
$(call import-module,protocols/android)

update it to: 
$(call import-module,../../cocos2d/plugin/publish/protocols/android)

Clean project and verify no more Problems. 

================

verify existence of libPluginProtocol.jar, here:
/cocos2d/plugin/publish/protocols/android/libPluginProtocol.jar

================

edit file:
/src/org/cocos2dx/cpp/AppActivity.java

imports: 
import android.os.Bundle;
import org.cocos2dx.plugin.PluginWrapper;
import org.cocos2dx.lib.Cocos2dxGLSurfaceView;

code: 
	// copied from HelloPlugins sample:
	// https://github.com/atapie/cocos2d-x/blob/master/plugin/samples/HelloPlugins/proj.android/src/org/cocos2dx/HelloPlugins/AppActivity.java
	public Cocos2dxGLSurfaceView onCreateView() 
	{
        Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this);
        // TestCpp should create stencil buffer
        glSurfaceView.setEGLConfigChooser(5, 6, 5, 0, 16, 8);
        
        PluginWrapper.init(this);
        PluginWrapper.setGLSurfaceView(glSurfaceView);
        return glSurfaceView;
    }
	
================
	
edit file:
/pluginx/jni/hellocpp/main.cpp

includes: 
#include "PluginJniHelper.h"

code: 
add the following to the existing method cocos_android_app_init:

    JavaVM* vm;
    env->GetJavaVM(&vm);
    PluginJniHelper::setJavaVM(vm);
    

================

verify that you've configured an account on Flurry and have an App ID. 

================

edit file: 
/Classes/HelloWorldScene.h

includes:
#include "ProtocolAnalytics.h"

add a new private variable to store a reference to the Analytics object:
 
private:
		cocos2d::plugin::ProtocolAnalytics* _pluginAnalytics;

=================

edit file: 
/Classes/HelloWorldScene.cpp

includes: 
#include "PluginManager.h"

code: 
add to bottom of method init:

	// Plugin-X
    _pluginAnalytics = dynamic_cast<cocos2d::plugin::ProtocolAnalytics*> (cocos2d::plugin::PluginManager::getInstance()->loadPlugin( "AnalyticsFlurry" ));

//    // enable the debug mode if you prefer
//    _pluginAnalytics->setDebugMode(true);

	// use your App ID from Flurry here:
    _pluginAnalytics->startSession( "12345678901234567890" );
        
===================

verify in Project > Properties > Java Build Path > Libraries tab that the following JAR files are included: 
FluryAgent.jar - pluginXDemo/plugin-x/plugins/flurry/android
libPluginFlurry.jar - /pluginXDemo/plugin-x/plugins/flurry/android
libPluginProtocol.jar - /pluginXDemo/plugin-x/protocols/android

===================

edit file: 
/pluginXDemo/AndroidManifest.xml

Below this line: 
    <uses-permission android:name="android.permission.INTERNET" />

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

===================

in Terminal, run build_native.py
/proj.android/build_native.py

This will take a few minutes on the first run, then will go faster on subsequent runs

===================

Test on Device

in Eclipse, right-click project, select Run As > Android Application. Select your device.

Verify in LogCat that everything seems OK.

Verify in Flurry web console that a session was logged when the app launched.  It may take some time to show up in the console. 

====================

rejoice.

Ok Everyone,
for those who want to use Plugin-X, PLEASE DONT USE COCOS CONSOLE for android compilation. I dont know why but cocos console cant detect package org.cocos2dx.plugin. If you use cocos console you’ll find this error: “package org.cocos2dx.plugin does not exist”

Please use the old method, compile the project using “build_native.py” and run the project through eclipse.
I have successfully implemented plugin-x Facebook for my project through this old method. for those who still failed, please ask me anytime.

Thank you

1 Like

That is sad. I don’t like eclipse. Is there anyway to use ant to build without cocos console? I am having trouble to get plugin work.

I dont know the other option. like or not, chukong tech still using eclipse for the project.
Maybe you can take a look on this.
Facebook Plugin-X Problems Android Eclipse Cocos2d-x 3.3rc1 - Please Help!

Hello EMebane, whatever steps you have specified I have already done that. But in Android.mk I was using
$(call import-module,plugin/protocols/proj.android/jni)
instead of
$(call import-module,…/…/cocos2d/plugin/publish/protocols/android).

On building project, it does not throw any error. But when i add cocos2d::plugin::FacebookAgent::getInstance() in my class file, it says " ‘facebook’ is not declared in the scope."

Then i changed Android.mk to “$(call import-module,…/…/cocos2d/plugin/publish/protocols/android)” and tried to build. It removes facebook declaration error but it gets failed while building with below error:
****ERROR:C:\Users\user\Downloads\Cocos2dx\Cocos_Workspace\FacebookLoginDemo\proj.android…/cocos2d/cocos/…/…/cocos2d/plugin/publish/protocols/android/Android.mk:PluginProtocolStatic: LOCAL_SRC_FILES points to a missing file
C:/Users/user/Downloads/Cocos2dx/android-ndk-r10d/build/core/prebuilt-library.mk:45: *** Android NDK: Aborting . Stop

Can you suggest me whats wrong with the code?
Thanks in advance.