我有一个Android应用程序,它依赖于我自己构建的一些android库。 如果我使用build.gradle中指定的库依赖项构建应用程序,那么我会得到以下异常。
-6694/com.mycompany.myproject D/AndroidRuntime: Shutting down VM
05-10 18:47:55.986 6694-6694/com.mycompany.myproject E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.mycompany.myproject, PID: 6694
java.lang.NoClassDefFoundError: Failed resolution of: Lorg/jacoco/agent/rt/internal_773e439/Offline;
at com.mycompany.ui.components.ListArrayAdapter.$jacocoInit(ListArrayAdapter.java)
at com.mycompany.ui.components.ListArrayAdapter.<init>(ListArrayAdapter.java)
at com.mycompany.myproject.NationalityFragment.onCreateView(NationalityFragment.java:47)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:339)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:602)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1220)
at android.app.Activity.performStart(Activity.java:5953)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
如果我删除了依赖项,而是从库中复制相关的类并运行应用程序,它运行得非常好。 此外,我还有其他依赖项,如junit和app-compat,这些似乎不会导致此问题。可能导致此问题的任何想法?
我的main build.gradle看起来像这样:
task wrapper(type: Wrapper) {
gradleVersion = '2.13'
}
buildscript {
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.0'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
}
}
subprojects {
project.ext.set('compileSdkVersion', 23)
project.ext.set('buildToolsVersion', '23.0.3')
project.ext.set('minSdkVersion', 19)
project.ext.set('targetSdkVersion', 23)
project.ext.set('versionCode', 1)
project.ext.set('versionName', '1.0-SNAPSHOT')
project.ext.set('javaVersion', JavaVersion.VERSION_1_7)
group 'com.mycompany.myproject'
version = project.ext.get('versionName')
repositories {
mavenLocal()
mavenCentral()
}
}
答案 0 :(得分:6)
更新Android Studio后出现此错误。我发现在设置中关闭Instant Run会修复它。
答案 1 :(得分:2)
根据here,您需要jacocoagent.jar
放置类路径。换句话说,您需要在gradle中编译相同版本的jacocoagent.jar
。
首先检查版本代码here(在您的情况下是版本0.7.4.201502262128
),然后从here下载jar。
将jacocoagent.jar
放入libs文件夹,然后在应用的compile fileTree(dir: 'libs', include: ['jacocoagent.jar'])
中添加build.gradle
。
答案 2 :(得分:0)
bijang是正确的。您需要添加jacocoagent.jar。而不是直接包括罐子,从maven拉下它。 jacocoagent的版本必须匹配jacocoplugin的版本,因为Offline.class包有一个commmit哈希。以下是我的依赖项:
编译&#39; org.jacoco:jacoco-maven-plugin:0.7.9&#39;
编译&#39; org.jacoco:org.jacoco.agent:0.7.9:运行时&#39;