简体   繁体   English

使用命令行工具构建 Android

[英]Android Build with command-line tools

I try to understand how a .apk file is built from source code.我试图了解.apk文件是如何从源代码构建的。 I know that with Android Studio or with the command gradlew the build can be done easily.我知道使用 Android Studio 或命令gradlew可以轻松完成构建。 The Android Sdk provides multiple tools that are used to build a .apk, which most of them I know what they do: Android Sdk 提供了多种用于构建 .apk 的工具,其中大多数我都知道它们是做什么的:

d8 : java byte code to dalvik bytecode d8 :java字节码到dalvik字节码

aapt2 : Compile resources and link them. aapt2 :编译资源并链接它们。 This tool creates a .apk file of compiled resources.此工具会创建一个包含已编译资源的.apk文件。 (It also creates R.java to reference the resources in the code.) (它还创建了R.java来引用代码中的资源。)

apksigner : Takes the .apk and signs it. apksigner :获取.apk并签名。 Otherwise, it cannot be installed on an Android device.否则,它无法安装在 Android 设备上。

zipalign : aligns uncompressed data of the .apk zipalign : 对齐.apk未压缩数据

The last two tools are used on the apk , which already contains all files.最后两个工具用于apk ,其中已经包含所有文件。 What I do not understand is how compiled code and compiled resources are combined?我不明白的是编译代码和编译资源是如何组合的? Before aapt2 was a thing the packaging was done with aapt , but it is not even listed on Androids Documentation .aapt2成为一种东西之前,包装是用aapt完成的,但它甚至没有在Android 文档中列出。 However, aapt is still in the Android Sdk build-tools.但是, aapt仍在 Android Sdk 构建工具中。 The new aapt2 has no option for packaging.新的aapt2没有打包选项。

So, the question is which command-line tools and in which order are used to create a complete .apk from source code?那么,问题是使用哪些命令行工具以及以何种顺序从源代码创建完整的.apk Thank you!谢谢!

EDIT: What I am looking for would be similar to this , where a .apk is created with the command-line step by step.编辑:我正在寻找的内容与类似,其中.apk是使用命令行逐步创建的。 Only the answer uses the old aapt , which I think is not used anymore.只有答案使用旧的aapt ,我认为它不再使用了。

EDIT 2: Thanks to Allen Shaw for giving me the missing information.编辑 2:感谢Allen Shaw给我提供了缺失的信息。 I was now able to compile an app with aapt2 .我现在能够用aapt2编译一个应用程序。 The answer to my question was simply to zip the aapt2 output and the classes.dex .我的问题的答案只是压缩aapt2输出和classes.dex

You may also use aapt2 command您也可以使用aapt2命令

aapt2 link -o output.apk

reference the google document here这里参考谷歌文档

Add .dex file into apk:将 .dex 文件添加到 apk 中:

zip -ur app-debug.apk classes.dex

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM