使用最新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命令,不能使用了。

使用最新android sdk 将jar文件编译成dex_第1张图片

2、jre环境报错。

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

使用最新android sdk 将jar文件编译成dex_第2张图片

不过随着各种软件,及编译环境的更新,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)

使用最新android sdk 将jar文件编译成dex_第3张图片

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

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

3、命令换了

C:\Users\Administrator\Desktop\gson>d8
Exception in thread "main" java.lang.RuntimeException: Invalid invocation.
Usage: d8 [options] [@] 
 where  are any combination of dex, class, zip, jar, or apk files
 and each  is a file containing additional arguments (one per line)
 and options are:
  --debug                 # Compile with debugging information (default).
  --release               # Compile without debugging information.
  --output          # Output result in .
                          #  must be an existing directory or a zip file.
  --lib    # Add  as a library resource.
  --classpath       # Add  as a classpath resource.
  --min-api       # Minimum Android API level compatibility (default: 1).
  --pg-map          # Use  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   # Specify desugared library configuration.
                          #  is a desugared library configuration (json).
  --desugared-lib-pg-conf-output 
                          # Output the Proguard configuration for L8 to .
  --main-dex-rules  # Proguard keep rules for classes to place in the
                          # primary dex file.
  --main-dex-list   # List of classes to place in the primary dex file.
  --main-dex-list-output 
                          # Output resulting main dex list in .
  --force-enable-assertions[:[|...]]
  --force-ea[:[|...]]
                          # Forcefully enable javac generated assertion code.
  --force-disable-assertions[:[|...]]
  --force-da[:[|...]]
                          # Forcefully disable javac generated assertion code.
                          # This is the default handling of javac assertion code
                          # when generating DEX file format.
  --force-passthrough-assertions[:[|...]]
  --force-pa[:[|...]]
                          # Don't change javac generated assertion code. This
                          # is the default handling of javac assertion code when
                          # generating class file format.
  --force-assertions-handler:[:[|...]]
  --force-ah:[:[|...]]
                          # Change javac and kotlinc generated assertion code
                          # to invoke the method  with each
                          # assertion error instead of throwing it.
                          # The  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  # Use  of threads for compilation.
                          # If not specified the number will be based on
                          # heuristics taking the number of cores into account.
  --map-diagnostics[:]  
                          # Map diagnostics of  (default any) reported as
                          #  to  where  and
                          #  are one of 'info', 'warning', or 'error'
                          # and the optional  is either the simple or
                          # fully qualified Java type name of a diagnostic.
                          # If  is unspecified, all diagnostics at
                          #  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  
                          # Rewrite human readable ART profile read from  and write to .
  --startup-profile 
                          # Startup profile  to use for dex layout.
  --version               # Print the version of d8.
  --help                  # Print this message.

最显著的变化有两个:

1、不再需要 --dex 命令

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

你可能感兴趣的:(android)