I am using Cocos2D 3.13.1.
Same here. Iām very interested too in using prebuilt libs with Android Studio.
So, I just created project:
cocos new TestLibs -p com.test.libs -l cpp -t binary
And there is no proj.android-studio but only proj.android folder. How properly import it into AS?
Try to summon @piotrros here - are you using AS with latest cocos2d-x and prebuilt libs?
Nah, but Iām still trying:
Xmm, so is it working now that you can compile and debug in Android studio c++ code for android? or you trying cmake stuff?
Iām using ndkBuild. Yes - I can debug. Now (see topic above) I finally got it working with precompiled libs. Compilation is then much faster and I can still debug.
One import thing to get debugging working:
for releasing app you only need:
abiFilters āarmeabiā
as adding more abis are increasing apk size and compilation time.
But, in my case, I own Nexus 6P with arm64-v8a so I have to add it to abiFilters in order to get debugging working.
Iām saying about c++ debugging, because java debugging will work anyway.
Any how-to step by step for newbies?
Iāve just created a new project using:
cocos new ProjectName -p projectname.com.name -l cpp -t binary
Configured it for build with prebuilt libs for iOS in Xcode. I even have it with source code, because I added pathās for cocos2d sources for header search path option.It compiles with prebuilt libs in no time and at the same time I have debug with source code of cocos2d. Cool.
Now, what should I do to set up it for Android Studio? There even no project folder created for itā¦
Make sure you have cocos2d-x 3.14 and NDK r13b properly installed and paths configured.
cocos -v
cocos2d-x-3.14
Cocos Console 2.2
- make precompiled libs:
cocos gen-libs -p android --ap android-10
if needed:
cocos gen-libs -p ios
cocos gen-libs -p mac
- create new project and donāt use -t binary:
cocos new ProjectName -l cpp
- remove cocos2d directory (you donāt need that):
cd ProjectName
rm -rf cocos2d
- update gradle version in build.gradle file (top directory):
classpath ācom.android.tools.build:gradle:2.2.3ā
- change libcocos2dx subproject path in settings.gradle (top directory):
project(ā:libcocos2dxā).projectDir = new File(ā/YOUR_PATH/cocos2d-x-3.14/cocos/platform/android/libcocos2dxā)
- edit project.properties file (app directory):
target=android-10
-
update build.gradle file (app directory):
apply plugin: ācom.android.applicationā
android {
compileSdkVersion 23
buildToolsVersion ā23.0.3ādefaultConfig { applicationId "org.cocos2dx.ProjectName" minSdkVersion 10 targetSdkVersion 23 versionCode 1 versionName "1.0" ext { cocospath = "/YOUR_PATH/cocos2d-x-3.14" } externalNativeBuild { ndkBuild { targets "MyGame" arguments "NDK_MODULE_PATH=$cocospath:$cocospath/cocos:$cocospath/external:$cocospath/cocos/prebuilt-mk:$cocospath/extensions" arguments "-j" + Runtime.runtime.availableProcessors() abiFilters "armeabi" }
// available abiFilters (for debugging):
// abiFilters āx86ā, āarmeabiā, āarmeabi-v7aā, āarm64-v8aā} } sourceSets.main { java.srcDir "src" res.srcDir "res" jniLibs.srcDir "libs" manifest.srcFile "AndroidManifest.xml" assets.srcDir "assets" } externalNativeBuild { ndkBuild { path "jni/Android.mk" } } signingConfigs { release { if (project.hasProperty("RELEASE_STORE_FILE")) { storeFile file(RELEASE_STORE_FILE) storePassword RELEASE_STORE_PASSWORD keyAlias RELEASE_KEY_ALIAS keyPassword RELEASE_KEY_PASSWORD } } } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' if (project.hasProperty("RELEASE_STORE_FILE")) { signingConfig signingConfigs.release } } }
}
dependencies {
compile fileTree(dir: ālibsā, include: [ā*.jarā])
compile project(ā:libcocos2dxā)
}task cleanAssets(type: Delete) {
delete āassetsā
}
task copyAssets(type: Copy) {
from āā¦/ā¦/Resourcesā
into āassetsā
}clean.dependsOn cleanAssets
preBuild.dependsOn copyAssets -
change gradle wrapper version in gradle/wrapper/gradle-wrapper.properties like this:
distributionUrl=https://services.gradle.org/distributions/gradle-2.14.1-all.zip
- In app/jni/Android.mk file remove these:
$(call import-add-path,$(LOCAL_PATH)/ā¦/ā¦/ā¦/cocos2d)
$(call import-add-path,$(LOCAL_PATH)/ā¦/ā¦/ā¦/cocos2d/external)
$(call import-add-path,$(LOCAL_PATH)/ā¦/ā¦/ā¦/cocos2d/cocos)
$(call import-add-path,$(LOCAL_PATH)/ā¦/ā¦/ā¦/cocos2d/cocos/audio/include)
Now open proj.android-studio in Android Studio and enjoy.
If you want to debug c++ make sure youāve added to abiFilters architecture, which your device is working on (for example arm64-v8a is needed for Nexus 6P). To find .cpp files expand ProjectName ā cpp ā Classes.
Iāve also attached example blank project, which Iāve created to write this post (and itās working).
ProjectName.zip (6.0 MB)
Thanks a lot for this. It just saved me a few hours. Also, I had to remove this because the cocos compile returned some weird error that the NDK is not set ĀÆ_(ć)_/ĀÆ. Thanks again!
Ok, it wasnāt working as expected. Whole cocos library was compiled instead. One small change is required:
$(call import-module,./prebuilt-mk)
instead of
$(call import-module,.)
in Android.mk file.
New test project included.
When you open this project in android studio youāll see the difference. Thereās no more bunch of libraries in ProjectName/cpp, just MyGame and cpufeatures (dunno why but itās here).
Iām also switching from armeabi to armeabi-v7a, because non armeabi-v7a are very old and I donāt support them anyway (too old android version for libraries like google play services and facebook).
Now app is compiling for about 18 seconds (about 3mins before) in my case
Also I can debug on arm64-v8a devices with just armeabi-v7a (donāt have to add arm64-v8a in build.gradle which will increase apk size and compilation time).
ProjectName.zip (5.8 MB)
I just get an error āError: Configuration with name ādefaultā not found.ā help
Did you use zip from the last post? Make sure you use latest android ndk and your android.mk file has all necessary .cpp files listed.
Should we download latest android NDK standalone or we should use one that is downloaded by SDK manager?
Both should work, but Iām using the one which is downloaded do SDK manager.
if i use SDK manager downloaded NDK, should i provide NDK path in bash_profile? and from where i will get the path to this NDK?
Yes, you should put something like this: export NDK_ROOT=/your_path_to_sdk/ndk-bundle
in .bash_profile
For me NDK bundle and ANDROID_NDK_HOME are both set to āC:\dev\android-ndk-r13b-windows-x86_64\android-ndk-r13bā and compile works but on Nexux7 debug doesnāt.
Debugging depends on ABI your device have. For example in my Nexus 6P itās arm64-v8, but it works also with armeabi-v7a. However I wonāt be able to debug while using armeabi only. Nexus 7 should work with armeabi-v7a.
it gives error while syncing gradle
Error:(22, 0) Could not find method targets() for arguments [MyGame] on object of type com.android.build.gradle.internal.dsl.NdkBuildOptions.
Strange, did you ran test project I posted above? What version of NDK, Android Studio are you using? Did you download cocos2d-x with that pull request merged? Did you build precompiled libs?