• 使用最新android sdk 将jar文件编译成dex


    最近需要一些比较骚的操作,所以需要将gson编译成dex。

    因为手上有jar包,所以就拿出了android sdk准备一把入魂,结果报错不断,让人无奈。只好根据报错来调整编译步骤,不得不为安卓环境更新Debug。

    1、dx变d8

    并不确定安卓更名的原因,但找到这个只是根据名字前面有个相同的d,这对于不太熟悉更新的人来说,只能靠蒙。

    并不确定从哪个版本开始,dx.bat更新成了d8.bat。而且在使用中,命令方式也发生了变化,这个后面再讲。

    所以,原来的dx --dex --output xxx.dex xxx.jar命令,不能使用了。

    2、jre环境报错。

    日常使用java的人,我觉得大多数人还是使用1.8。

    不过随着各种软件,及编译环境的更新,java1.8的问题慢慢出现了,也出现了各种不兼容的情况。

    只是到目前为止,还没有更新的动力。

    这次编译,报错了:

    1. Error: A JNI error has occurred, please check your installation and try again
    2. Exception in thread "main" java.lang.UnsupportedClassVersionError: com/android/tools/r8/D8 has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0
    3. at java.lang.ClassLoader.defineClass1(Native Method)
    4. at java.lang.ClassLoader.defineClass(Unknown Source)
    5. at java.security.SecureClassLoader.defineClass(Unknown Source)
    6. at java.net.URLClassLoader.defineClass(Unknown Source)
    7. at java.net.URLClassLoader.access$100(Unknown Source)
    8. at java.net.URLClassLoader$1.run(Unknown Source)
    9. at java.net.URLClassLoader$1.run(Unknown Source)
    10. at java.security.AccessController.doPrivileged(Native Method)
    11. at java.net.URLClassLoader.findClass(Unknown Source)
    12. at java.lang.ClassLoader.loadClass(Unknown Source)
    13. at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    14. at java.lang.ClassLoader.loadClass(Unknown Source)
    15. at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)

    报错就是说java1.8太老了,带不动了。

    所以,切换java版本,运行了,继续报错。

    3、命令换了

    1. C:\Users\Administrator\Desktop\gson>d8
    2. Exception in thread "main" java.lang.RuntimeException: Invalid invocation.
    3. Usage: d8 [options] [@<argfile>] <input-files>
    4. where <input-files> are any combination of dex, class, zip, jar, or apk files
    5. and each <argfile> is a file containing additional arguments (one per line)
    6. and options are:
    7. --debug # Compile with debugging information (default).
    8. --release # Compile without debugging information.
    9. --output <file> # Output result in <file>.
    10. # <file> must be an existing directory or a zip file.
    11. --lib <file|jdk-home> # Add <file|jdk-home> as a library resource.
    12. --classpath <file> # Add <file> as a classpath resource.
    13. --min-api <number> # Minimum Android API level compatibility (default: 1).
    14. --pg-map <file> # Use <file> as a mapping file for distribution.
    15. --intermediate # Compile an intermediate result intended for later
    16. # merging.
    17. --file-per-class # Produce a separate dex file per class.
    18. # Synthetic classes are in their own file.
    19. --file-per-class-file # Produce a separate dex file per input .class file.
    20. # Synthetic classes are with their originating class.
    21. --no-desugaring # Force disable desugaring.
    22. --desugared-lib <file> # Specify desugared library configuration.
    23. # <file> is a desugared library configuration (json).
    24. --desugared-lib-pg-conf-output <file>
    25. # Output the Proguard configuration for L8 to <file>.
    26. --main-dex-rules <file> # Proguard keep rules for classes to place in the
    27. # primary dex file.
    28. --main-dex-list <file> # List of classes to place in the primary dex file.
    29. --main-dex-list-output <file>
    30. # Output resulting main dex list in <file>.
    31. --force-enable-assertions[:[<class name>|<package name>...]]
    32. --force-ea[:[<class name>|<package name>...]]
    33. # Forcefully enable javac generated assertion code.
    34. --force-disable-assertions[:[<class name>|<package name>...]]
    35. --force-da[:[<class name>|<package name>...]]
    36. # Forcefully disable javac generated assertion code.
    37. # This is the default handling of javac assertion code
    38. # when generating DEX file format.
    39. --force-passthrough-assertions[:[<class name>|<package name>...]]
    40. --force-pa[:[<class name>|<package name>...]]
    41. # Don't change javac generated assertion code. This
    42. # is the default handling of javac assertion code when
    43. # generating class file format.
    44. --force-assertions-handler:[:[|...]]
    45. --force-ah:[:[|...]]
    46. # Change javac and kotlinc generated assertion code
    47. # to invoke the method with each
    48. # assertion error instead of throwing it.
    49. # The is specified as a class name
    50. # followed by a dot and the method name.
    51. # The handler method must take a single argument of
    52. # type java.lang.Throwable and have return type void.
    53. --thread-count # Use of threads for compilation.
    54. # If not specified the number will be based on
    55. # heuristics taking the number of cores into account.
    56. --map-diagnostics[:]
    57. # Map diagnostics of (default any) reported as
    58. # to where and
    59. # are one of 'info', 'warning', or 'error'
    60. # and the optional is either the simple or
    61. # fully qualified Java type name of a diagnostic.
    62. # If is unspecified, all diagnostics at
    63. # will be mapped.
    64. # Note that fatal compiler errors cannot be mapped.
    65. --android-platform-build
    66. # Compile as a platform build where the runtime/bootclasspath
    67. # is assumed to be the version specified by --min-api.
    68. --art-profile
    69. # Rewrite human readable ART profile read from and write to .
    70. --startup-profile
    71. # Startup profile to use for dex layout.
    72. --version # Print the version of d8.
    73. --help # Print this message.

    最显著的变化有两个:

    1、不再需要 --dex 命令

    2、不能直接转化成.dex文件,需要zip或者jar后缀。

  • 相关阅读:
    Hystirx熔断降级机制
    【考研复试】计算机相关专业面试英语自我介绍范文(一)
    基于学生成绩管理系统(附源代码及数据库)
    js正则表达式
    CSS中:root伪类的使用
    手写实现call() apply() bind()函数,附有详细注释,包含this指向、arguments讲解
    跨域方案的抉择
    简历编写指南及注意事项
    SSH框架(Struts2+Spring+Hibernate)搭建整合详细步骤
    图像相似度对比分析软件,图像相似度计算方法
  • 原文地址:https://blog.csdn.net/John_Lenon/article/details/132791938