简体   繁体   English

Gradle提供的JAR依赖项无法按预期工作

[英]Gradle provided JAR dependency not working as expected

I've been stuck on this problem all day, I'm hoping someone can help me get my program running. 我整天都在解决这个问题,希望有人可以帮助我让程序运行。

I am building an Annotation Processor for Android, it currently has two modules: "mymodule" and "mymodule-compiler" 我正在为Android构建注释处理器,它目前具有两个模块:“ mymodule”和“ mymodule-compiler”

mymodule-compiler depends on mymodule: mymodule-compiler取决于mymodule:

dependencies {
  compile project(':mymodule')
  ...
}

mymodule uses the netflix "provided" plugin for Android: mymodule使用适用于Android的netflix“提供”插件:

apply plugin: 'nebula.provided-base'

dependencies {
  provided 'com.google.android:android:4.1.1.4'
}

This actually works perfectly, however when I run my compiler unit test suite, a lot of tests fail because the Android dependency specified is v4.1.1.4 and I actually need v6.0. 这实际上可以完美地运行,但是当我运行编译器单元测试套件时,许多测试失败,因为指定的Android依赖项为v4.1.1.4,而我实际上需要v6.0。 v6.0 is not available on Maven (and looks like it never will be because Google haven't updated it since 2012). v6.0在Maven上不可用(而且看起来永远不会,因为Google自2012年以来就没有对其进行更新)。

I found out that the Android SDK provides a "Unit Test" JAR version of Android for each release, which is the entire SDK "stubbed out". 我发现,Android SDK为每个发行版提供了一个Android的“单元测试” JAR版本,这是整个SDK的“残缺”。 I assume the versions on Maven are stubbed out too(?). 我假设Maven上的版本也已存根(?)。 I can easily grab that JAR and put it in my project under libs/. 我可以轻松地抓取该JAR并将其放在libs /下的我的项目中。

Now when I update the mymodule gradle file: 现在,当我更新mymodule gradle文件时:

dependencies {
  provided files('libs/android.jar')
}

My project still compiles correctly and I can see v23 files! 我的项目仍然可以正确编译,并且可以看到v23文件! However, when I run my unit tests, I see the following error: 但是,当我运行单元测试时,我看到以下错误:

!!! JUnit version 3.8 or later expected:

java.lang.RuntimeException: Stub!
  at junit.runner.BaseTestRunner.<init>(BaseTestRunner.java:5)
  at junit.textui.TestRunner.<init>(TestRunner.java:54)
  at junit.textui.TestRunner.<init>(TestRunner.java:48)
  at junit.textui.TestRunner.<init>(TestRunner.java:41)
  at com.intellij.rt.execution.junit.JUnitStarter.junitVersionChecks(JUnitStarter.java:205)
  at com.intellij.rt.execution.junit.JUnitStarter.canWorkWithJUnitVersion(JUnitStarter.java:188)
  at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:65)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

It shouldn't matter that the Android JAR is stubbed out because I never execute Android code, I only need it for imports. 取消存储Android JAR无关紧要,因为我从不执行Android代码,只需要将其用于导入即可。

Does anyone have any ideas about what I'm seeing and how I could fix this? 有人对我所看到的内容以及如何解决此问题有任何想法吗?

EDIT 1: More info: 编辑1:更多信息:

If I remove the Android dependencies from mymodule entirely, and instead put the following into mymodule-compiler.gradle: 如果我从mymodule完全删除Android依赖项,而是将以下内容放入mymodule-compiler.gradle中:

dependencies {
  testCompile files('libs/android.jar')
  ...
}

All my tests will pass. 我所有的测试都会通过。 This is obviously not a fix though, as 'mymodule' does need the Android dependency. 不过,这显然不是解决方案,因为“ mymodule”确实需要Android依赖项。

EDIT 2: 编辑2:

Doing the same test as EDIT 1, but using the netflix provided scope instead of testCompile works too. 做与EDIT 1相同的测试,但是使用netflix提供的范围而不是testCompile也可以。 If I use the provided scope in both projects though, I get the same Stub! 如果我在两个项目中都使用提供的作用域,则会得到相同的Stub! exception. 例外。

[Worked out together with OP] [与OP合作]

The local jar contains additional classes over the maven jar which seem to interfere. 本地jar包含在maven jar上似乎干扰的其他类。 To get it working delete these from the jar (specifically in this case: all junit classes). 要使其正常工作,请从jar中删除这些文件(在这种情况下,尤其是:所有junit类)。

Comparison of the two jars (left is maven jar, right is local jar): 这两个jar的比较(左为maven jar,右为本地jar): 比较

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

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