Maven:在装有retrolambda-maven-plugin的应用程序和使用android-maven-plugin的DEX-ed中使用Java 8库

Lyu*_*riv 8 android maven java-8 android-maven-plugin retrolambda

我已经为我的内部使用写了一小组minilibraries.这是使用Maven构建的.这些库的目标是"常规"Java,GWT和Android.其中一些是用Java 8编写的,因为我没有打算在GWT或Android上运行它们,因此其他库是用旧的Java 6编写的,以支持这两个库.我有一个将我的libs完全迁移到Java 8的计划(在语言特性方面),我成功地在尚未发布的GWT 2.8.0上运行Java 8重写库.但是,我不能让Java 8重写的libs为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
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

是否可以配置Retrolambda插件来处理库类以及所有依赖项?或者我可以使用另一个字节码处理工具?


更新#1

我认为Retrolambda失败了我错了.再深入研究一下,我已经发现android-maven-plugin可以归咎于它,因为它直接从Maven存储库中选择未经过检测的JAR文件,而不是从target目录中选择.启用详细日志记录发现此伪代码命令由以下内容调用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
Run Code Online (Sandbox Code Playgroud)

我的想法是执行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>
Run Code Online (Sandbox Code Playgroud)

第2步:使用Retrolambda检测复制的依赖项

调用 retrolambda-maven-plugin

第3步:编译DEX文件

调用android-maven-plugin排除复制到目标目录的依赖项,因为它们都应该位于目标目录中

但是这也失败了,因为我找不到一种方法来排除工件被DEXed与android-maven-plugin.

如何抑制工件从存储在非检测状态的存储库中取出?

我的插件配置:

  • org.apache.maven.plugins:Maven的依赖关系的插件:2.10
  • net.orfjackal.retrolambda:retrolambda - Maven的插件:2.0.2
  • com.jayway.maven.plugins.android.generation2:Android的Maven的插件:3.9.0-rc.3

更新#2

Simpligility团队发布了Android Maven插件4.4.1,其中包含下面描述的场景.在插件更新日志中查看更多内容.

<plugin>
    <groupId>com.simpligility.maven.plugins</groupId>
    <artifactId>android-maven-plugin</artifactId>
    <version>4.4.1</version>
</plugin>
Run Code Online (Sandbox Code Playgroud)

示例场景可以在http://simpligility.github.io/android-maven-plugin/instrumentation.html找到

Lyu*_*riv 5

四个月后,经过一段时间的调查,我终于成功了.首先,关于解决方案的关键是修补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使用原始分支编译DEX和APK文件android-maven-plugin.

安装前叉:

#!/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
Run Code Online (Sandbox Code Playgroud)

完成.接下来,在您的fork中注册fork pom.xml并配置构建插件:

<!-- 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>
Run Code Online (Sandbox Code Playgroud)

它应该工作.


更新

最近我修了Proguard Maven mojo的叉子.

更新2

android-maven-plugin库所有者合并我的提交,所以伪像滤波定于下一个版本.