3.10 and Android Studio

Nice to see a new project created with 3.10 creates what looks to be a project for Android Studio. How should I open it from within Android Studio, and do I have to do anything more to make it compile and run?

thanks

2 Likes

just watch this video and you are set:

or read this brief explanation of the steps required:

1)- download android studio the full version
2)- create a cocos2d-x project
3)- compile it from the terminal with this command

if you create an ARM virtual machine use this command(not recommended, its slow)

cocos compile -p android --android-studio

if you create a x86 virtual machine use this command(recommended, its fast):

cocos compile -p android --android-studio --app-abi x86

4)- import the file ā€œproj.android-studioā€ into android-studio
5)- create an android virtual machine(x86 or ARM)
6)- run the android virtual machine
7)- run the project from within android-studio

PS: you should read my comment in this post on how to automate the compilation process so you donā€™t have to type the command in the command line every time you wanna compile:


PS: remember google is your friend!

6 Likes

This is an excellent reply. Thank you! Iā€™m still having issues running my game on a device from Android Studio. After following your instructions, and pressing the ā€˜Runā€™ button in AS, I get the error:

java.lang.UnsatisfiedLinkError: Couldn't load cocos2dlua from loader dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.upstartillustration.GameKit-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.upstartillustration.GameKit-1, /vendor/lib, /system/lib]]]: findLibrary returned null

Android Studio: v1.5.1
Cocos2d-x: v3.10
NDK: r10d

Any help would be appreciated. It looks like all I need to do is figure out why itā€™s not able to find the cocos2dlua binary.

Iā€™m starting to think the --android-studio option doesnā€™t do anything. When I attempt to run cocos run, instead of cocos compile, it runs on the device with no issues. The problem is, itā€™s not executing the code in the proj.android-studio folder. For instance, I modified the AppActivity.java class inside the proj.android-studio/app/src/org/cocos2dx/lua/AppActivity.java and it didnā€™t run the Log.d command I was using to test if it was running the code. It only compiles and moves the APK to the runtime-src/simulator directory. Also, I see that the settings.gradle file is configured to load the libcocos2dx module, but I donā€™t see where it would ever attempt to find the cocos2dlua libraryā€¦ as there isnā€™t anything with that name anywhere in the build directories.

The main differences I see with my environment, compared to the video, is that heā€™s running cocos compile from the base directory. I must run the script from frameworks/runtime-src or it says that the ā€˜androidā€™ platform is not supported. Only ā€˜macā€™ and ā€˜iosā€™. Iā€™m at a loss. Any help would be appreciated.

I think I may have figured it out. When I first created the project it failed to create a .cocos-project.json. I needed to add a key/value pair in the .cocos-project.json file to ensure it was using Android Studio.

This is what my config file looks like now:

{
    "has_native": true,
    "project_type": "lua",
    "android_cfg": {
        "project_path": "proj.android",
        "studio_proj_path": "proj.android-studio"
    }
}

It now builds OK. Iā€™ll know in a while if all is well.

Update
Works great now!

I have issued the following command and apk is generated.

cocos compile -p android --android-studio --app-abi x86

But it is not running on x86 AVD. Could you please tell me how to fix?

This topic is temporarily closed due to a large number of community flags

Hello Joseph39,

Thanks for your guide. It really kool. I follow it and build my app successfully.

But i have one more question. When build so with command:

cocos compile -p android --android-studio

This seem debug mode. How we build in release mode?

Regrads,
Tai Hai

Hello,

I found the answer is: cocos compile -p android -m release --android-studio.

Have a nice day.

2 Likes

I get error
/Users/johnzakharov/Desktop/cocos2d/naOptovke/cocos2d/cocos/platform/android/jni/TouchesJni.cpp:47: error: undefined reference to ā€˜__cxa_throw_bad_array_lengthā€™
/Users/johnzakharov/Desktop/cocos2d/naOptovke/cocos2d/cocos/platform/android/jni/TouchesJni.cpp:48: error: undefined reference to ā€˜__cxa_throw_bad_array_lengthā€™
/Users/johnzakharov/Desktop/cocos2d/naOptovke/cocos2d/cocos/platform/android/jni/TouchesJni.cpp:49: error: undefined reference to ā€˜__cxa_throw_bad_array_lengthā€™
/Users/johnzakharov/Desktop/cocos2d/naOptovke/cocos2d/cocos/platform/android/jni/TouchesJni.cpp:55: error: undefined reference to ā€˜__cxa_throw_bad_array_lengthā€™
collect2: error: ld returned 1 exit status
make: *** [obj/local/armeabi/libMyGame.so] Error 1
make: Leaving directory `/Users/johnzakharov/Desktop/cocos2d/naOptovke/proj.android-studio/appā€™