doorxp

Blog

#Cocos Creator# 安卓包运行时找不到settings.js文件?

问题环境

CocosCreator工程,输出Android Studio工程,用Android Studio打开,根据提示升级相关的Gradle:
CocosCreator_Android_Cannot_Find_Settings_js_01_upgrade_gradle

然后打包,安装到安卓手机上,运行测试,咦,怎么闪退了?

如果查看logcat日志,可以看到错误:

CocosCreator_Android_Cannot_Find_Settings_js_02_error

完整的错误信息:

E/jswrapper: ScriptEngine::onGetStringFromFile jsb-adapter/jsb-builtin.js not found, possible missing file.
    ScriptEngine::runScript script jsb-adapter/jsb-builtin.js, buffer is empty!
E/jswrapper: ScriptEngine::onGetStringFromFile src/settings.js not found, possible missing file.
    ScriptEngine::runScript script src/settings.js, buffer is empty!
    [ERROR] Failed to invoke require, location: /Applications/CocosCreator.app/Contents/Resources/cocos2d-x/cocos/scripting/js-bindings/manual/jsb_global.cpp:292
    ScriptEngine::onGetStringFromFile src/cocos2d-jsb.js not found, possible missing file.
    ScriptEngine::runScript script src/cocos2d-jsb.js, buffer is empty!
    [ERROR] Failed to invoke require, location: /Applications/CocosCreator.app/Contents/Resources/cocos2d-x/cocos/scripting/js-bindings/manual/jsb_global.cpp:292
D/vndksupport: Loading /vendor/lib64/hw/android.hardware.graphics.mapper@2.0-impl.so from current namespace instead of sphal namespace.
E/jswrapper: ScriptEngine::onGetStringFromFile jsb-adapter/jsb-engine.js not found, possible missing file.
    ScriptEngine::runScript script jsb-adapter/jsb-engine.js, buffer is empty!
    [ERROR] Failed to invoke require, location: /Applications/CocosCreator.app/Contents/Resources/cocos2d-x/cocos/scripting/js-bindings/manual/jsb_global.cpp:292
D/vndksupport: Loading /vendor/lib64/hw/gralloc.sdm845.so from current namespace instead of sphal namespace.
E/jswrapper: ERROR: Uncaught TypeError: Cannot read property 'debug' of undefined, location: main.js:0:0
    STACK:
    [0]window.boot@main.js:10
    [1]anonymous@main.js:212
    ScriptEngine::evalString script main.js, failed!
A/libc: /Applications/CocosCreator.app/Contents/Resources/cocos2d-x/cocos/scripting/js-bindings/jswrapper/Value.cpp:539: se::Object *se::Value::toObject() const: assertion "isObject()" failed
    Fatal signal 6 (SIGABRT), code -6 in tid 32669 (GLThread 109567), pid 32595 (dowgame.balance)
Application terminated.

这是怎么回事?

原因

首先,检查一下导出的apk包是不是正常的。

直接在Android Studio里,双击打开对应的apk,可以查看包里的内容

果然没有js文件!

CocosCreator_Android_Cannot_Find_Settings_js_03_checking_apk

有什么办法呢?

解决方法

  1. 重新打一个包,然后不升级Gradle,直接打包。

看看这时候的apk包是否正常:
CocosCreator_Android_Cannot_Find_Settings_js_04_correct_apk

居然是正常的,看来是升级Gradle以后的产生的问题。

  1. 如果升级Gradle,需要修改一下build.gradle

android.applicationVariants.all { variant ->
    // delete previous files first
    delete "${buildDir}/intermediates/merged_assets/${variant.dirName}"

    variant.mergeAssets.doLast {
        def sourceDir = "${buildDir}/../../../../.."
        
// 以下几行是旧的代码,在新版Gradle下有问题
//        copy {
//            from "${sourceDir}/res"
//            into "${outputDir}/res"
//        }
//
//        copy {
//            from "${sourceDir}/src"
//            into "${outputDir}/src"
//        }
//
//        copy {
//            from "${sourceDir}/jsb-adapter"
//            into "${outputDir}/jsb-adapter"
//        }

// 新的拷贝文件的方法,在新版Gradle可用
        copy{
            from "${sourceDir}"
            include "res/**"
            include "src/**"
            include "jsb-adapter/**"

            into outputDir
        }

        copy {
            from "${sourceDir}/main.js"
            from "${sourceDir}/project.json"
            into outputDir
        }
    }
}

CocosCreator_Android_Cannot_Find_Settings_js_05_correction

打包测试,这次从打包的临时目录查看一下,文件都在

临时文件目录:xx/app/build/intermediates/merged_assets/debug/out

CocosCreator_Android_Cannot_Find_Settings_js_06_merged_assets

参考

  1. https://docs.gradle.org/current/userguide/working_with_files.html#sec:copying_directories_example


发表评论:

Powered By Z-BlogPHP 1.5.1 Zero

Copyright doorxp.com Rights Reserved.