cocos打包安卓: gradlew打包Android环境搭建

简介: cocos打包安卓: gradlew打包Android环境搭建
cd frameworks\runtime-src\proj.android-studio
gradlew
复制代码

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

1.安装Java和下载gradlew

第一次会提示没有java

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

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

网络异常,图片无法展示
|

重新执行gradlew,会下载需要的gradle

网络异常,图片无法展示
|

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

> You have not accepted the license agreements of the following SDK components:
  [Android SDK Build-Tools 26.0.2, Android SDK Platform 29].
  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.
  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

网络异常,图片无法展示
|
提示信息和项目配置对应的

网络异常,图片无法展示
|

4. 配置NDK

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

> NDK not configured.
  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,安装即可

A problem occurred configuring project ':libcocos2dx'.
> You have not accepted the license agreements of the following SDK components:
  [Android SDK Platform 14].
复制代码

网络异常,图片无法展示
|

再次执行gradlew

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

网络异常,图片无法展示
|

gradlew的使用

根据上一步的提示:

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

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

打包

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

------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------
Android tasks
-------------
androidDependencies - Displays the Android dependencies of the project.
signingReport - Displays the signing info for each variant.
sourceSets - Prints out all the source sets defined in this project.
Build tasks
-----------
assemble - Assembles all variants of all applications and secondary packages.
assembleAndroidTest - Assembles all the Test applications.
assembleDebug - Assembles all Debug builds.
assembleRelease - Assembles all Release builds.
build - Assembles and tests this project.
buildDependents - Assembles and tests this project and all projects that depend on it.
buildNeeded - Assembles and tests this project and all projects it depends on.
clean - Deletes the build directory.
cleanBuildCache - Deletes the build cache directory.
compileDebugAndroidTestSources
compileDebugSources
compileDebugUnitTestSources
compileReleaseSources
compileReleaseUnitTestSources
extractDebugAnnotations - Extracts Android annotations for the debug variant into the archive file
extractReleaseAnnotations - Extracts Android annotations for the release variant into the archive file
mockableAndroidJar - Creates a version of android.jar that's suitable for unit tests.
Build Setup tasks
-----------------
init - Initializes a new Gradle build.
wrapper - Generates Gradle wrapper files.
Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'proj.android-studio'.
components - Displays the components produced by root project 'proj.android-studio'. [incubating]
dependencies - Displays all dependencies declared in root project 'proj.android-studio'.
dependencyInsight - Displays the insight into a specific dependency in root project 'proj.android-studio'.
dependentComponents - Displays the dependent components of components in root project 'proj.android-studio'. [incubating]
help - Displays a help message.
model - Displays the configuration model of root project 'proj.android-studio'. [incubating]
projects - Displays the sub-projects of root project 'proj.android-studio'.
properties - Displays the properties of root project 'proj.android-studio'.
tasks - Displays the tasks runnable from root project 'proj.android-studio' (some of the displayed tasks may belong to subprojects).
Install tasks
-------------
installDebug - Installs the Debug build.
installDebugAndroidTest - Installs the android (on device) tests for the Debug build.
installRelease - Installs the Release build.
uninstallAll - Uninstall all applications.
uninstallDebug - Uninstalls the Debug build.
uninstallDebugAndroidTest - Uninstalls the android (on device) tests for the Debug build.
uninstallRelease - Uninstalls the Release build.
Verification tasks
------------------
check - Runs all checks.
connectedAndroidTest - Installs and runs instrumentation tests for all flavors on connected devices.
connectedCheck - Runs all device checks on currently connected devices.
connectedDebugAndroidTest - Installs and runs the tests for debug on connected devices.
deviceAndroidTest - Installs and runs instrumentation tests using all Device Providers.
deviceCheck - Runs all device checks using Device Providers and Test Servers.
lint - Runs lint on all variants.
lintDebug - Runs lint on the Debug build.
lintRelease - Runs lint on the Release build.
lintVitalRelease - Runs lint on just the fatal issues in the release build.
test - Run unit tests for all variants.
testDebugUnitTest - Run unit tests for the debug build.
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)

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

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

网络异常,图片无法展示
|

然后我下载了r16b解压后发现是有的:

网络异常,图片无法展示
|

Werror=format-security

切换到r16b后再次进行编译

/frameworks/cocos2d-x/cocos/3d/../platform/CCPlatformMacros.h:221:67: 
error: format not a string literal and no format arguments [-Werror=format-security]
#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)

不知原因的问题

error: undefined reference to 'vtable
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即可,堆空间设置的大一点

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

真机安装

gradlew installDebug

网络异常,图片无法展示
|
手机系统中有应用阻止了安装

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

lint导致打包停止

