java.lang.NoClassDefFoundError: butterknife.ButterKnife - android

I've recently started doing Android development again and I am not yet used to the new gradle build system. Any dependency I add (so far I've tried Joda DateTime and butterknife), it always throws an error NoClassDefFoundError.
Here is what I've done. I'm using IntelliJ.
build.gradle:
compile 'joda-time:joda-time:2.1'
compile 'com.jakewharton:butterknife:4.0.1'
File > Project Structure > Modules > MyModule > Dependencies > Add Library, download from Maven, click export and bump it up the list.
My dependency list now includes a maven module com.jakewharton:butterknife:4.0.1
What am I doing wrong here? My IDE resolves everything fine, but at runtime none of the classes can be resolved.
Side note: My BUILD would not even complete until I added the above line to build.gradle. So I have to add the dependencies in Project > Project Structure AND to the build.gradle?
Also my project structure is:
UITest (parent)
UIBeta (android application)
UILib (android module)
I see the butterknife lib in the UITest\libs folder, shouldn't the children be able to find this?

Well I was going to delete this but maybe it will help someone else:
My gradle clean was broken because some aar was locked and it could not delete it, I had to close out IntelliJ, rename the aar (appcompat7 I think it was), then restart. I might not even have had to rename the file as I'm guessing the lock was released upon closing.
After restarting my clean worked, upon next run no more error.

Related

Android Studio generates R file only for modules with no module dependencies in multi-module project

After updating to AS 3.3.1 from 3.2.something I've got a strange bug: R file is generated only for modules that do not implement any other modules. I still can compile project or go to declaration via cmd+B, but every resource file like R.layout.activity_main is highlited with an "unresolved reference" message, until I remove every module implementation from module build.gradle and resync the project.
As far as I've noticed, the problem does not related with android gradle 3.3.0 or kotlin 1.3, since my old projects are affected as well.
What I've already tried:
Clean/Rebuild project
Invalidate cache
Resync Gradle
Switch to Android Gradle 3.2.1
delete .idea directory
Here's my project, module build.gradle example and used libraries.
For example, if I comment implementation(project(":presentation")) line in mainscreen/build.gradle.kts, the error disappears. Obviously, it isn't the solution.
UPD: SOLVED
Solution: you have to specify different package names in each AndroidManifest.xml files, i.e. com.mycompany.example.launcher for launcher module, com.mycompany.example.presentation for presentation module et cetera.
[solution1]
You can delete the old version of gradle from your system and start installing its new version.
[solution2]
Or remove Android Studio and reinstall it

change in build.gradle to sync gradle

