简体   繁体   English

安卓“dx”工具

[英]Android 'dx' tool

Is there any documentation for 'dx'?有没有关于“dx”的文档?

In particular, I am interested in knowing what the --core-library option does.我特别想知道--core-library选项的作用。

What is the 'dx' tool?什么是“dx”工具?

The dx tool converts Java class files into a *.dex ( Dalvik executable)* file. dx工具将 Java 类文件转换为*.dexDalvik可执行文件)* 文件。

Where is it?它在哪里?

The dx.jar file was original located under android-sdk/platforms/android-X/tools/lib/ before (especially in Android 3 and Android 4 ) and was moved to android-sdk/platform-tools/lib/ later. dx.jar文件最初位于android-sdk/platforms/android-X/tools/lib/之前(尤其是在Android 3Android 4 中),后来移至android-sdk/platform-tools/lib/

How does it fit in Android?它如何适应Android?

The Java source files are converted to Java class files by the Java compiler. Java 源文件由 Java 编译器转换为Java 类文件

The dx tool converts Java class files into a *.dex (Dalvik executable)* file. dx工具将 Java 类文件转换为*.dex (Dalvik 可执行文件)* 文件。 All class files of the application are placed in this .dex file.应用程序的所有类文件都放在这个.dex文件中。 During this conversion process redundant information in the class files are optimized in the .dex file.在此转换过程中,类文件中的冗余信息在.dex文件中进行了优化。

For example, if the same String is found in different class files, the .dex file contains only one reference of this String.例如,如果在不同的类文件中发现相同的 String,则.dex文件仅包含此 String 的一个引用。

These .dex files are therefore much smaller in size than the corresponding class files.因此,这些.dex文件的大小比相应的类文件小得多。

The .dex file and the resources of an Android project, eg, the images and XML files, are packed into an .apk (Android Package) file. .dex文件和 Android 项目的资源,例如图像和 XML 文件,被打包到一个.apk (Android 包)文件中。

To understand better, look at the Android build process:为了更好地理解,请查看 Android 构建过程:

构建过程

FYI:供参考:

