我已经编写了一组供我内部使用的小库。这是使用Maven构建的。这些库的目标是"常规"Java、GWT和Android。其中一些是用Java 8编写的,因为我没有打算在GWT或Android上运行它们,因此其他库是用旧的Java 6编写的,以支持这两个库。我有一个将我的库完全迁移到Java 8(就语言特性而言)的计划,并且我成功地在尚未发布的GWT 2.8.0上运行了Java 8重写的库。然而,我不能使Java 8重写库为Android应用程序编译。问题是Retrolambda (retrolambda-maven-plugin
插件)似乎只能处理当前的Maven模块类,而完全忽略了依赖类。因此,android-maven-plugin
使用以下命令破坏目标应用程序构建:
[INFO] UNEXPECTED TOP-LEVEL EXCEPTION:
[INFO] com.android.dx.cf.iface.ParseException: bad class file magic (cafebabe) or version (0034.0000)
[INFO] at com.android.dx.cf.direct.DirectClassFile.parse0(DirectClassFile.java:472)
[INFO] at com.android.dx.cf.direct.DirectClassFile.parse(DirectClassFile.java:406)
[INFO] at com.android.dx.cf.direct.DirectClassFile.parseToInterfacesIfNecessary(DirectClassFile.java:388)
[INFO] at com.android.dx.cf.direct.DirectClassFile.getMagic(DirectClassFile.java:251)
[INFO] at com.android.dx.command.dexer.Main.processClass(Main.java:665)
[INFO] at com.android.dx.command.dexer.Main.processFileBytes(Main.java:634)
[INFO] at com.android.dx.command.dexer.Main.access$600(Main.java:78)
[INFO] at com.android.dx.command.dexer.Main$1.processFileBytes(Main.java:572)
[INFO] at com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpener.java:284)
[INFO] at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:166)
[INFO] at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:144)
[INFO] at com.android.dx.command.dexer.Main.processOne(Main.java:596)
[INFO] at com.android.dx.command.dexer.Main.processAllFiles(Main.java:498)
[INFO] at com.android.dx.command.dexer.Main.runMonoDex(Main.java:264)
[INFO] at com.android.dx.command.dexer.Main.run(Main.java:230)
[INFO] at com.android.dx.command.dexer.Main.main(Main.java:199)
[INFO] at com.android.dx.command.Main.main(Main.java:103)
[INFO] ...while parsing foo/bar/FooBar.class
retrolambda-maven-plugin
配置如下:
<plugin>
<groupId>net.orfjackal.retrolambda</groupId>
<artifactId>retrolambda-maven-plugin</artifactId>
<version>2.0.2</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>process-main</goal>
<goal>process-test</goal>
</goals>
</execution>
</executions>
<configuration>
<target>1.6</target>
<defaultMethods>true</defaultMethods>
</configuration>
</plugin>
是否可以配置Retrolambda插件来处理库类以及所有依赖关系?或者我可以使用另一种字节码处理工具?
UPDATE # 1
关于Retrolambda失败,我想我错了。再深入研究一下,我发现这应该归咎于android-maven-plugin
,因为它直接从Maven存储库而不是从target
目录中选择未检测的JAR文件。启用详细日志记录发现android-maven-plugin
调用的伪代码命令:
$JAVA_HOME/jre/bin/java
-Xmx1024M
-jar "$ANDROID_HOME/sdk/build-tools/android-4.4/lib/dx.jar"
--dex
--output=$BUILD_DIRECTORY/classes.dex
$BUILD_DIRECTORY/classes
$M2_REPO/foo1/bar1/0.1-SNAPSHOT/bar1-0.1-SNAPSHOT.jar
$M2_REPO/foo2/bar2/0.1-SNAPSHOT/bar2-0.1-SNAPSHOT.jar
$M2_REPO/foo3/bar3/0.1-JAVA-8-SNAPSHOT/bar3-0.1-JAVA-8-SNAPSHOT.jar
我的想法是执行maven-dependency-plugin
插件,以获得这三个工件到$BUILD_DIRECTORY/classes
目录,让retrolambda-maven-plugin
仪器的依赖关系。假设:
步骤1:将依赖项复制到目标目录
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>process-classes</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<includeScope>runtime</includeScope>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
步骤2:使用Retrolambda
检测复制的依赖项调用retrolambda-maven-plugin
步骤3:编译DEX文件
调用android-maven-plugin
,不包括复制到目标目录的依赖项,因为它们都应该位于目标目录
但是这也失败了,因为我找不到一种方法来排除与android-maven-plugin
一起被DEXed的工件。
如何禁止从存储在非仪器状态下的存储库中提取工件?
我的插件配置:
- org.apache.maven.plugins: maven-dependency-plugin: 2.10
- net.orfjackal.retrolambda: retrolambda-maven-plugin: 2.0.2
- com.jayway.maven.plugins.android.generation2: android-maven-plugin: 3.9.0-rc.3
更新# 2
Simpligility团队已经发布了Android Maven Plugin 4.4.1,其中包含了下面描述的场景。查看插件变更日志。
<plugin>
<groupId>com.simpligility.maven.plugins</groupId>
<artifactId>android-maven-plugin</artifactId>
<version>4.4.1</version>
</plugin>
示例场景可在http://simpligility.github.io/android-maven-plugin/instrumentation.html
四个月后,经过一番调查,我终于成功了。首先,解决方案的关键是修补android-maven-plugin
。我在GitHub上分叉了原始插件,并添加了一些配置过滤器选项,允许根据组id,工件id及其各自的版本包含或排除工件。这对于配置retrolambda-maven-plugin
非常重要。整个工作流程与我在问题中提到的基本相同。
-
maven-compiler-plugin
启用Java 8语言特性支持。可选的,因为主要目标是处理Java 8的依赖项。 -
maven-dependency-plugin
将所有Java 8依赖项解压缩到当前项目构建目标目录中,以便进行进一步处理。 -
retrolambda-maven-plugin
使用Retrolambda插件处理获得的所有类文件 -
android-maven-plugin
使用原android-maven-plugin
的分支编译DEX和APK文件
安装fork:
#!/bin/bash
# The last upstream merge revision
PLUGIN_COMMIT=a79e45bc0721bfea97ec139311fe31d959851476
# Clone the fork
git clone https://github.com/lyubomyr-shaydariv/android-maven-plugin.git
# Ensure proper revision
cd android-maven-plugin
git checkout $PLUGIN_COMMIT
# Build the forked plugin, no tests
mvn clean package -Dmaven.test.skip=true
# Clone plugin JAR
cd target
cp android-maven-plugin-4.3.1-SNAPSHOT.jar android-maven-plugin-4.3.1-SNAPSHOT-$PLUGIN_COMMIT.jar
# Clone and modify pom.xml
cp ../pom.xml pom-$PLUGIN_COMMIT.xml
sed -i "s/<version>4.3.1-SNAPSHOT<\/version>/<version>4.3.1-SNAPSHOT-$PLUGIN_COMMIT<\/version>/g" pom-$PLUGIN_COMMIT.xml
# Update the plugin descriptor
unzip android-maven-plugin-4.3.1-SNAPSHOT-$PLUGIN_COMMIT.jar META-INF/maven/plugin.xml
sed -i "s/<version>4.3.1-SNAPSHOT<\/version>/<version>4.3.1-SNAPSHOT-$PLUGIN_COMMIT<\/version>/g" META-INF/maven/plugin.xml
zip android-maven-plugin-4.3.1-SNAPSHOT-$PLUGIN_COMMIT.jar META-INF/maven/plugin.xml
# Install the plugin
mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file -DpomFile=pom-$PLUGIN_COMMIT.xml -Dfile=android-maven-plugin-4.3.1-SNAPSHOT-$PLUGIN_COMMIT.jar
。接下来,在pom.xml
中注册fork并配置构建插件:
<!-- enable Java 8 for the current module -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!-- unpack Java 8 dependency classes to the current module build directory -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<phase>process-classes</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<includeScope>runtime</includeScope>
<includeGroupIds>foo-group,bar-group,baz-group</includeGroupIds>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<!-- Convert Java 8 to Java 6 -->
<plugin>
<groupId>net.orfjackal.retrolambda</groupId>
<artifactId>retrolambda-maven-plugin</artifactId>
<version>2.0.6</version>
<executions>
<execution>
<phase>process-classes</phase>
<goals>
<goal>process-main</goal>
<goal>process-test</goal>
</goals>
</execution>
</executions>
<configuration>
<defaultMethods>true</defaultMethods>
<target>1.6</target>
</configuration>
</plugin>
<!-- DEXify and build the APK excluding the Java 8 dependencies as they are already processed -->
<plugin>
<groupId>com.simpligility.maven.plugins</groupId>
<artifactId>android-maven-plugin</artifactId>
<version>4.3.1-SNAPSHOT-a79e45bc0721bfea97ec139311fe31d959851476</version>
<executions>
<execution>
<phase>package</phase>
</execution>
</executions>
<configuration>
<androidManifestFile>${project.basedir}/src/main/android/AndroidManifest.xml</androidManifestFile>
<assetsDirectory>${project.basedir}/src/main/android/assets</assetsDirectory>
<resourceDirectory>${project.basedir}/src/main/android/res</resourceDirectory>
<sdk>
<platform>19</platform>
</sdk>
<undeployBeforeDeploy>true</undeployBeforeDeploy>
<proguard>
<skip>true</skip>
<config>${project.basedir}/proguard.conf</config>
</proguard>
<excludes>
<exclude>foo-group</exclude>
<exclude>bar-group</exclude>
<exclude>baz-group</exclude>
</excludes>
</configuration>
<extensions>true</extensions>
<dependencies>
<dependency>
<groupId>net.sf.proguard</groupId>
<artifactId>proguard-base</artifactId>
<version>5.2.1</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</plugin>
应该可以。
更新
最近我修补了Proguard Maven mojo的分支。
更新2 android-maven-plugin
存储库所有者合并了我的提交,因此工件过滤被安排到下一个版本。