Android Studio upgrade to 3.0 tips Java.lang.NoClassDefFoundError
It took 2 hours to solve the problem, and there was a similar problem on Stack Overflow, but no one offered answers.
This error means that the runtime does not find the class, so it is common for you to compile the pass but not run.
After writing this to answer, hope that more people see and avoid, save everyone's energy.
Workaround
First write the method, the cause and the principle written in the back, the time rushed directly to see the solution.
The common occurrence of this error is caused by the use of provided
For example, my old configuration is as follows:
Project build.gradle
File:
buildscript { dependencies { classpath 'com.android.tools.build:gradle:3.0.1' // need delete in gradle3.x version classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' }}
Module build.gradle
File:
apply plugin: 'com.android.library'apply plugin: 'android-apt'android { // ...}configurations { provided}dependencies { // ... // Fyber Annotations provided 'com.fyber:fyber-annotations:1.3.0' apt 'com.fyber:fyber-annotations-compiler:1.4.0' // ...}
Change to the latest build.gradle3.x configuration:
Project build.gradle
File:
buildscript { dependencies { classpath 'com.android.tools.build:gradle:3.0.1' }}
Module build.gradle
File:
apply plugin: 'com.android.library'android { // ... // add this code to enable annotationProcessor javaCompileOptions { annotationProcessorOptions { includeCompileClasspath = true } }}dependencies { // ... // Fyber Annotations compileOnly 'com.fyber:fyber-annotations:1.3.0' annotationProcessor 'com.fyber:fyber-annotations-compiler:1.4.0' // ...}
After you change the above configuration build Project
, it runs successfully. If not, please look down:
Precautions:
The above Module build.gralde
configuration must be written in the use of annotations Module
!
For example Module A
MainActivity
, I used @FyberSDK
the annotations in, then I wrote the above configuration in Module A
the build.gralde
file.
@FyberSDKpublic class MainActivity extends BaseActivity<MainPresenter, MainModel> implements IBannerDelegate, IMainView, View.OnClickListener { //.... }
Supplement related knowledge
About APT's introduction
1. What is apt?
APT (Annotation processing tools) is a tool that handles annotations, detects source code files, finds Annotation, and automatically generates code based on annotations. The annotation processor can generate additional source files and other files based on annotation in the source file (the content of the file is determined by the writer of the annotation processor), and apt compiles the generated source files and the original source files. Generate the class file together.
2, Annotationprocessor
Annotationprocessor is one of the apt tools, which is a built-in framework developed by Google and does not need to be introduced and can be used directly in the Build.gradle file.
3, Android-apt
ANDROID-APT is an apt framework developed by a developer himself, where the source code is hosted, and with the release of the Android Gradle plugin version 2.2, the Android Gradle plugin provides the name Annotationprocessor function to completely replace android-apt, since this android-apt author on the official website announced that the latest Android Gradle plugin now supports Annotationprocessor, and warns and or blocks android-apt, and recommend that you use the official Android plugin annotationprocessor.
Other
New Configuration |
the corresponding outdated configuration |
Description |
Implementation |
Compile |
Module compilation is available, the user runtime of the module is available, and for a large number of projects using the library, the compilation time can be significantly improved because it reduces the build system to recompile some module. Most app/test because of the use of this configuration |
Api |
Compile |
Module compilation is available, and the user of the module is compiled and run, as is the case with obsolete compile. Usually the library module will use it, if the app module must use it, it has to be in its want to expose the API to the test module using |
Compileonly |
Provided |
Module compilation is available, but the user of module is not available at compile and run time. The same as the old provided. |
Runtimeonly |
apk |
module and its users, the runtime is available. It's the same as the outdated apk. |
At last
Others have tried a lot about the pit of Android Studio 3.0
When you encounter a pit, you can refer to:
Official Upgrade 3.0 Guide
Pits series Android Studio 3.0 configuration changes
Android Studio upgrade to 3.0 tips Java.lang.NoClassDefFoundError