当前位置:   article > 正文

gradlew打包Android环境搭建

gradlew
  1. cd frameworks\runtime-src\proj.android-studio
  2. gradlew

gradlew命令其实运行的是proj.android-studio目录下的gradlew.bat脚本

1.安装Java和下载gradlew

第一次会提示没有java

  1. ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
  2. Please set the JAVA_HOME variable in your environment to match the
  3. location of your Java installation.

Java下载页面,我选择的是jdk8,安装完毕,检查下Java是否正确安装

https://upload-images.jianshu.io/upload_images/3375785-467cb4d14b90119b.png?imageMogr2/auto-orient/strip|imageView2/2/w/548/format/webp
重新执行gradlew,会下载需要的gradle

https://upload-images.jianshu.io/upload_images/3375785-c00242fc2a9a58c2.png?imageMogr2/auto-orient/strip|imageView2/2/w/661/format/webp

2.配置SDK

运行失败,提示

> SDK location not found. Define location with sdk.dir in the local.properties file or with an ANDROID_HOME environment variable.

我使用的是android-sdk_r24.4.1-windows

定义local.properties文件,或者定义ANDROID_HOME环境变量

我选择增加local.properties文件,需要注意路径的书写方式

sdk.dir=D://dev/android-sdk-windows

3.下载项目使用的SDK platform 和 build-tools版本

再次执行gradlew

  1. > You have not accepted the license agreements of the following SDK components:
  2. [Android SDK Build-Tools 26.0.2, Android SDK Platform 29].
  3. Before building your project, you need to accept the license agreements and complete the installation of the missing components using the Android Studio SDK Manager.
  4. Alternatively, to learn how to transfer the license agreements from one workstation to another, go to http://d.android.com/r/studio-ui/export-licenses.html

提示指定版本的sdk的license有问题,我选择了重新安装来绕过了这个问题
运行SDK Manager.exe,重新安装下build-toolssdk platform

https://upload-images.jianshu.io/upload_images/3375785-7d80b1683ad47af2.png?imageMogr2/auto-orient/strip|imageView2/2/format/webp
提示信息和项目配置对应的

https://upload-images.jianshu.io/upload_images/3375785-d886cfc0b6fbd44c.png?imageMogr2/auto-orient/strip|imageView2/2/format/webp

4. 配置NDK

再次执行gradlew,提示ndk没有配置

  1. > NDK not configured.
  2. Download it with SDK manager

查阅资料得知cocos2dx v3.16官方建议使用NDK r14
在local.properties中配置

ndk.dir=D://dev//android-ndk-r14b

再次执行,如果发现报错NDK中缺少ABI,多半是NDK版本混用导致的

> No toolchains found in the NDK toolchains folder for ABI with prefix: aarch64-linux-android

5.配置libcocos2dx需要的SDK

再次执行,发现项目可以正常通过,但是libcocos2dx缺少对应的sdk,同步骤3,安装即可

  1. A problem occurred configuring project ':libcocos2dx'.
  2. > You have not accepted the license agreements of the following SDK components:
  3. [Android SDK Platform 14].

https://upload-images.jianshu.io/upload_images/3375785-91cc83d19f49c1f3.png?imageMogr2/auto-orient/strip|imageView2/2/format/webp

再次执行gradlew

如果你看到如下图所示的build successful,只是说明通过了配置的环境检查

https://upload-images.jianshu.io/upload_images/3375785-474eb5d7dfcc87e1.png?imageMogr2/auto-orient/strip|imageView2/2/format/webp

gradlew的使用

根据上一步的提示:

