I am using gradle dependency of
implementation 'com.google.android.gms:play-services-location:15.0.1'
at runtime I get below error
java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/android/gms/location/LocationRequest;
I am trying to fetch lat long using LocationRequest and when I am using this code in a standalone project it works. And when I am trying to build a library with same code I get above error.
I have checked, both standalone project and library project have same dependencies and versions.
This maybe because of 2 reasons
1st possible reason
When you used this library directly you used implementation method in build.gradle.
So you can use it directly in simple app module.
When you move it to your library and use that location library using same implementation option that location library can be only used by your library. and can't be used by app module in which you have used your library
Try by replacing that implementation by api for location library like this
api 'com.google.android.gms:play-services-location:15.0.1'
For more details refer this post for implementation vs api - Here
2nd possible reason
As you mentioned in comments, its a runtime error
As per my opinion it maybe because of obfuscation by ProGuard
add that class to keep ProGuard rule like this
-keep class com.google.android.gms.location.** { *; }
So this will stop obfuscation of that Location Request class
The code is working now with below changes (I am still looking for an explnation)
in the library gradle edited to below dependency
compileOnly "com.google.android.gms:play-services-location:15.0.1"
and in the app's gradle file added below dependency
runtimeOnly 'com.google.android.gms:play-services-location:11.6.0'
my guess is, since "compileOnly" takes care of adding the dependency in library and "runtimeOnly" takes care of using that dependency the code works and is able to find LocationRequest class.
P.S adityakamble49 's answer in the thread also helped. Please try that as well as it might work for your case.
Related
I have a project with the following structure.
+-MyApplication
+-MyLibrayOne
+-MyLibrayTwo
MyApplication is my main application whereas MyLibrayOne and MyLibrayTwo are two libraries imported into project. MyApplication uses some classes of MyLibrayOne and MyLibrayOne uses some classes of MyLibrayTwo.
In the .gradle file of MyLibrayOne I have used - compile project(':MyLibrayTwo'). Everything works fine. But if I replace compile with implementation it can not import the classes from MyLibrayTwo. It gives error: cannot find symbol class XXXX error.
Use api instead of implementation will solve your problem. I.e.
dependencies {
// Enforce the runtime dependencies
api project(':MyLibrayOne')
api project(':MyLibrayTwo')
}
Simply to explain, api will let the depending project see all the classes of the depended projects but implementation cannot do this.
First I encourage you to have a look here in the Google I/O 2017.
compile and implementation are not the same thing.
compile lets the application access to all sub-dependencies that the sub-library has access to. This means that if MyLibrayOne depends on MyLibraryTwo and you need also access to the class from MyLibrarayTwo you'll have to direct gradle to expose the classes from MyLibrayTwo using the compile directive.
If this is not needed then implementation is enough.
I can only guess that your case is the former and so you'll need to keep using compile. Since compile is now deprecated use the api directive. They are the same.
Also, have a look here in the gradle documentation.
I made a library, but when I tried to use it with implementation 'com.example:mylibrary:1.3.0' in my app's build.gradle, I keep getting an error saying the ConstraintLayout dependency (which the library uses but not the app) is not found. However it was explicitly defined in the library's build.gradle with implementation.
When I ran gradlew app:dependencies on the terminal, it shows that the library has no dependencies, even though it actually has 2. This seems to be the source of the problem, Gradle can't detect the dependencies of the library.
I didn't run into this problem for a while but when I decided to remove the ConstraintLayout dependency from my app an error appears during build.
When you're using implementation for the dependencies in your library, the project which is dependent with it will not see the dependencies. You need to use api instead of implementation.
I'm getting the following error while trying to use Work Manager. I've migrated my project to AndroidX and all other architecture components are working.
def work_version = "1.0.0-alpha02"
/* Work Manager for Background Tasks */
implementation "android.arch.work:work-runtime:$work_version"
implementation "android.arch.work:work-firebase:$work_version"
I'm quite sure I need some dependencies from the Support Library. But I have no clue which one's they are.
I've tried adding the annotations package since the error says it can't find a class file for RestrictTo$Scope. Still doesn't work.
implementation "com.android.support:support-annotations:28.0.0-alpha1"
You are including a reference to the wrong support library, you want the androidx one.
androidx.annotation
Look in
AndroidX refactoring
you will find
androidx.annotation:annotation:1.0.0-alpha1
to correspond with
com.android.support:support-annotations
Make sure that you have only libraries for one or the other kind in your dependencies (project wide).
(This is basically the same question as in Android: library project with Retrofit results in NoClassDefFoundError but for play-services-auth).
I'm trying to build an android library project which uses play-services-auth to access SmartLock for Passwords.
It seems to be working as long as I use api instead of implementation on my gradle-dependency for com.google.android.gms:play-services-auth. It seems that this is the only dependency needing this (all others can used with implementation).
When using implementation here as well, the calling app will crash with NoClassDefFoundError:
java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/android/gms/common/api/GoogleApiClient$Builder;
I'm using consumerProguardFiles with a bunch of proguard-rule-files (e.g. one for okhttp3, retrofit2 etc. from this repo). All this will then be packed into an AAR, so rules from the dependencies should be added as well.
It seems that I'm missing a proguard-rule for play-services-auth. Can somebody post the rules needed to use com.google.android.gms:play-services-auth in a library project?
Previously my gradle used to look like this and worked fine (apart from few registered bugs)
implementation 'com.dji:dji-sdk:4.3.2'
Now, after changing to
implementation 'com.dji:dji-sdk:4.4.0'
the Camera and other files cannot be recognized anymore. I am attaching a screenshot of the unrecognized imports.
However when I am trying to add
//dji-drones-sdk
implementation 'com.dji:dji-sdk:4.4.0'
provided 'com.dji:dji-sdk-provided:4.4.0'
I am getting "could not download dji-sdk-provided.jar"
Screenshot attached
All the examples and github codes are in version 4.3.2. Can anyone help me out?
Here is the link to the dji sdk
I have found the issue. After Gradle 3.4, the "provided" is replaced by "compileOnly"
I quote,
Gradle adds the dependency to the compilation classpath only (it is not added to the build output). This is useful when you're creating an Android library module and you need the dependency during compilation, but it's optional to have present at runtime. That is, if you use this configuration, then your library module must include a runtime condition to check whether the dependency is available, and then gracefully change its behavior so it can still function if it's not provided. This helps reduce the size of the final APK by not adding transient dependencies that aren't critical. This configuration behaves just like provided (which is now deprecated).
Hence using compileOnly in place of provided will do the trick.
Here is a link to the gradle changes documentation