java.lang.NoClassDefFoundError: retrofit2/Retrofit$Builder

Answered

I want to develop a plugin for Android Studio IDE, which function is fetching some data from network.So I decide to use Retrofit2.

I imported the jar to AS IDE, and the plugin shown in popup menu, it seemed to work.

But when I clicked it, the AS IDE reported an error. It seemed like that it could not load the Retrofit class. 

I try to find out the cause, but fail. So I attach the repository url, which is https://github.com/CoderLengary/IDE-Plugin-For-Test.

PS: This plugin doesn't have complex logic.

Here is the screenshot:

 

Here is the log:


```

java.lang.NoClassDefFoundError: retrofit2/Retrofit$Builder
at com.example.plugin.test.NetClient.createRetrofit(NetClient.kt:12)
at com.example.plugin.test.NetClient.access$createRetrofit(NetClient.kt:7)
at com.example.plugin.test.NetClient$api$2.invoke(NetClient.kt:9)
at com.example.plugin.test.NetClient$api$2.invoke(NetClient.kt:7)
at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
at com.example.plugin.test.NetClient.getApi(NetClient.kt)
at com.example.plugin.test.AbViewModel$api$2.invoke(AbViewModel.kt:10)
at com.example.plugin.test.AbViewModel$api$2.invoke(AbViewModel.kt:8)
at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
at com.example.plugin.test.AbViewModel.getApi(AbViewModel.kt)
at com.example.plugin.test.AbViewModel.login(AbViewModel.kt:16)
at com.example.plugin.test.GenerateAbAction.actionPerformed(GenerateAbAction.kt:23)
at com.intellij.openapi.actionSystem.ex.ActionUtil$1.run(ActionUtil.java:265)
at com.intellij.openapi.actionSystem.ex.ActionUtil.performActionDumbAware(ActionUtil.java:282)
at com.intellij.openapi.actionSystem.impl.ActionMenuItem$ActionTransmitter.lambda$actionPerformed$0(ActionMenuItem.java:292)
at com.intellij.openapi.wm.impl.FocusManagerImpl.runOnOwnContext(FocusManagerImpl.java:265)
at com.intellij.openapi.wm.impl.IdeFocusManagerImpl.runOnOwnContext(IdeFocusManagerImpl.java:107)
at com.intellij.openapi.actionSystem.impl.ActionMenuItem$ActionTransmitter.actionPerformed(ActionMenuItem.java:282)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at com.intellij.openapi.actionSystem.impl.ActionMenuItem.lambda$fireActionPerformed$0(ActionMenuItem.java:111)
at com.intellij.openapi.application.TransactionGuardImpl.runSyncTransaction(TransactionGuardImpl.java:82)
at com.intellij.openapi.application.TransactionGuardImpl.lambda$submitTransaction$1(TransactionGuardImpl.java:106)
at com.intellij.openapi.application.TransactionGuardImpl.submitTransaction(TransactionGuardImpl.java:115)
at com.intellij.openapi.application.TransactionGuard.submitTransaction(TransactionGuard.java:121)
at com.intellij.openapi.actionSystem.impl.ActionMenuItem.fireActionPerformed(ActionMenuItem.java:111)
at com.intellij.ui.plaf.beg.BegMenuItemUI.doClick(BegMenuItemUI.java:524)
at com.intellij.ui.plaf.beg.BegMenuItemUI.access$300(BegMenuItemUI.java:35)
at com.intellij.ui.plaf.beg.BegMenuItemUI$MyMouseInputHandler.mouseReleased(BegMenuItemUI.java:546)
at java.awt.Component.processMouseEvent(Component.java:6550)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3325)
at java.awt.Component.processEvent(Component.java:6315)
at java.awt.Container.processEvent(Container.java:2239)
at java.awt.Component.dispatchEventImpl(Component.java:4899)
at java.awt.Container.dispatchEventImpl(Container.java:2297)
at java.awt.Component.dispatchEvent(Component.java:4721)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4904)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4535)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4476)
at java.awt.Container.dispatchEventImpl(Container.java:2283)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4721)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:766)
at java.awt.EventQueue.access$500(EventQueue.java:98)
at java.awt.EventQueue$3.run(EventQueue.java:715)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:84)
at java.awt.EventQueue$4.run(EventQueue.java:739)
at java.awt.EventQueue$4.run(EventQueue.java:737)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:736)
at com.intellij.ide.IdeEventQueue.defaultDispatchEvent(IdeEventQueue.java:878)
at com.intellij.ide.IdeEventQueue._dispatchEvent(IdeEventQueue.java:823)
at com.intellij.ide.IdeEventQueue.lambda$dispatchEvent$8(IdeEventQueue.java:466)
at com.intellij.openapi.progress.impl.CoreProgressManager.computePrioritized(CoreProgressManager.java:704)
at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:465)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)


Caused by: java.lang.ClassNotFoundException: retrofit2.Retrofit$Builder PluginClassLoader[com.example.len.RetrofitTest, 1.3-SNAPSHOT] com.intellij.ide.plugins.cl.PluginClassLoader@37ffdb7b
at com.intellij.ide.plugins.cl.PluginClassLoader.loadClass(PluginClassLoader.java:75)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 64 more

```


Android Studio Info:

Version: Android Studio 3.6.1

Build: 192.7142.36

IntelliJ Idea Info:

Version: 2019.2.4 Ultimate Edition

Build: 192.7142.36

3 comments
Comment actions Permalink

I extract the jar and there's no Retrofit lib. I think it may be the reason for this issue.

0
Comment actions Permalink

Len,

Sorry for the late reply!

I have fetched and run your project with no issues. It builds and runs correctly on AS 3.6.

Anyway - since you're using Kotlin - have you considered using ktor?

0
Comment actions Permalink

Thanks, Jakub. Actually, I think I find the solution.

If you run Gradle->Tasks->build->jar to get an output jar, it doesn't contain any dependencies. So if you import this jar and run the plugin, the IDE will report an error, such as "java.lang.NoClassDefFoundError". To resolve this problem, you need to do this:

1. In project build.gradle file, declare all the dependencies by "compile", for example:

dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"

compile 'com.squareup.retrofit2:retrofit:2.5.0'
compile 'com.squareup.retrofit2:adapter-rxjava2:2.5.0'
compile 'com.squareup.retrofit2:converter-gson:2.5.0'

compile "com.github.akarnokd:rxjava2-swing:0.3.7"

compile "io.reactivex.rxjava2:rxjava:2.2.4"
compile "io.reactivex.rxjava2:rxkotlin:2.2.0"
}

2. Add a gradle task in the project app build.gradle file

task fatJar(type: Jar) {
archiveBaseName = project.name + '-all'
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}


3. Run this task instead of "Gradle->Tasks->build->jar", and you will get a new jar which contains all the dependencies. 

0

Please sign in to leave a comment.