I'm new to android.
I build a new project in android studio, but I can not sync the gradle.
I've searched and there were some recommendations which said the solution is making changes in module/build.gradle
I found two build.gradle but I don't know which one is module/build.gradle, however I did make change to both of them.(I added android method)
the first grdle file
second one
The tab you're looking is Project. It's like a File Explorer.
It says Project change it to Android from drop down menu.
That tab will have only the necessary files. There you will find both the Project gradle and module gradle.
The thing is you can find those files in Project also but it's easy and more convenient to work in Android Tab.
You not necessarily need to make changes to build.gradle in order to run a Gradle sync task. Let me first answer your first question:
I don't know which one is module build.gradle
When you crate a new Android studio project there are two build.gradle files you see. One in MyAppliaction/build.gradle and other is app/build.gradle.
Here MyAppliaction/build.gradle is the Project gradle file which is also called global gradle where you can configure project wide golbal configurations. As per your screenshot you have added depandencies{} in project build.gradle which is not correct(that's why your gradle sync is failed!), you can add depandencies object only in module build.gradle.
app/build.gradle is the "module" gradle file where you can configure module specific configs and dependencies. Module build.gradle will at least one for each module if you create one more library module in your project, that will have separate build.gradle.
but I can not sync the gradle
Yes, when you make any change to project or module build.gradle, you get a "Sync" button on top which can start gradle sync. But if you want gradle sync anytime just open on "Gradle" tab from right and click "sync" icon on top left. (see screenshot below)

Gradle project Sync failed , How to fix?

I imported a material Dialog library from Github.
https://github.com/drakeet/MaterialDialog
And I imported the library in Android Studio.
However, now The Android Studio says "Gradle project sync failed, basic functionality (editing, debugging) will not work properly.
I tried alternative answers to similar problems from other questions here,
Gradle project sync failed?
Android Studio Gradle project sync failed
But, none works for me .
The error I get in console is : Error:Dependency ZyiaAlarm:materialDialog:unspecified on project app resolves to an APK archive which is not supported as a compilation dependency. File: F:\ZyiaAlarm\materialDialog\build\outputs\apk\materialDialog-release-unsigned.apk
My doubt is: Any way to restore the things back, I mean to get the gradle as it was as I am a beginner, there's much to learn about gradle.
OR
anyway to fix this library I imported ?
Any help will be appreciated!
Too long for a comment.
It is not clear how do you import it.
If you would like to build locally this library, you should have this type of structure:
root
MaterialLib
build.gradle
app
build.gradle
build.gradle
settings.gradle
where MaterialLib is the folder MaterialDialog/library from github.
You haven't to import the other folders from github.
Also in
app/build.gradle you should have:
dependencies {
compile project(':MaterialLib')
}
and in the settings.gradle you should have:
include ':app',':MaterialLib'
However I suggest you avoiding the local library.
Remove the MaterialLib (don't import code from github).
Just add to your app/build.gradle these lines:
dependencies {
compile 'me.drakeet.materialdialog:library:1.2.2'
}
In this you should have this type of structure:
root
app
build.gradle
build.gradle
settings.gradle
and in the settings.gradle you should have:
include ':app'

Unable to find module with Gradle path. Linking to library -unspecified instead

Update 2 This has been fixed in Android Studio 1.2 Beta 3
Update: This has reportedly been fixed in an upcoming release of Android Studio.
In Android Studio 1.2 Beta 2 I started getting the following warnings when syncing Gradle.
Failed to set up dependencies
Warning:Unable to find module with Gradle path ':Library1'. Linking to library 'Library1-unspecified' instead.
Warning:Unable to find module with Gradle path ':Library2'. Linking to library 'Library2-unspecified' instead.
I've noticed that jumping to a declaration declared in one of the library modules pulls up the Decompiler instead of just going to the actual source in the library module.
In my Android Studio Project I have the following modules:
- App
- Library1
- Library2
Module: App build.gradle snippet:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':Library1')
compile project(':Library2')
}
settings.gradle file:
include ':App'
include ':Library1'
include ':Library2'
project(':Library1').projectDir = new File(settingsDir, '../library1/lib')
project(':Library2').projectDir = new File(settingsDir, '../Library2/lib')
I faced this with Android Studio 2.2.3. The problem was with the .iml file that was causing the problem.
After importing the module folder into the project directory and adding the changes to include module in settings.gradle, delete the .iml file from the module folder and do a Gradle Sync. Then right click on any module in the Project Window and click on "Configure Project Subset...", if the imported module is not checked, check it and Android Studio should do a Gradle Sync. By then the imported module will appear in the Project Window and will have a .iml file regenerated that is does not cause any issues.
delete the .iml file under the error module.
restart Android studio
Right click project, Select "Configure Project Subset ..." and select your module, rebuild your project.
I met the same problem on AS 2.0 preview,
and I fixed it by following steps:
1.Rename the module.
2.Change the names in app/build.gradle and setting.gradle.
3.Start gradle sync.
I don't know why but it works on my issue.
Check in settings. of your app.
Down there is the field "Load/Unload Modules"
Your module might be unloaded, that's why Android Studio ignores it even if you specify it in Gradle.
At least that was the problem for me.
If you are using React-native>60, then you should run npx jetify on root of your project before doing Gradle sync.
For me the following worked in Android Studio 3.5.3
First of all, check if the path is correct in your Gradle settings.
If the path is correct then invalidate caches & restart by navigating to the top 'File' menu
It is a known bug introduced in 1.2 Beta 2.
See This issue on the Android issue tracker. Fix should be out within hours.
I had this problem and solved it by clicking on
File> Ivalidate cache / Restart...
as shown below:
Problem:
This problem can also occur when you change the path to the module.
settings.gradle
include ':app', ':filechooserlibrary'
project(':filechooserlibrary').projectDir = new File('../../repos-github/file-chooser/file-chooser-library/filechooserlibrary')
I changed the path to '../../github/file-chooser/file-chooser-library/filechooserlibrary'
The old path did not exist any more. But then, instead of accepting the new path, Android Studio recreated the old path structure thus referring to an empty directory.
Solution:
So, I deleted this obsolete path structure and restarted Android Studio, after deleting the .iml file (I don't know if this is necessary, but it cannot do harm). Then I reimported the Gradle project and it worked.
Try using an updated version of NDK.
I faced the same issue.
I was using NDK version r17c. Issue solve after using r21e.
Android Studio Version: 4.1.3
OS: Ubuntu 18.04.5

Cannot resolve R.java, duplicate class

I just start using Android Studio for a week and it works great for me, but when I started Android Studio today I get the error: 'error: duplicate class: mypackage.R'. I saw this error before when I used Eclipse so I tried to rebuild the project a few times and restarting Android Studio, this didn't help.
After reading some Stackoverflow questions I tried to deleted R.java and rebuild again, now I don't get any error while rebuilding. The only problem is that Android Studio cannot resolve R, so every line which uses R gets an error (project builds and run).
I also tried to delete all the R.class files but this doesn't help either. I checked if some class imports android.R, but they all don't, they just import 'mypackage.R'. I even deleted all my files and checked out a older version of my project but I still have the same problem.
Edit:
If I don't delete R.java the compiler doesn't give any error. Just when I try to build I get a lot of errors like: 'error: cannot find symbol variable button_login'. If I search the R.java file for 'button_login' I just find it. I did this with multiple errors.
Edit2:
When I deleted all the libaries the project now builds. But the problem is that I need those libaries (HTTPComponents). I added them from maven (File --> Project Structure --> Libaries --> +-sign') and added them to my settings.graddle:
buildscript {
repositories {
maven { url 'http://repo1.maven.org/maven2' }
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4'
}
}
apply plugin: 'android'
dependencies {
compile files('libs/android-support-v4.jar')
compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.2.5'
compile group: 'org.apache.httpcomponents', name: 'httpmime', version: '4.2.5'
//compile 'org.apache.httpcomponents-httpclient:4.2.5' doesn't work either
//compile 'org.apache.httpcomponents-httpmime:4.2.5' doesn't work either
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 15
targetSdkVersion 16
}
}
When I run gradlew.bat --stacktrace --info assemble I get errors that Gradle is unable to find the libaries from the maven repository. I checked the repository but they are available. http://search.maven.org/#browse%7C1628757896
Unfortunately I run into this as well, sometimes frequently, mostly happens when I put my computer to sleep and back on while Eclipse is on. Sometimes recompiling and validating the project works.
I did find this solution online somewhere, lost the link and do not claim credits for this:
Disable auto build, and clean the project then
Right click your project -> properties -> Java Build Path -> Source -> Add Folder -> add gen and src
Then compile
If that doesn't work, try to delete the gen folder and do that process
Let me know.
Delete the Build folder generated by Android Studio automatically!
Problem is simple usually. You have fresh instal of studio? So you just fogot add SDK support for those version Android in wich try to compile project. Android Studio can`t generate R file. Start SDK manager, install all missing SDK version and rebuld project.
Another problem can be if you use 64bit OS. You ned to install 32 bit lib, because aapt can`t work with 64bit libs.
in Ubuntu install ia32-libs
Fedora described here
in other distr find libs in google (just google:ia32-libs for "mydistr"), i cant describe all of them ;)
P.S. sorry for my English ;)
Delete all class file in your_package_name(androisTest) folder only keep ApplicationTest file.
If you are using some library from which you are getting this error, just delete the build folder under your app source and rebuild the project. I was using OpenCV library for which I w as getting this error and that got resolved by deleting the build folder and rebuilding the project.
Delete all generated code inside "gen" folder.
Uncheck Project->Build Automatically in eclipse.
compile android source once again.
Error will be removed, worked for me.
This still happens sometimes in Android Studio, but a simple Build / Clean Project followed by a Build / Rebuild Project takes care of it.
I have not had this problem for a long time until today.
Error:(10, 14) java: duplicate class: com.domain.name.R
After 2 hours of wasting time on the project and getting this error,
Finally I removed the .idea, gen and out folders and also iml file.
Then I closed the project and then re-import it using import project of main menu again.
Its working well right now.

Categories

Resources