Building and Running

本文详细介绍了Android应用从源代码到apk文件的构建全过程,包括编译、打包及签名步骤。阐述了Eclipse环境下如何自动构建apk,以及非Eclipse环境下使用Ant构建脚本的实现方式。同时,强调了应用程序在模拟器或设备上运行前的签名必要性,以及不同开发模式(debug vs. release)下的具体操作。

Building and Running

构建和运行

During the build process, your Android projects are compiled and packaged into an .apk file, the container for your application binary. It contains all of the information necessary to run your application on a device or emulator, such as compiled .dex files (.class files converted to Dalvik byte code), a binary version of the AndroidManifest.xml file, compiled resources (resources.arsc) and uncompiled resource files for your application.

在整个编译过程中,你的安卓工程将会打包成一个.apk 文件,它是你的程序的二进制代码容器。他包含了在设备或者模拟器上运行你的应用程序的所有必要的信息,例如编译过的.dex文件(.class文件转化形成的Dalvik字节码),一个二进制版本的AndroidManifest.xml文件,编译过的资源文件(resources.arsc)以及其他未编译的资源文件。

If you are developing in Eclipse, the ADT plugin incrementally builds your project as you make changes to the source code. Eclipse outputs an .apk file automatically to the bin folder of the project, so you do not have to do anything extra to generate the .apk.

如果你在Eclipse中进行开发,那么ADT插件将会增量的构建你的应用程序中源码改变的部分。Eclipse自动在你工程的bin目录下,输出了一个.apk文件,所以你无需做其他任何额外的操作。

If you are developing in a non-Eclipse environment, you can build your project with the generated build.xml Ant file that is in the project directory. The Ant file calls targets that automatically call the build tools for you.

如果你在非Eclipse环境中开发,你可以使用Ant的build.xml来编译你的工程。Ant文件能够自动的为你调用编译工具。

To run an application on an emulator or device, the application must be signed using debug or release mode. You typically want to sign your application in debug mode when you develop and test your application, because the build tools use a debug key with a known password so you do not have to enter it every time you build. When you are ready to release the application to Google Play, you must sign the application in release mode, using your own private key.

为了在模拟器或者设备上运行你的应用程序,你的应用程序必须使用调试(debug)或者发布(release)模式进行签名。当你开发和测试你的应用程序的时候,你可能希望用调试模式进行签名,因为构建工具使用的是已知的调试签名,所以你在每次编译时无需键入密码。当你准备在谷歌市场上发布时,你必须使用你的私钥以发布模式进行签名。

Fortunately, Eclipse or your Ant build script signs the application for you in debug mode when you build your application. You can also easily setup Eclipse or your Ant build to sign your application in release mode as well. For more information on signing applications, see Signing Your Applications.

幸运的是,Eclipse或者你的Ant构建脚本能够以调试或者发布模式签名你的应用程序。更多信息,请看Signing Your Applications.

The following diagram depicts the components involved in building and running an application:

下面的图表描述了如何编译和运行:

A Detailed Look at the Build Process


The build process involves many tools and processes that generate intermediate files on the way to producing an .apk. If you are developing in Eclipse, the complete build process is automatically done periodically as you develop and save your code changes. If you are using other IDEs, this build process is done every time you run the generated Ant build script for your project. It is useful, however, to understand what is happening under the hood since much of the tools and processes are masked from you. The following diagram depicts the different tools and processes that are involved in a build:

构建程序包含了很多程序和过程,他们在产生.apk文件的过程中产生了很多中间中间。如果你在Eclipse中开发,在你保存改变的代码之后,完整的编译工具将会周期性的进行。如果你使用其他的IDE工具,构建过程会在你每次执行Ant编译脚本时执行。这很有意义,然而,为了理解构建工具隐藏背后发生了什么事情。下面这张图表将会描述不同的工具在你的构建过程中所进行的操作。

The general process for a typical build is outlined below:

一个典型的程序通过下面这些步骤生成:

  • The Android Asset Packaging Tool (aapt) takes your application resource files, such as the AndroidManifest.xmlfile and the XML files for your Activities, and compiles them. An R.java is also produced so you can reference your resources from your Java code.
  • 安卓资源打包工具(aapt)获取你的应用程序的资源文件,例如AndroidManifest.xml文件,XML等,编译他们。一个R.java也是产品,以至于你能够通过Java代码引用他们。
  • The aidl tool converts any .aidl interfaces that you have into Java interfaces.
  • aidl个工具用于转化任何的.aidl接口为Java接口
  • All of your Java code, including the R.java and .aidl files, are compiled by the Java compiler and .class files are output.
  • 所有的Java代码,包括R.java以及.aidl文件,将会通过Java编译器,以.class文件类型输出。
  • The dex tool converts the .class files to Dalvik byte code. Any 3rd party libraries and .class files that you have included in your project are also converted into .dex files so that they can be packaged into the final .apk file.
  • dex工具将会把.class文件转化为Dalvik字节码。任何第三方的.class文件也会转化为.dex文件,以便能够打包到.apk文件中。
  • All non-compiled resources (such as images), compiled resources, and the .dex files are sent to the apkbuilder tool to be packaged into an .apk file.
  • 所有的非编译资源(例如图像),编译资源,以及.dex文件都会发送给apkbuilder工具,以便于打包成为你个.apk文件。
  • Once the .apk is built, it must be signed with either a debug or release key before it can be installed to a device.
  • 一旦.apk包构建后,你必须使用debug或者release进行签名,然后才能安装到设备上面。
  • Finally, if the application is being signed in release mode, you must align the .apk with the zipalign tool. Aligning the final .apk decreases memory usage when the application is running on a device.
  • 最终,如果应用程序以release进行了签名,你需要使用zipalign工具进行对齐。对齐的.apk将会在运行时节省内存开销。
