一個是cocos2d-x庫文件,還有一個runtime-src這裏放着各個平臺的工程文件還有一個classes文件夾java
B. 遊戲須要的Classes文件android
有三個平臺,android,mac,winapache
只看android工程文件proj.android-studiojson
可使用android studio打開這個工程android-studio
打開build.gradle文件,架構
externalNativeBuild是用來配置生成so包的cpu架構app
externalNativeBuild { ndkBuild { if (!project.hasProperty("PROP_NDK_MODE") || PROP_NDK_MODE.compareTo('none') != 0) { // skip the NDK Build step if PROP_NDK_MODE is none targets 'cocos2djs' arguments 'NDK_TOOLCHAIN_VERSION=4.9' def module_paths = [project.file("../../../cocos2d-x"), project.file("../../../cocos2d-x/cocos"), project.file("../../../cocos2d-x/external")] if (Os.isFamily(Os.FAMILY_WINDOWS)) { arguments 'NDK_MODULE_PATH=' + module_paths.join(";") } else { arguments 'NDK_MODULE_PATH=' + module_paths.join(':') } arguments '-j' + Runtime.runtime.availableProcessors() abiFilters.addAll(PROP_APP_ABI.split(':').collect{it as String}) } } } }
// Specifies which native libraries to build and package for this // product flavor. If you don't configure this property, Gradle // builds and packages all shared object libraries that you define // in your CMake or ndk-build project. targets 'cocos2djs'
這個targets參數,指定哪一個native庫用來構建和打包,若是沒有配置這個屬性,gradle會 將全部native庫進行構建和打包,工具
猜想應該是調用cocos2djs這個底層庫進行編譯gradle
arguments是gradle配置ndk參數的方法,用來設置ndk-build打包時的參數ui
最重要的是abiFilters這個過濾,規定須要打包的CPU架構,
abiFilters.addAll(PROP_APP_ABI.split(':').collect{it as String})
PROP_APP_ABI屬性是從Android工程的gradle.properties文件中獲取到了
PROP_TARGET_SDK_VERSION=19
PROP_APP_ABI=armeabi-v7a:x86:arm64-v8a
表示以:分割,
這樣編譯後會出來三個架構的so文件
C,得到so文件
咱們想要的so文件就出來了,只要編譯出來的遊戲能夠正常運行,咱們就能夠提取這個so文件。
note:須要提取的是ndkBuild-》release->...->armeabi-v7a中的so文件,上面截圖中的是調式的so
如今咱們須要的資源都已經準備好的,能夠進行工程的組合
,保險起見,咱們新建一個文件夾進行android工程簡化。
好比一個籃球遊戲,新建basketball文件夾
將as工程全部文件複製到basketball中,注意,紅框中的文件不須要修改,
一. 修改app下的文件,將上面標註的A js資源文件放到assets目錄中
二. 將C中得到的so文件放到libs包對應cpu架構下面
還有須要將cocos依賴的jar包也放到lib下面
D:\android-workspace\frameworks\cocos2d-x\cocos\platform\android\java\libs
這四個jar包放到咱們app的libs文件下
三。 添加額外的java 的cocos庫文件
須要將libs源碼文件提取出來放到咱們的app/src/main/java中
目錄路徑
D:\android-workspace\frameworks\cocos2d-x\cocos\platform\android\java\src\org\cocos2dx\lib
複製到src.main.java
四. 修改app/build.gradle文件
主要就是刪除掉打包編譯so文件的代碼,去掉無用的依賴庫
修改前
import org.apache.tools.ant.taskdefs.condition.Os apply plugin: 'com.android.application' android { compileSdkVersion PROP_COMPILE_SDK_VERSION.toInteger() buildToolsVersion PROP_BUILD_TOOLS_VERSION defaultConfig { applicationId "com.uelink.JewelBlockPuzzle" minSdkVersion PROP_MIN_SDK_VERSION targetSdkVersion PROP_TARGET_SDK_VERSION versionCode 1 versionName "1.0" externalNativeBuild { ndkBuild { if (!project.hasProperty("PROP_NDK_MODE") || PROP_NDK_MODE.compareTo('none') != 0) { // skip the NDK Build step if PROP_NDK_MODE is none targets 'cocos2djs' arguments 'NDK_TOOLCHAIN_VERSION=4.9' def module_paths = [project.file("../../../cocos2d-x"), project.file("../../../cocos2d-x/cocos"), project.file("../../../cocos2d-x/external")] if (Os.isFamily(Os.FAMILY_WINDOWS)) { arguments 'NDK_MODULE_PATH=' + module_paths.join(";") } else { arguments 'NDK_MODULE_PATH=' + module_paths.join(':') } arguments '-j' + Runtime.runtime.availableProcessors() abiFilters.addAll(PROP_APP_ABI.split(':').collect{it as String}) } } } } sourceSets.main { java.srcDir "src" res.srcDir "res" jniLibs.srcDir "libs" manifest.srcFile "AndroidManifest.xml" } externalNativeBuild { ndkBuild { if (!project.hasProperty("PROP_NDK_MODE") || PROP_NDK_MODE.compareTo('none') != 0) { // skip the NDK Build step if PROP_NDK_MODE is none 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 } externalNativeBuild { ndkBuild { arguments 'NDK_DEBUG=0' } } } debug { externalNativeBuild { ndkBuild { arguments 'NDK_DEBUG=1' } } } } } android.applicationVariants.all { variant -> // delete previous files first delete "${buildDir}/intermediates/assets/${variant.dirName}" variant.mergeAssets.doLast { copy { from "${buildDir}/../../../../../res" into "${buildDir}/intermediates/assets/${variant.dirName}/res" } copy { from "${buildDir}/../../../../../src" into "${buildDir}/intermediates/assets/${variant.dirName}/src" } copy { from "${buildDir}/../../../../../jsb-adapter" into "${buildDir}/intermediates/assets/${variant.dirName}/jsb-adapter" } copy { from "${buildDir}/../../../../../main.js" from "${buildDir}/../../../../../project.json" into "${buildDir}/intermediates/assets/${variant.dirName}" } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation fileTree(dir: "../../../cocos2d-x/cocos/platform/android/java/libs", include: ['*.jar']) implementation project(':libcocos2dx') }
修改後
import org.apache.tools.ant.taskdefs.condition.Os apply plugin: 'com.android.application' android { compileSdkVersion PROP_COMPILE_SDK_VERSION.toInteger() buildToolsVersion PROP_BUILD_TOOLS_VERSION defaultConfig { applicationId "com.uelink.JewelBlockPuzzle" minSdkVersion PROP_MIN_SDK_VERSION targetSdkVersion PROP_TARGET_SDK_VERSION versionCode 1 versionName "1.0" } sourceSets.main { java.srcDir "src" res.srcDir "res" jniLibs.srcDir "libs" manifest.srcFile "AndroidManifest.xml" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' externalNativeBuild { ndkBuild { arguments 'NDK_DEBUG=0' } } } debug { externalNativeBuild { ndkBuild { arguments 'NDK_DEBUG=1' } } } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) }
可能還須要修改下java文件包名,修改寫報錯,程序就能夠正常運行了。