gradlew --help可以查看详细的命令参数

  1. USAGE: gradlew [option...] [task...]
  2. -?, -h, --help Shows this help message.
  3. -a, --no-rebuild Do not rebuild project dependencies.
  4. -b, --build-file Specify the build file.
  5. --build-cache Enables the Gradle build cache. Gradle will try to reuse outputs from previous builds. [incubating]
  6. -c, --settings-file Specify the settings file.
  7. --configure-on-demand Configure necessary projects only. Gradle will attempt to reduce configuration time for large multi-project builds. [incubating]
  8. --console Specifies which type of console output to generate. Values are 'plain', 'auto' (default) or 'rich'.
  9. --continue Continue task execution after a task failure.
  10. -D, --system-prop Set system property of the JVM (e.g. -Dmyprop=myvalue).
  11. -d, --debug Log in debug mode (includes normal stacktrace).
  12. --daemon Uses the Gradle Daemon to run the build. Starts the Daemon if not running.
  13. --foreground Starts the Gradle Daemon in the foreground. [incubating]
  14. -g, --gradle-user-home Specifies the gradle user home directory.
  15. -I, --init-script Specify an initialization script.
  16. -i, --info Set log level to info.
  17. --include-build Include the specified build in the composite. [incubating]
  18. -m, --dry-run Run the builds with all task actions disabled.
  19. --max-workers Configure the number of concurrent workers Gradle is allowed to use. [incubating]
  20. --no-build-cache Disables the Gradle build cache. [incubating]
  21. --no-daemon Do not use the Gradle Daemon to run the build.
  22. --no-scan Disables the creation of a build scan. (https://gradle.com/build-scans) [incubating]
  23. --offline Execute the build without accessing network resources.
  24. -P, --project-prop Set project property for the build script (e.g. -Pmyprop=myvalue).
  25. -p, --project-dir Specifies the start directory for Gradle. Defaults to current directory.
  26. --parallel Build projects in parallel. Gradle will attempt to determine the optimal number of executor threads to use. [incubating]
  27. --profile Profile build execution time and generates a report in the <build_dir>/reports/profile directory.
  28. --project-cache-dir Specify the project-specific cache directory. Defaults to .gradle in the root project directory.-q, --quiet Log errors only.
  29. --recompile-scripts Force build script recompiling.
  30. --refresh-dependencies Refresh the state of dependencies.
  31. --rerun-tasks Ignore previously cached task results.
  32. -S, --full-stacktrace Print out the full (very verbose) stacktrace for all exceptions.
  33. -s, --stacktrace Print out the stacktrace for all exceptions.
  34. --scan Creates a build scan. Gradle will emit a warning if the build scan plugin has not been applied. (https://gradle.com/build-scans) [incubating]
  35. --status Shows status of running and recently stopped Gradle Daemon(s).
  36. --stop Stops the Gradle Daemon if it is running.
  37. -t, --continuous Enables continuous build. Gradle does not exit and will re-execute tasks when task file inputs change. [incubating]
  38. -u, --no-search-upward Don't search in parent folders for a settings.gradle file.
  39. -v, --version Print version info.
  40. -w, --warn Set log level to warn.
  41. -x, --exclude-task Specify a task to be excluded from execution.

打包

gradlew tasks命令中,我们会发现很多task

  1. ------------------------------------------------------------
  2. All tasks runnable from root project
  3. ------------------------------------------------------------
  4. Android tasks
  5. -------------
  6. androidDependencies - Displays the Android dependencies of the project.
  7. signingReport - Displays the signing info for each variant.
  8. sourceSets - Prints out all the source sets defined in this project.
  9. Build tasks
  10. -----------
  11. assemble - Assembles all variants of all applications and secondary packages.
  12. assembleAndroidTest - Assembles all the Test applications.
  13. assembleDebug - Assembles all Debug builds.
  14. assembleRelease - Assembles all Release builds.
  15. build - Assembles and tests this project.
  16. buildDependents - Assembles and tests this project and all projects that depend on it.
  17. buildNeeded - Assembles and tests this project and all projects it depends on.
  18. clean - Deletes the build directory.
  19. cleanBuildCache - Deletes the build cache directory.
  20. compileDebugAndroidTestSources
  21. compileDebugSources
  22. compileDebugUnitTestSources
  23. compileReleaseSources
  24. compileReleaseUnitTestSources
  25. extractDebugAnnotations - Extracts Android annotations for the debug variant into the archive file
  26. extractReleaseAnnotations - Extracts Android annotations for the release variant into the archive file
  27. mockableAndroidJar - Creates a version of android.jar that's suitable for unit tests.
  28. Build Setup tasks
  29. -----------------
  30. init - Initializes a new Gradle build.
  31. wrapper - Generates Gradle wrapper files.
  32. Help tasks
  33. ----------
  34. buildEnvironment - Displays all buildscript dependencies declared in root project 'proj.android-studio'.
  35. components - Displays the components produced by root project 'proj.android-studio'. [incubating]
  36. dependencies - Displays all dependencies declared in root project 'proj.android-studio'.
  37. dependencyInsight - Displays the insight into a specific dependency in root project 'proj.android-studio'.
  38. dependentComponents - Displays the dependent components of components in root project 'proj.android-studio'. [incubating]
  39. help - Displays a help message.
  40. model - Displays the configuration model of root project 'proj.android-studio'. [incubating]
  41. projects - Displays the sub-projects of root project 'proj.android-studio'.
  42. properties - Displays the properties of root project 'proj.android-studio'.
  43. tasks - Displays the tasks runnable from root project 'proj.android-studio' (some of the displayed tasks may belong to subprojects).
  44. Install tasks
  45. -------------
  46. installDebug - Installs the Debug build.
  47. installDebugAndroidTest - Installs the android (on device) tests for the Debug build.
  48. installRelease - Installs the Release build.
  49. uninstallAll - Uninstall all applications.
  50. uninstallDebug - Uninstalls the Debug build.
  51. uninstallDebugAndroidTest - Uninstalls the android (on device) tests for the Debug build.
  52. uninstallRelease - Uninstalls the Release build.
  53. Verification tasks
  54. ------------------
  55. check - Runs all checks.
  56. connectedAndroidTest - Installs and runs instrumentation tests for all flavors on connected devices.
  57. connectedCheck - Runs all device checks on currently connected devices.
  58. connectedDebugAndroidTest - Installs and runs the tests for debug on connected devices.
  59. deviceAndroidTest - Installs and runs instrumentation tests using all Device Providers.
  60. deviceCheck - Runs all device checks using Device Providers and Test Servers.
  61. lint - Runs lint on all variants.
  62. lintDebug - Runs lint on the Debug build.
  63. lintRelease - Runs lint on the Release build.
  64. lintVitalRelease - Runs lint on just the fatal issues in the release build.
  65. test - Run unit tests for all variants.
  66. testDebugUnitTest - Run unit tests for the debug build.
  67. testReleaseUnitTest - Run unit tests for the release build.

其中Build Tasks中的build是我们需要的打包命令

gradlew build --build-cache

加快打包速度:

--build-cache           Enables the Gradle build cache. Gradle will try to reuse outputs from previous builds. [incubating]

NDK版本的问题: process_begin: CreateProcess failed. make (e=2)

  1. process_begin: CreateProcess(NULL,
  2. D:/dev/android-ndk-r14b/build//../toolchains/arm-linux-androideabi-4.9/prebuilt/windows-x86_64/bin/arm-linux-androideabi-g++
  3. -MMD -MP -MF
  4. .... 我这里做了省略,这里的报错超级超级长
  5. ...) failed.
  6. make (e=2): ?????????????????

CreateProcess给出的报错信息中,使用到了ndk的一个目录下的文件
ndk/toolchains/arm-linux-androideabi-4.9/prebuilt/windows-x86_64/bin/arm-linux-androideabi-g++
实际上,我发现是没有这个文件的

https://upload-images.jianshu.io/upload_images/3375785-51bf2cbf9008f789.png?imageMogr2/auto-orient/strip|imageView2/2/format/webp
然后我下载了r16b解压后发现是有的:

https://upload-images.jianshu.io/upload_images/3375785-072cbb4ab8b6cb90.png?imageMogr2/auto-orient/strip|imageView2/2/w/692/format/webp

Werror=format-security

切换到r16b后再次进行编译

  1. /frameworks/cocos2d-x/cocos/3d/../platform/CCPlatformMacros.h:221:67:
  2. error: format not a string literal and no format arguments [-Werror=format-security]
  3. #define CCLOGERROR(format,...) cocos2d::log(format, ##__VA_ARGS__)
  • 解决办法1:在app/jni/Android.mk中加入,不对prinft的参数进行常量字符串检查

LOCAL_DISABLE_FORMAT_STRING_CHECKS :=true

  • 解决办法2(未验证):app/jni/Application.mk加入

APP_CFLAGS += -Wno-error=format-security
意思是无视这个error

  • 解决办法3(未验证)
    在CMake脚本文件CMakeLists.txt里面添加一行add_definitions (-Wno-format-security)即可。
    这种情况实际是编译器把warining作为error处理了,遇到其他类似情况同样处理,报[-Werror,-WXXX]add_definitions (-Wno-XXX)

不知原因的问题

  1. error: undefined reference to 'vtable
  2. the vtable symbol may be undefined because the class is missing its key function

把构造,析构的实现放在cpp里面就解决了

android studio 下载

下载地址
gradle版本对照表

build.gradle 4.x 需要下载Android Studio3.0

java heap space

gradle.properties修改jvm options即可,堆空间设置的大一点

  1. # Specifies the JVM arguments used for the daemon process.
  2. # The setting is particularly useful for tweaking memory settings.
  3. # Default value: -Xmx10248m -XX:MaxPermSize=256m
  4. # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
  5. org.gradle.jvmargs=-Xmx4096m

真机安装

gradlew installDebug

https://upload-images.jianshu.io/upload_images/3375785-f4da1e40d6cc5499.png?imageMogr2/auto-orient/strip|imageView2/2/format/webp
 

手机系统中有应用阻止了安装

小米手机:在手机的开发者模式中关闭MIUI优化

lint导致打包停止

  1. * What went wrong:
  2. Execution failed for task ':xxxx:lint'.
  3. > Lint found errors in the project; aborting build.
  4. Fix the issues identified by lint, or add the following to your build script to proceed with errors:
  5. ...
  6. android {
  7. lintOptions {
  8. abortOnError false
  9. }
  10. }
  11. ...

按照fix提示,修改build.gradle即可

小结

类似问题最好遇到一次记录一次,防止反复查阅,很费时间的


作者:许彦峰
链接:https://www.jianshu.com/p/228252dec692
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Guff_9hys/article/detail/923298
推荐阅读
相关标签
  

闽ICP备14008679号