内容概要:本文介绍了一个基于冠豪猪优化算法(CPO)的无人机三维路径规划项目,利用Python实现了在复杂三维环境中为无人机规划安全、高效、低能耗飞行路径的完整解决方案。项目涵盖空间环境建模、无人机动力学约束、路径编码、多目标代价函数设计以及CPO算法的核心实现。通过体素网格建模、动态障碍物处理、路径平滑技术和多约束融合机制,系统能够在高维、密集障碍环境下快速搜索出满足飞行可行性、安全性与能效最优的路径,并支持在线重规划以适应动态环境变化。文中还提供了关键模块的代码示例,包括环境建模、路径评估和CPO优化流程。; 适合人群:具备一定Python编程基础和优化算法基础知识,从事无人机、智能机器人、路径规划或智能优化算法研究的相关科研人员与工程技术人员,尤其适合研究生及有一定工作经验的研发工程师。; 使用场景及目标:①应用于复杂三维环境下的无人机自主导航与避障;②研究智能优化算法(如CPO)在路径规划中的实际部署与性能优化;③实现多目标(路径最短、能耗最低、安全性最高)耦合条件下的工程化路径求解;④构建可扩展的智能无人系统决策框架。; 阅读建议:建议结合文中模型架构与代码示例进行实践运行,重点关注目标函数设计、CPO算法改进策略与约束处理机制,宜在仿真环境中测试不同场景以深入理解算法行为与系统鲁棒性。
### 解决 Bash 脚本中 `set` 命令的无效选项错误并优化 CppUTest 构建流程与覆盖率报告生成 #### 1. 分析 `set` 命令无效选项错误 在运行 Bash 脚本时,如果出现 `'invalid option'` 错误,通常是因为传递给 `set` 的选项不符合其预期格式或规则。`set` 命令用于设置 shell 的环境变量和选项[^2]。例如,以下代码展示了如何正确使用 `set` 命令来启用调试模式(`-x`)并打印位置参数: ```bash set -x set -- "arg1" "arg2" "arg3" echo $1 $2 $3 ``` 如果脚本中存在类似以下的无效命令,则会引发错误: ```bash set -o invalidoption ``` #### 2. 解决方案:修复 `set` 命令中的无效选项 为解决 `'invalid option'` 错误,可以采取以下措施: - **检查脚本中的 `set` 命令**:确认传递给 `set` 的选项是否正确。例如,常见的有效选项包括 `-e`(遇到错误退出)、`-u`(禁止未定义变量)、`-x`(调试模式)等[^3]。 - **分离选项和参数**:如果脚本中需要同时指定选项和参数,应使用连字符将两者分隔开。例如: ```bash set -o errexit - "param1" "param2" ``` #### 3. 优化 CppUTest 构建流程 CppUTest 是一个轻量级的单元测试框架,适用于 C 和 C++ 项目。为了优化构建流程,可以采用以下方法: - **使用 Makefile 自动化构建**:通过编写 Makefile 文件,简化构建过程。例如: ```makefile CC = g++ CFLAGS = -Wall -g -O0 LDFLAGS = -lcpputest SOURCES = $(wildcard *.cpp) OBJECTS = $(SOURCES:.cpp=.o) EXECUTABLE = test_executable all: $(EXECUTABLE) $(EXECUTABLE): $(OBJECTS) $(CC) $(OBJECTS) -o $@ $(LDFLAGS) %.o: %.cpp $(CC) -c $< -o $@ $(CFLAGS) clean: rm -f *.o $(EXECUTABLE) ``` - **集成 Gcov 和 Lcov 工具**:通过 Gcov 和 Lcov 工具生成覆盖率报告。以下是配置示例: ```makefile CFLAGS += --coverage LDFLAGS += --coverage coverage: lcov --capture --directory . --output-file coverage.info genhtml coverage.info --output-directory coverage_report ``` #### 4. 示例脚本:结合 CppUTest、Gcov 和 Lcov 以下是一个完整的示例脚本,展示如何修复 `set` 命令错误并生成覆盖率报告: ```bash #!/bin/bash # 启用调试模式并设置严格选项 set -euo pipefail # 定义变量 SOURCE_DIR="src" TEST_DIR="tests" COVERAGE_REPORT="coverage_report" # 构建步骤 make clean make all # 运行测试 ./test_executable # 生成覆盖率报告 lcov --capture --directory . --output-file coverage.info genhtml coverage.info --output-directory ${COVERAGE_REPORT} # 输出结果路径 echo "Coverage report generated at: ${COVERAGE_REPORT}/index.html" ``` #### 5. 注意事项 - 确保安装了必要的工具(如 Gcov、Lcov 和 CppUTest),并通过包管理器安装缺失的依赖项。 - 如果脚本需要兼容多个 Bash 版本,建议使用 POSIX 兼容的语法[^4]。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值