草庐IT

gradlew打包Android环境搭建

许彦峰 2023-03-28 原文
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即可

小结

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

有关gradlew打包Android环境搭建的更多相关文章

  1. ruby-on-rails - 在 Rails 开发环境中为 .ogv 文件设置 Mime 类型 - 2

    我正在玩HTML5视频并且在ERB中有以下片段:mp4视频从在我的开发环境中运行的服务器很好地流式传输到chrome。然而firefox显示带有海报图像的视频播放器,但带有一个大X。问题似乎是mongrel不确定ogv扩展的mime类型,并且只返回text/plain,如curl所示:$curl-Ihttp://0.0.0.0:3000/pr6.ogvHTTP/1.1200OKConnection:closeDate:Mon,19Apr201012:33:50GMTLast-Modified:Sun,18Apr201012:46:07GMTContent-Type:text/plain

  2. Vscode+Cmake配置并运行opencv环境(Windows和Ubuntu大同小异) - 2

    之前在培训新生的时候,windows环境下配置opencv环境一直教的都是网上主流的vsstudio配置属性表,但是这个似乎对新生来说难度略高(虽然个人觉得完全是他们自己的问题),加之暑假之后对cmake实在是爱不释手,且这样配置确实十分简单(其实都不需要配置),故斗胆妄言vscode下配置CV之法。其实极为简单,图比较多所以很长。如果你看此文还配不好,你应该思考一下是不是自己的问题。闲话少说,直接开始。0.CMkae简介有的人到大二了都不知道cmake是什么,我不说是谁。CMake是一个开源免费并且跨平台的构建工具,可以用简单的语句来描述所有平台的编译过程。它能够根据当前所在平台输出对应的m

  3. 安卓apk修改(Android反编译apk) - 2

    最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路

  4. ruby-on-rails - ruby gem如何在rails环境下工作 - 2

    我试图在rails中了解rubygems是如何变得可以自动使用的,而不是在使用required的文件中gem? 最佳答案 这是通过bundler/setup完成的:http://bundler.io/v1.3/bundler_setup.html.它在您的config/boot.rb文件中是必需的。简而言之,它首先将环境变量设置为指向您的Gemfile:ENV['BUNDLE_GEMFILE']||=File.expand_path('../../Gemfile',__FILE__)然后它通过要求bundler/setup将所有ge

  5. ruby-on-rails - 我需要一个真正的 UNIX RoR 开发环境 - 2

    从一开始,我就是一个Windows高手。我从MS-DOS开始。我安装了Windows2.1以及此后的所有Windows。现在,我家里有10台不同的Windows机器在运行,从Windows7Ultimate到各种版本的WindowsServer。我还没有完成Windows8,也不想去那里。我在服务器和各种软件方面都有UNIX经验,但它并不是我的首选环境。但是,我想我正在转换。我试图假装使用Cygwin和MSYS在Windows下运行UNIX。我的目的是搭建一个开发环境。两者都让我失望了。我花了比开发更多的时间来解决一系列技术问题。这是NotAcceptable。到目前为止,我的Ruby

  6. ruby-on-rails - 如果特定语言环境中缺少翻译,如何配置 i18n 以使用 en 语言环境? - 2

    如果特定语言环境中缺少翻译,如何配置i18n以使用en语言环境翻译?当前已插入翻译缺失消息。我正在使用RoR3.1。 最佳答案 找到相似的question这里是答案:#application.rb#railswillfallbacktoconfig.i18n.default_localetranslationconfig.i18n.fallbacks=true#railswillfallbacktoen,nomatterwhatissetasconfig.i18n.default_localeconfig.i18n.fallback

  7. ruby-on-rails - 可移植 Ruby on Rails 环境 - 2

    我给自己买了一个新的8gigUSBkey,我正在寻找一个合适的解决方案来拥有一个可移植RoR环境来学习。我在谷歌上搜索了一下,发现了一些可能性,但我很想听听一些现实生活中的经历和意见。谢谢! 最佳答案 我喜欢InstantRails,非常容易使用,无需安装程序,也不会修改您的系统环境。 关于ruby-on-rails-可移植RubyonRails环境,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/q

  8. ruby-on-rails - 如何通过 URL 更改语言环境? - 2

    在我的双语Rails4应用程序中,我有一个像这样的LocalesController:classLocalesController用户可以通过此表单更改其语言环境:deflocale_switcherform_tagurl_for(:controller=>'locales',:action=>'change_locale'),:method=>'get',:id=>'locale_switcher'doselect_tag'set_locale',options_for_select(LANGUAGES,I18n.locale.to_s)end这有效。但是,目前用户无法通过URL更改

  9. ruby - 从 FaSTLane 将环境变量传递给 shell 脚本 - 2

    我在跑Fastlane(适用于iOS的持续构建工具)以执行用于解密文件的自定义shell脚本。这是命令。sh"./decrypt.shENV['ENCRYPTION_P12']"我想不出将环境变量传递给该脚本的方法。显然,如果我将密码硬编码到脚本中,它就可以正常工作。sh"./decrypt.shmypwd"有什么建议吗? 最佳答案 从直接Shell中扩展假设这里的sh是一个faSTLane命令,它以给定的参数作为脚本文本调用shell命令:#asafastlanedirectivesh'./decrypt.sh"$ENCRYPTI

  10. 【自动驾驶环境感知项目】——基于Paddle3D的点云障碍物检测 - 2

    文章目录1.自动驾驶实战:基于Paddle3D的点云障碍物检测1.1环境信息1.2准备点云数据1.3安装Paddle3D1.4模型训练1.5模型评估1.6模型导出1.7模型部署效果附录show_lidar_pred_on_image.py1.自动驾驶实战:基于Paddle3D的点云障碍物检测项目地址——自动驾驶实战:基于Paddle3D的点云障碍物检测课程地址——自动驾驶感知系统揭秘1.1环境信息硬件信息CPU:2核AI加速卡:v100总显存:16GB总内存:16GB总硬盘:100GB环境配置Python:3.7.4框架信息框架版本:PaddlePaddle2.4.0(项目默认框架版本为2.3

随机推荐