* What went wrong:
Execution failed for task ':xxxx:lint'.
> Lint found errors in the project; aborting build.
  Fix the issues identified by lint, or add the following to your build script to proceed with errors:
  ...
  android {
      lintOptions {
          abortOnError false
      }
  }
  ...
复制代码

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

小结

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


目录
相关文章
|
20天前
|
敏捷开发 Java 机器人
云效产品使用常见问题之打包后的Android应用获取下载地址失败如何解决
云效作为一款全面覆盖研发全生命周期管理的云端效能平台,致力于帮助企业实现高效协同、敏捷研发和持续交付。本合集收集整理了用户在使用云效过程中遇到的常见问题,问题涉及项目创建与管理、需求规划与迭代、代码托管与版本控制、自动化测试、持续集成与发布等方面。
|
21天前
|
存储 设计模式 数据库
构建高效的安卓应用:探究Android Jetpack架构组件
【4月更文挑战第20天】 在移动开发的世界中,构建一个既高效又可维护的安卓应用是每个开发者追求的目标。随着Android Jetpack的推出,Google为开发者提供了一套高质量的库、工具和指南,以简化应用程序开发流程。本文将深入探讨Jetpack的核心组件之一——架构组件,并展示如何将其应用于实际项目中,以提升应用的响应性和稳定性。我们将通过分析这些组件的设计原则,以及它们如何协同工作,来揭示它们对于构建现代化安卓应用的重要性。
|
4天前
|
Java Android开发
Android module 打包成aar或jar
Android module 打包成aar或jar
11 0
|
11天前
|
缓存 算法 Android开发
构建高效安卓应用:深入理解Android Profiler
【4月更文挑战第30天】 在移动开发领域,应用性能优化是提升用户体验的关键因素之一。针对安卓开发者,Android Studio提供的Android Profiler是一个强大的工具,它集成了多个性能分析仪器,帮助开发者监控、分析并优化应用性能。本文将深入探讨Android Profiler的核心功能,并通过案例演示如何利用该工具定位性能瓶颈,以及采取相应的优化措施,从而构建更加高效的安卓应用。
|
18天前
|
移动开发 API 数据处理
构建高效安卓应用:探究Android 12中的新特性与性能优化策略
【4月更文挑战第23天】 随着移动设备的普及,用户对应用程序的性能和效率要求越来越高。安卓系统作为市场占有率最高的移动操作系统之一,其版本更新带来了众多性能提升和新特性。本文将深入探讨Android 12版本中引入的关键性能优化技术,并分析这些技术如何帮助开发者构建更加高效的安卓应用。我们将从最新的运行时权限、后台任务优化、以及电池使用效率等方面入手,提供具体的实践建议,旨在帮助开发者更好地利用这些新工具,以提升应用的响应速度、降低能耗,并最终提高用户的满意度。
|
1月前
|
监控 API Android开发
构建高效安卓应用:探究Android 12中的新特性与性能优化
【4月更文挑战第8天】 在本文中,我们将深入探讨Android 12版本引入的几项关键技术及其对安卓应用性能提升的影响。不同于通常的功能介绍,我们专注于实际应用场景下的性能调优实践,以及开发者如何利用这些新特性来提高应用的响应速度和用户体验。文章将通过分析内存管理、应用启动时间、以及新的API等方面,为读者提供具体的技术实现路径和代码示例。
|
3月前
|
Java 开发工具 Android开发
cordova打包android apk
cordova打包android apk
16 0
|
3月前
|
Android开发 数据安全/隐私保护
打包 android apk签名
打包 android apk签名
20 0
|
8天前
|
存储 安全 Android开发
安卓应用开发:构建一个高效的用户登录系统
【5月更文挑战第3天】在移动应用开发中,用户登录系统的设计与实现是至关重要的一环。对于安卓平台而言,一个高效、安全且用户体验友好的登录系统能够显著提升应用的用户留存率和市场竞争力。本文将探讨在安卓平台上实现用户登录系统的最佳实践,包括对最新身份验证技术的应用、安全性考量以及性能优化策略。
|
11天前
|
前端开发 Android开发 iOS开发
【Flutter前端技术开发专栏】Flutter在Android与iOS上的性能对比
【4月更文挑战第30天】Flutter 框架实现跨平台移动应用,通过一致的 UI 渲染(Skia 引擎)、热重载功能和响应式框架提高开发效率和用户体验。然而,Android 和 iOS 的系统差异、渲染机制及编译过程影响性能。性能对比显示,iOS 可能因硬件优化提供更流畅体验,而 Android 更具灵活性和广泛硬件支持。开发者可采用代码、资源优化和特定平台优化策略,利用性能分析工具提升应用性能。
【Flutter前端技术开发专栏】Flutter在Android与iOS上的性能对比