简体   繁体   English

在Android项目中使用Maven库

[英]Using maven library in android project

I have a certain library project which does a certain work. 我有一个图书馆项目,可以做一些工作。 The library is built using maven. 该库是使用maven构建的。

I now want to include this library in an android project. 我现在想将此库包含在android项目中。 I added the library jar as a compile dependency in gradle and I can successfully use the library in my android code. 我在gradle中将库jar添加为编译依赖项,并且可以在我的android代码中成功使用该库。

I have JDK 8 installed and I build the library using it. 我已经安装了JDK 8,并使用它来构建库。 But, as I have read, android uses Java 7. Since the library is built using JDK 8, can this cause a problem? 但是,正如我读过的,android使用Java7。由于该库是使用JDK 8构建的,这会引起问题吗?

If it can cause problems, I don't think building the library using JDK 7 would solve it either, since the library depends on other maven libraries from external maven repository. 如果它可能引起问题,我也不认为使用JDK 7构建库也可以解决该问题,因为该库依赖于来自外部maven存储库的其他maven库。 Is there anything I can do about it? 有什么我可以做的吗?

This is a common JDK lifecycle problem. 这是一个常见的JDK生命周期问题。 The good news is there are things you can do to make sure that everything in your build is compliant with a certain JDK. 好消息是您可以做一些事情来确保构建中的所有内容都与某个JDK兼容。

First off, make sure that your module is indeed compiled with the latest JDK version you are willing to accept. 首先,请确保您的模块确实已使用您愿意接受的最新JDK版本进行编译。 You can set the compiler plugin to only generate bytecode that is compliant to certain version, for instance JDK 7. 您可以将编译器插件设置为仅生成与某些版本兼容的字节码,例如JDK 7。

For example: 例如:

 <build>
    <plugins>
       <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.3</version>
        <configuration>
          <source>THE_JDK_VERSION_YOU_WANT</source>
          <target>THE_JDK_VERSION_YOU_WANT</target>
        </configuration>
      </plugin>
    </plugins>
  </build>

NOTE 注意

There is a drawback to this and that is that while setting a specific source and target to the compiler, the code may still use JDK features that aren't available in the target environment's JRE. 这有一个缺点,那就是在为编译器设置特定的sourcetarget ,代码可能仍会使用目标环境的JRE中不可用的JDK功能。 For example, having set JAVA_HOME to JDK8 and source and target to 1.7 will still allow the code to use (say) ConcurrentHashMap.mappingCount() which came in JDK8. 例如,将JAVA_HOME为JDK8并将sourcetarget为1.7仍将允许代码使用(例如)JDK8中附带的ConcurrentHashMap.mappingCount()

The answer to this problem is to use the animal-sniffer plugin. 这个问题的答案是使用animal-sniffer插件。 This plugin will check your code for any usage of disallowed API:s, such as JDK8, if you configure it that way. 如果您以这种方式进行配置,此插件将检查您的代码中是否有禁止使用的API:s的用法,例如JDK8。 This plugin was previously hosted by Codehaus, but it will come up if Google around a bit. 这个插件以前是由Codehaus托管的,但是如果Google稍作改动,它就会出现。 I can provide you with a working example tomorrow when I get back to work. 明天上班时,我可以为您提供一个工作示例。

Next, as you pointed out, you have your dependencies. 接下来,正如您所指出的,您具有依赖性。 Fortunately, the enforcer plugin can use an extended rule-set, called <enforceBytecodeVersion> that can check that all of your dependencies are also compliant with a specific bytecode version. 幸运的是, enforcer插件可以使用扩展的规则集,名为<enforceBytecodeVersion>可以检查所有的依赖性也符合特定字节码的版本。 All the details are available here . 所有详细信息都可以在这里找到

EDITED 已编辑

Here comes the configuration for the animal-sniffer plugin. 这是animal-sniffer插件的配置。 There's a newer version available (1.14), but it didn't work for me. 有一个更新的版本(1.14),但对我而言不起作用。 Maybe you'll have better luck. 也许您会有更好的运气。 The API signatures for the JDK are available at Maven Central. 在Maven Central中可以找到JDK的API签名。

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>animal-sniffer-maven-plugin</artifactId>
  <version>1.13</version>
  <executions>
    <execution>
      <id>check-for-jdk6-compliance</id>
      <phase>test</phase>
      <goals>
        <goal>check</goal>
      </goals>
    </execution>                
  </executions>
  <configuration>
    <signature>
      <groupId>org.codehaus.mojo.signature</groupId>
      <artifactId>java16</artifactId>
      <version>1.1</version>
    </signature>
  </configuration>
</plugin>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM