使用最新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的问题慢慢出现了,也出现了各种不兼容的情况。

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

这次编译,报错了:

Error: A JNI error has occurred, please check your installation and try again
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
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$100(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)

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

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

3、命令换了

C:\Users\Administrator\Desktop\gson>d8
Exception in thread "main" java.lang.RuntimeException: Invalid invocation.
Usage: d8 [options] [@<argfile>] <input-files>
 where <input-files> are any combination of dex, class, zip, jar, or apk files
 and each <argfile> is a file containing additional arguments (one per line)
 and options are:
  --debug                 # Compile with debugging information (default).
  --release               # Compile without debugging information.
  --output <file>         # Output result in <file>.
                          # <file> must be an existing directory or a zip file.
  --lib <file|jdk-home>   # Add <file|jdk-home> as a library resource.
  --classpath <file>      # Add <file> as a classpath resource.
  --min-api <number>      # Minimum Android API level compatibility (default: 1).
  --pg-map <file>         # Use <file> as a mapping file for distribution.
  --intermediate          # Compile an intermediate result intended for later
                          # merging.
  --file-per-class        # Produce a separate dex file per class.
                          # Synthetic classes are in their own file.
  --file-per-class-file   # Produce a separate dex file per input .class file.
                          # Synthetic classes are with their originating class.
  --no-desugaring         # Force disable desugaring.
  --desugared-lib <file>  # Specify desugared library configuration.
                          # <file> is a desugared library configuration (json).
  --desugared-lib-pg-conf-output <file>
                          # Output the Proguard configuration for L8 to <file>.
  --main-dex-rules <file> # Proguard keep rules for classes to place in the
                          # primary dex file.
  --main-dex-list <file>  # List of classes to place in the primary dex file.
  --main-dex-list-output <file>
                          # Output resulting main dex list in <file>.
  --force-enable-assertions[:[<class name>|<package name>...]]
  --force-ea[:[<class name>|<package name>...]]
                          # Forcefully enable javac generated assertion code.
  --force-disable-assertions[:[<class name>|<package name>...]]
  --force-da[:[<class name>|<package name>...]]
                          # Forcefully disable javac generated assertion code.
                          # This is the default handling of javac assertion code
                          # when generating DEX file format.
  --force-passthrough-assertions[:[<class name>|<package name>...]]
  --force-pa[:[<class name>|<package name>...]]
                          # Don't change javac generated assertion code. This
                          # is the default handling of javac assertion code when
                          # generating class file format.
  --force-assertions-handler:<handler method>[:[<class name>|<package name>...]]
  --force-ah:<handler method>[:[<class name>|<package name>...]]
                          # Change javac and kotlinc generated assertion code
                          # to invoke the method <handler method> with each
                          # assertion error instead of throwing it.
                          # The <handler method> is specified as a class name
                          # followed by a dot and the method name.
                          # The handler method must take a single argument of
                          # type java.lang.Throwable and have return type void.
  --thread-count <number> # Use <number> of threads for compilation.
                          # If not specified the number will be based on
                          # heuristics taking the number of cores into account.
  --map-diagnostics[:<type>] <from-level> <to-level>
                          # Map diagnostics of <type> (default any) reported as
                          # <from-level> to <to-level> where <from-level> and
                          # <to-level> are one of 'info', 'warning', or 'error'
                          # and the optional <type> is either the simple or
                          # fully qualified Java type name of a diagnostic.
                          # If <type> is unspecified, all diagnostics at
                          # <from-level> will be mapped.
                          # Note that fatal compiler errors cannot be mapped.
  --android-platform-build
                          # Compile as a platform build where the runtime/bootclasspath
                          # is assumed to be the version specified by --min-api.
  --art-profile <input> <output>
                          # Rewrite human readable ART profile read from <input> and write to <output>.
  --startup-profile <file>
                          # Startup profile <file> to use for dex layout.
  --version               # Print the version of d8.
  --help                  # Print this message.

最显著的变化有两个:

1、不再需要 --dex 命令

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值