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 have an Android project with the following dependencies:
-- Android App
---> MySDK.Jar
------> 'org.apache.commons:commons-lang3:3.5'
This is MySDK.jar that has a dependency on commons-lang3.
I'm working on Android Studio and I'm thus using Gradle.
Here is my problem:
I have shared "MySDK.Jar" to someone and he has built his own Android App on top of it.
It works but we have seen that the compiler doesn't notice the missing dependency on 'org.apache.commons:commons-lang3:3.5'. At run-time there will be a crash if the code using 'org.apache.commons:commons-lang3:3.5' is called. One may not notice the problem if he doesn't call the code using this library.
I know that we can solve this issue by adding the following line to Android App build.gradle file:
compile 'org.apache.commons:commons-lang3:3.5'
I'm wondering if there is a way to get a compile error indicating such missing dependencies? It is indeed better to see the dependency problem at compilation time rather than at runtime.
What are the recommended good practices for this?
Thanks!
commons-lang3 is a transitive dependency of Android App. As such, it is often not needed for compilation - there are exceptions, especially regarding multiple levels of inheritance. So at compile time you (usually) do not know whether you miss a transitive dependency that you need at runtime.
This is where Gradle comes in. Gradle can (as Maven) resolve dependencies transitively from a Maven repository (as MavenCentral). If you put MySDK into a Maven repository (like Nexus or Artifactory, which have open source versions), everyone using MySDK will automatically draw commons-lang3 so you will not miss anything at runtime.
If you are just adding the jar file in your project you can't warning about the missing dependencies.
To do it you have to publish the jar file in a maven repo.
In this way you have a pom file which describes the dependencies that gradle has to download.
Provide a method like MySDK.init() int your MySDK.jar,call a method whe is belong to org.apache.commons:commons-lang3:3.5' in the MySDK.init() method, then put init() into onCreate() of your Application,
Another way is,putorg.apache.commons:commons-lang3:3.5 into MySDK.jar,
Hope it helps you :)
I updated Android Studio from 2.x to 3.x last week end. The project migration was perfect, build was great. And now, from 2 hours, I can't explain why, I can't build, I have this error on Glide now:
Error:(26, 22) error: cannot find symbol class GlideApp
All was good before, I didn't change anything (gradle or configuration), and now this error appears...
For information about Glide, in my Gradle:
annotationProcessor 'com.github.bumptech.glide:compiler:4.0.0-RC1'
compile 'com.github.bumptech.glide:glide:4.0.0-RC1'
compile 'com.github.bumptech.glide:okhttp3-integration:4.0.0-RC1'
The GlideApp file is generated automatically (I checked it).
So, it's a crazy situation. Thank you very much guys!
Some of these situations tend to pop up from time to time (for an instance with the generated class R, where it ends up not being generated). The easiest way I have solved this in the past is to clean the project and rebuild it. And if that doesn't work, press File in the top menu and press "Invalidate caches and restart". A second popup will appear, press "Invalidate caches and restart". It may take a while as Android Studio needs to re-index and rebuild the project, but it solves most issues I have had.
The same happened to me and it was one of weirdest errors I ever got,
I used Butterknife in my project and I found out that if you define the views in private mode, you get this error
use:
#BindView(R.id.tv_option_one)
TextView tv_option_one;
#BindView(R.id.tv_option_two)
TextView tv_option_two;
instead of
#BindView(R.id.tv_option_one)
private TextView tv_option_one;
#BindView(R.id.tv_option_two)
private TextView tv_option_two;
it mostly happens when Butterknife can't find the view when you use bindView or the onClick annotation, and the worst part is that it shows errors everywhere except the place where they should be.
I ran into the same problem when I migrated to AndroidX. I ended up solving it by adding/updating the dependencies to
implementation 'com.github.bumptech.glide:glide:4.8.0-SNAPSHOT'
kapt 'com.github.bumptech.glide:compiler:4.8.0-SNAPSHOT'
kapt 'androidx.annotation:annotation:1.0.0-rc01'
This also happens when you have another error in your code. Gradle doesn't get to generate Glide classes since another error is thrown before it
Glide 4.x introduces some breaking changes. Be sure to follow the instructions in the Download & Setup page of Glide docs.
Do not neglect the changes in proguard.cfg. After changes, rebuild your project and you should be able to access GlideApp.
add those dependencies like so
implementation 'com.github.bumptech.glide:glide:4.8.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
In my case, I forgot to apply kapt plugin, So I just added it to my module-level build.gradle.
apply plugin: 'kotlin-kapt'
You can find more details about it - Glide Docs
I have picked up this problem when a sub-package uses GlideApp with AppGlideModule being extended in a class one level up in the package hierarchy.
I suspect that the compiler tries to compile the sub-package classes before generating GlipeApp.
My solution was to extend AppGlideModule in a separate module and then add it as a dependency to all the modules using GlideApp.
If you are using DI, you could try comment out GlideApp error code, then rebuild. IDE should you where the error truly is.
I got the solution of this issue.
Build -> Clean project (After) Rebuild Project
Then after you will able to import the GlideApp in your project
For Reference
https://github.com/bumptech/glide/issues/1945
For me, the problem was that there was a different import error (which was a "valid" error, I had indeed deleted the referenced method).
But because I got like 20 messages that GlideApp can't be found when building the app, I didn't even notice that there was such an error.
After correcting that error and rebuilding the project, everything worked normally again.
Generation of the following class worked for me:
import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.module.AppGlideModule;
#GlideModule
public final class MyAppGlideModule extends AppGlideModule {
// Dummy. Needed to generate GlideApp. See:
// GlideApp.with(service).
}
I'm trying to import this as a library in a project I'm working on in Android Studio:
https://github.com/d4rken/myolib
I cant workout exactly how to do it. even before I try to do this, I tried to open the project using new->Import project-> (selecting the settings.gradle). This then complained about not knowing about:
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.3.1'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
So I tried adding these manually using the advice on:
Importing github projects as library to existing project
Adding external library in Android studio
But its still complaining about no cached version for offline mode. If I do disable work in offline mode then, well it just sits there forever. I want to give it everything manually because in my experience disabling work in offine mode usually doesn't help.
I completely understand this isn't a new topic on Stackoverflow. The issue of how best to import libraries as .jar, .aar, .os etc. etc has been covered a few times. And yet most answers are subtly different and some work sometimes, others work other times.
Does anyone know of a detailed explanation about how I should achieve this ?
If anyone has been successful in importing this project and using its libraries I will be eternally grateful if you could tell me how you achieved such wizardry.
You import the library through your app's build.gradle file. An import section looks something like this:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:24.1.1'
compile "com.android.support:support-v13:24.1.1"
}
So to import the library you just linked, you would simply add this line to your dependencies section, as per the readme:
compile 'eu.darken.myolib:myolib:0.0.4'
If I include a library via gradle for an android project, can I change the source code of said library (for instance to force a specific typeface) and have that code not get modified each time the code gets update?
Thanks,
Here are a couple of ways to consider going about this:
You should be able to download the specific version of the library you need and place it in the libs directory, edit the code you need to change, then in your build.gradle make sure that you have the following:
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
This will prevent gradle from pulling the latest version of the library from its repository. You will have to manually update in the future if you want to go this route.
If the methods you need to change are not private, you can create a child class and perform method overriding to suit your needs.