解决CocosCreator打包Android时的“ no rule to make target cocos2djs ” 问题

感谢YF Lu同学的耐心测试以及详细整理!这里分享并记录一下问题的解决方案和一些原因

CocosCreator Android building troublesh...

Android env setup and building panel

First make sure that you have working "Android SDK+NDK" combination setup in the "overall CocosCreator Settings". 在这里插入图片描述Second, mind some settings in the build panel. 在这里插入图片描述 When building for "Android",** shall I use "default" or "link" template?** Actually either works, but you should tailor-make the "build-template folder" for the chosen template.


Gradle version

Use a matching Gradle version

Make sure that you use "Matching CocosCreator version & Gradle version", e.g. "CocosCreator or v2.1.3 only works with Gradle-4.x". Note that "AndroidStudio" could use a different Gradle version than that specified by "GradleWrapper", to be sure about the pipeline you can always build with your "GradleWrapper". 在这里插入图片描述

What if you didn't use a matching Gradle version

If due to an unknown reason your built Android project just comes up with an unmatching Gradle version, you might get a "no rule to make target cocos2djs" error.

Such problem could occur after blind clicked "upgrade" in AndroidStudio, which may overwrite your "project.android-studio/build.grade".

Another case showed that the same error might also show up after a newly created "HelloWorld" project (from CocosCreator v2.1.3 welcome pane, no build-template customization, no CocosCreator folder src code customization, even without opening AndroidStudio). In this case the resulted "proj.android-studio/build.gradle" uses a "com.android.tools.build:gradle:4.x.x (the AndroidGradlePlugin)", and the automatically chosen "GradleWrapper version" will be "5.x" (the actual Gradle).

There were some build warning like "deprecared variant.mergeAssets", not sure whether it's relevant to this issue. 在这里插入图片描述在这里插入图片描述


Solution

You cannot just change the "distributionurl" in "proj.android-studio/gradle/wrapper/gradle-wrapper.properties" to use "another Gradle version", which will only result in yet some other building issues.

The currently working solution is to create a "Gradle version constraint build-template file", as shown in the screenshot below, to specify version constraints. 在这里插入图片描述

Building from CLI

To see explicit warnings of the use of "obsolete APIs". bashshell> /path/to/frontend/build/jsb-link/frameworks/runtime- src/proj.android-studio (master) $ ./gradlew build -Pandroid.debug.obsoleteApi=true --warning-mode all 在这里插入图片描述