The program AAPT ( Android Asset Packaging Tool ) performs APK creation.程序AAPT( Android 资产打包工具执行APK创建。 The resulting .apk file contains all necessary data to run the Android application and can be deployed to an Android device via the ADB (Android device bridge) tool.生成的.apk文件包含运行 Android 应用程序所需的所有数据,并且可以通过ADB (Android 设备桥接器)工具部署到 Android 设备。

Reference参考

This is a special purpose flag that is only used when building some of the framework JAR files ( core.jar , framework.jar , etc.).这是一个特殊用途的标志,仅在构建某些框架 JAR 文件( core.jarframework.jar等)时使用。 Normally, dx will refuse to process any java.* or javax.* classes.通常,dx 将拒绝处理任何 java.* 或 javax.* 类。 So this option is used for core.jar , where all those classes are actually defined.所以这个选项用于core.jar ,其中所有这些类实际上都被定义了。

Here's a relevant blurb from the dx source ( dalvik/dx/src/com/android/dx/command/dexer/Main.java ), that gets printed if you try to include a java.* or javax.* class in an application.这是来自 dx 源( dalvik/dx/src/com/android/dx/command/dexer/Main.java )的相关简介,如果您尝试在应用程序中包含 java.* 或 javax.* 类,则会打印该简介.

Ill-advised or mistaken usage of a core class (java.* or javax.*) when not building a core library.在不构建核心库时不明智或错误地使用核心类(java.* 或 javax.*)。 This is often due to inadvertently including a core library file in your application's project, when using an IDE (such as Eclipse).这通常是由于在使用 IDE(例如 Eclipse)时无意中在应用程序的项目中包含了一个核心库文件。 If you are sure you're not intentionally defining a core class, then this is the most likely explanation of what's going on.如果您确定您不是故意定义核心类,那么这就是最有可能的解释。

However, you might actually be trying to define a class in a core namespace, the source of which you may have taken, for example, from a non-Android virtual machine project.但是,您实际上可能正在尝试在核心命名空间中定义一个类,例如,您可能从非 Android 虚拟机项目中获取该类的来源。 This will most assuredly not work.这肯定不会奏效。 At a minimum, it jeopardizes the compatibility of your app with future versions of the platform.至少,它会危及您的应用程序与平台未来版本的兼容性。 It is also often of questionable legality.它的合法性也经常受到质疑。

If you really intend to build a core library -- which is only appropriate as part of creating a full virtual machine distribution, as opposed to compiling an application -- then use the "--core-library" option to suppress this error message.如果您真的打算构建一个核心库——它只适合作为创建完整虚拟机发行版的一部分,而不是编译应用程序——然后使用“--core-library”选项来抑制此错误消息。 If you go ahead and use "--core-library" but are in fact building an application, then be forewarned that your application will still fail to build or run, at some point.如果您继续使用“--core-library”,但实际上正在构建应用程序,那么请预先警告您的应用程序在某些时候仍然无法构建或运行。 Please be prepared for angry customers who find, for example, that your application ceases to function once they upgrade their operating system.例如,如果愤怒的客户发现您的应用程序在升级操作系统后停止运行,请做好准备。 You will be to blame for this problem.你会为这个问题负责。

If you are legitimately using some code that happens to be in a core package, then the easiest safe alternative you have is to repackage that code.如果您合法地使用某些恰好位于核心包中的代码,那么您拥有的最简单安全的替代方法是重新打包该代码。 That is, move the classes in question into your own package namespace.也就是说,将有问题的类移动到您自己的包命名空间中。 This means that they will never be in conflict with core system classes.这意味着它们永远不会与核心系统类发生冲突。 JarJar is a tool that may help you in this endeavor. JarJar 是一个可以帮助您完成这项工作的工具。 If you find that you cannot do this, then that is an indication that the path you are on will ultimately lead to pain, suffering, grief, and lamentation.如果你发现你不能做到这一点,那么这表明你所走的道路最终会导致痛苦、痛苦、悲伤和悲叹。

The --core-library option on Dx will bypass the stupidity check that prevents you from accidentally including Java core libraries in your Android app. Dx 上的--core-library选项将绕过愚蠢检查,防止您在 Android 应用程序中意外包含 Java 核心库。

Dx will barf if you try to include a library that contains packages in the java.* or javax.* namespace.如果您尝试包含包含java.*javax.*命名空间中的包的库,则 Dx 将失败。 The thinking is that classes in that namespace are likely to depend on other JDK "core" classes, which will break your app since they (may) not be present on Android.想法是该命名空间中的类可能依赖于其他 JDK“核心”类,这会破坏您的应用程序,因为它们(可能)不存在于 Android 上。

Now, of course, just because a Java package starts with java.* or javax.* does not necessarily mean that it depends on the JDK proper.现在,当然,仅仅因为 Java 包以java.*javax.*开头并不一定意味着它依赖于 JDK。 It may work perfectly fine in Android.它可能在 Android 中工作得很好。 The recommendation, if you know what you are doing, if you know that your java/x.* classes don't depend on JDK core classes, is to use a tool like JarJar to repackage the JAR file under a different namespace.建议,如果您知道自己在做什么,如果您知道您的 java/x.* 类不依赖于 JDK 核心类,则建议使用 JarJar 之类的工具将 JAR 文件重新打包到不同的命名空间下。

That being said, to get around the stupidity check, add the --core-library option to dx.话虽如此,为了绕过愚蠢检查,请将--core-library选项添加到 dx。 Change the last line of $ANDROID_HOME/platform-tools/dx from,$ANDROID_HOME/platform-tools/dx的最后一行从,

exec java $javaOpts -jar "$jarpath" "$@"

to,到,

exec java $javaOpts -jar "$jarpath" --core-library "$@"

In my case, I was including a library that depended on Jackson , which depends on JAXB .就我而言,我包含了一个依赖于Jackson的库,而后者依赖于JAXB For me, overriding the stupidity check was acceptable because the library's use of Jackson was only for JSON and not for XML serialization (I only include the JAXB API library, not the implementation).对我来说,覆盖愚蠢检查是可以接受的,因为库对 Jackson 的使用仅用于 JSON 而不是用于 XML 序列化(我只包括 JAXB API 库,而不是实现)。 of course I wish there was a cleaner way to go about this, but rewriting the top-level library to avoid using Jackson was not an option.当然,我希望有一种更简洁的方法来解决这个问题,但重写顶级库以避免使用 Jackson 不是一种选择。

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

相关问题 是否有适用于Android的DX工具的API规范或示例! - Are there a specification of an API or samples for the DX tool for Android! Android SDK 中的“dx”工具在哪里? - Where is the 'dx' tool located in the Android SDK? 为 Android 运行 dx 或 d8 工具时出错 - Error while running dx or d8 tool for Android Intelij想法错误编译Adobe Air for Android-DX工具失败 - Intelij idea error compiling Adobe Air for Android - dx tool failed Github 操作:构建工具 33.0.0 在 /usr/local/lib/android/sdk/build-tools/33.0.0/dx 缺少 DX - Github Action: Build-tool 33.0.0 is missing DX at /usr/local/lib/android/sdk/build-tools/33.0.0/dx Android Studio(IntelliJ),maven构建工具:无法加载dx.jar - Android Studio (IntelliJ), maven build tool: Failed to load dx.jar 由于“ dx工具失败”,因此无法在Flash Builder 4.7上构建Android Release Build - Cannot build Android Release Build on Flash Builder 4.7 due to “dx tool failed” Azure DevOps ionic Android Build 错误“Build-tool 31.0.0 is missing DX at /usr/local/lib/android/sdk/build-tools/31.0.0/dx” - Azure DevOps ionic Android Build Error "Build-tool 31.0.0 is missing DX at /usr/local/lib/android/sdk/build-tools/31.0.0/dx" Android DX使用 - Android dx usage Android SDK中没有DX? - No dx in android SDK?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM