获取gradle构建的aapt2参数? [英] Getting the aapt2 parameters of a gradle build?

查看:301
本文介绍了获取gradle构建的aapt2参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有兴趣尝试使用aapt2从命令行编译android应用程序.尝试执行aapt2 link命令时,链接约束布局库时遇到问题.该程序可从Android studio/gradle成功构建.如何使gradle命令行调用向我显示正在运行的aapt2命令?

I am interested in trying to compile an android application from the command line using aapt2. I am having a problem linking the constraint-layout library when I try to perform the aapt2 link command. This program builds successfully from Android studio/gradle. How can I make gradle command line invocation show me the aapt2 command it is running?

出于记录,我尝试运行./gradlew assembleDebug --debug,我所看到的只是对aapt2-proto库的调用.

For the record, I have tried to run ./gradlew assembleDebug --debug and all I can see is the calls to the aapt2-proto library.

推荐答案

链接

该命令会有点复杂,并且取决于很多事情(例如您的资源,项目的依赖项,使用的标志等),因此获取完整命令的最简单方法是实际破坏"资源.
编辑您的 res/values/strings.xml 文件以包含:

Link

The command will be a bit complicated and will depend on many things (like your resources, the project's dependencies, flags used etc), so the easiest way to get the full command is to actually "break" a resource.
Edit your res/values/strings.xml file to contain:

<string name="incorrect">@string/idontexist</string>

转到项目目录,然后运行"gradlew clean assembleDebug". AAPT2在链接过程中将失败,并且Android Gradle插件将打印出所使用的完整命令.
我正在使用3.2.0-alpha13版本,它为我提供了完整的命令:

Go to the project's directory and run "gradlew clean assembleDebug". AAPT2 will fail during linking and Android Gradle Plugin will print out the full command used.
I'm using version 3.2.0-alpha13 and it gives me the command in full:

error: failed linking references.
Command: <path>/.gradle/caches/transforms-1/files-1.1/aapt2-3.2.0-alpha13-4662957-linux.jar/cbe84ab07c48b199e5fe8d202dd5845e/aapt2-3.2.0-alpha13-4662957-linux/aapt2 link -I\
      <path>/Android/Sdk/platforms/android-27/android.jar\
      --manifest\
      <path>/AndroidStudioProjects/Library/app/build/intermediates/merged_manifests/debug/processDebugManifest/merged/AndroidManifest.xml\
      -o\
      <path>/AndroidStudioProjects/Library/app/build/intermediates/processed_res/debug/processDebugResources/out/resources-debug.ap_\
      -R\
      @<path>/AndroidStudioProjects/Library/app/build/intermediates/incremental/processDebugResources/resources-list-for-resources-debug.ap_.txt\
      --auto-add-overlay\
      --java\
      <path>/AndroidStudioProjects/Library/app/build/generated/not_namespaced_r_class_sources/debug/processDebugResources/r\
      --proguard-main-dex\
      <path>/AndroidStudioProjects/Library/app/build/intermediates/legacy_multidex_aapt_derived_proguard_rules/debug/processDebugResources/manifest_keep.txt\
      --custom-package\
      com.example.foo.bar\
      -0\
      apk\
      --output-text-symbols\
      <path>/AndroidStudioProjects/Library/app/build/intermediates/symbols/debug/R.txt\
      --no-version-vectors
Daemon:  AAPT2 aapt2-3.2.0-alpha13-4662957-linux Daemon #0

此处用于AAPT2的标志:

The flags used here for AAPT2:

  • -I :平台android.jar的路径
  • -manifest :清单文件(AGP使用合并的清单,而不仅仅是源文件中的清单)
  • -o :输出文件
  • -R :资源.通常,您将一一传递,但是由于这里有很多传递,它们被写入文件,然后将该文件传递给AAPT2(这就是路径前面有"@"符号的原因)
  • -自动添加覆盖:允许覆盖
  • -java :R.java的输出目录
  • -proguard-main-dex :主dex的Proguard规则的输出文件
  • -custom-package :R类的软件包
  • -0 :请勿压缩这些扩展名
  • -output-text-symbols :R.txt的输出文件
  • -无版本向量:没有矢量可绘制对象的自动版本控制
  • -I: path to the platform's android.jar
  • --manifest: the manifest file (AGP uses the merged manifest, not simply the one in your sources)
  • -o: output file
  • -R: resources. Normally you'd pass one by one, but since there are many here, they're written to a file and then that file is passed to AAPT2 (that's why there's the "@" symbol before the path)
  • --auto-add-overlay: allow overlays
  • --java: output directory for the R.java
  • --proguard-main-dex: output file for Proguard rules for main dex
  • --custom-package: package for the R class
  • -0: do not compress these extensions
  • --output-text-symbols: output file for the R.txt
  • --no-version-vectors: no automatic versioning of vector drawables

其他可能对您有用的标志:

Other flags that might be useful to you:

  • -v :详细记录
  • -A :包含要放置在APK文件中的资产的目录,将不会对其进行处理或压缩,只需将其原样放入输出文件中即可.
  • -h :打印出帮助
  • -v: verbose logging
  • -A: directory containing assets you want placed in your APK file, they will not be processed or compressed, just put as-is into the output file
  • -h: print out help

编译

编译命令非常简单.您基本上可以告诉AAPT2哪个文件可以编译以及在哪里输出.

Compile

Compile command is pretty straightforward. You basically tell AAPT2 which single file to compile and where to output it.

<path>aapt2 compile -o /path/to/output/dir /path/to/file/to/compile.xml

用于编译的标志为:

  • -o :将放置编译文件的目录
  • -pseudo-localize :为伪语言环境生成源
  • -旧版:使APT2宽大处理并将某些错误视为警告(如APT1一样)
  • -v :详细记录
  • -h :打印帮助
  • -o: the directory where the compiled files will be placed
  • --pseudo-localize: generate sources for pseudo-locales
  • --legacy: causes AAPT2 to be more lenient and treat some errors as warnings (like AAPT1 did)
  • -v: verbose logging
  • -h: print out help

要记住的一件事是,编译文件是二进制.flat文件,输出名称基于输入名称和文件的父目录名称.这意味着,如果输入文件是 res/drawable-450dpi/img.png ,则输出文件将是 drawable-450dpi_img.png.flat .编译后的值文件的扩展名为"arsc.flat",而不仅仅是".flat",因此文件 res/values-en/strings.xml 将被编译为 values-en_strings.arsc .flat .这是由AAPT2自动完成的,因此您不必担心,但是如果以后需要查找已编译的文件,很高兴知道这一点.

关于编译的另一个有趣的事实是,记住输入文件的内容(以及XML文件的行号)很漂亮,因此,如果链接失败,错误将不会指向.flat文件,而是指向而是原始输入文件.

One thing to keep in mind is that the compiled files are binary .flat files and the output name is based in the input name and the file's parent directory name. That means if an input file was res/drawable-450dpi/img.pngthe output file will be drawable-450dpi_img.png.flat. Compiled values files get the extension "arsc.flat" instead of just ".flat", so a file res/values-en/strings.xml will be compiled to values-en_strings.arsc.flat. This is done automatically by AAPT2 so you don't need to worry about it, but it's good to know in case you need to find the compiled file later on.

Another fun fact about compile is that it's nifty when it comes to remembering what the input file was (and the line numbers for XML files), so if linking fails the error won't point to the .flat file, but to the original input file instead.

这篇关于获取gradle构建的aapt2参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