Android - Change Library from Gradle Behaviour? - android

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.

Related

Android studio is not able to resolve import

I am trying to work on a sample project to learn MvRx. However, seems something is wrong. Android Studio is not able to find and import activityViewModel automatically.
1) I tried to import it manually by writing its package name but it is still gray.
2) From Gradle tab, I selected my root project and clicked on Refresh Gradle Project in order to refresh all dependencies. It did not help, too.
What is the problem?
If someone is getting same error even when using activityViewModel() in fragment, this answer may be helpful.
In my case I was using activityViewModel() inside fragment. Still I was getting this as well as many other errors. Finally I figured out that mvrx is now using kotlin coroutines and all dependencies on rxjava are removed in 2.0.0-beta1.
To solve this use
implementation "com.airbnb.android:mvrx-rxjava2:2.0.0-beta3"
in place of
implementation "com.airbnb.android:mvrx:2.0.0-beta3"
in dependencies section of your build.gradle.
Your feature code must be in a Fragment (that extends BaseMvRxFragment), not in an Activity.
Because you have other com.airbnb.mvrx. references that have resolved correctly, it means you do have a reference to the com.airbnb.mvrx library. However, it is likely that you have a different version of the library referenced than the sample's original author. Look in your build.gradle file for dependencies and see if you have the library referenced there. If so, compare its version to the one in the sample, if different, then modify your reference to the sample's version number. If not, then add a reference to the library with the appropriate version number of the library where that object exists.
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.airbnb.mvrx:mvrx:12.0.1'
}

How to download libraries in Android Studio?

Whenever I have to add certain library from the internet to my Android project, I add them inside the dependencies in the app level gradle script and it downloads the library for me. Is it possible to download these library files so that I can use them in other projects as well without downloading the whole library and dependency files again?
Just go to Maven central and download the libraries.
For example, here is Volley. Just click the download JAR button.
I would strongly recommend sticking with Gradle / Maven, though, to keep consistency with versions and appropriately handle additional dependencies for the libraries you want to download. They are called package managers for a reason, and they do their job well.
The libraries are actually downloaded to disk only once and shared between projects, they aren't downloaded for every new project.
Put library's jar file inside libs folder.
Add this line in module level build.gradle (if not present):
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
// Other libraries
}
Find the gradle dependency for your library and put it in dependencies of your build.gradle file. For Example,
dependencies {
// other dependencies
compile 'com.google.code.gson:gson:2.6.2'
}
and then build the gradle file.
I'd just run into an issue where a coworker's AS instance wasn't syncing with the server. In that case, we simply had to manually invoke the "Sync Project with Gradle File". For whatever reason, his AS instance wasn't doing that automatically.

Gradle dependencies update

One of the advantages of using Gradle in Android Studio is that it helps in dependency management. So if I have used a particular version of a library in my build.gradle file,
then how will I force it to update the dependency version once the higher version is available?
Some of the dependencies in my build.gradle are specified as
dependencies {
compile project(':facebookSDK')
compile files('libs/picasso-2.1.1.jar')
compile files('libs/crouton-1.8.1.jar')
}
One of the advantages of using Gradle in Android Studio is that it helps in dependency management.
Not the way that you are using it.
So if i have used a particular version of a library in my build.gradle file, then how will i force it to update the dependency version once the higher version is available?
In your case, you would download the new JARs, put them in libs/, and update your build.gradle to match.
The preferred approach is for you to delete those JARs and replace your two compile files statements with ones that pull down the dependencies from Maven Central or another artifact repository. You can find the proper statements for popular open source libraries like those via the Gradle, please site.
In your case, you would use:
compile 'com.squareup.picasso:picasso:2.3.3'
compile 'de.keyboardsurfer.android.widget:crouton:1.8.5'
These will require you to also have a repositories closure as a peer to your dependencies closure:
repositories {
mavenCentral()
}
This may already exist.
These compile statements still pin you to a specific version of those libraries, but moving to a new version would be a simple matter of updating the compile statement, and Gradle will pull down the new dependency on your next build.
If you want, you could replace part of the version number with a wildcard (e.g., 2.3.+). This will cause Gradle to automatically update to new patchlevels of the library, in this case. Some developers do not approve of this approach, as while it is convenient, it does reduce your ability to be able to reproduce a build (e.g., you need to recompile some older branch of your code, and now you don't know what version of the artifact you were using back then).
As you are compiling files from your local project, I don't think you can automatically compile a new individual jar version if available. What you can do instead of compiling individual files is:
compile fileTree(dir: 'libs', include: '*.jar')
This will compile all jars in the libs directory so you will always have the latest version.
Both the libraries you are using are available to be compiled as dependencies from mavencentral.
compile 'de.keyboardsurfer.android.widget:crouton:1.8.5'
compile 'com.squareup.picasso:picasso:2.3.3'
If you want to ensure you are getting the latest versions is you use a plus in place of the version number. It's up to you how open you want to be with this.. so
compile 'de.keyboardsurfer.android.widget:crouton:1.+'
compile 'com.squareup.picasso:picasso:2.+'
will give you the latest version under the 1. or 2. versioning cycles...
If you want, you could replace part of the version number with a wildcard (e.g., 2.3.+).
This will cause Gradle to automatically update to new patch-levels of the library, in this case.
Some developers do not approve of this approach, as while it is convenient it does reduce your ability to be able to reproduce a build (e.g., you need to recompile some older branch of your code, and now you don't know what version of the artifact you were using back then).
I have found Gradle, please to be my answer here.
Easily get the latest android library gradle compile statement.

add external jar to an android project *manually* (without eclipse etc)

my problem is simple:
in the web there are numerous ways and tutorials on how to include jars in an android application through eclipse, android studio etc.
I want to do it manually; so, I want to know which files do I have to edit to bind my app
with the specific jars.
in the web there are numerous ways and tutorials on how to include jars in an android application through eclipse, android studio
That is because the use of third-party libraries is tied to the build system being used to build the app.
I want to do it manually
It is unclear what "manually" means in this context.
If you mean that you are using Ant, just put the JAR(s) in your project's libs/ directory, and you are done. Note that this will work with Eclipse as well.
If you mean that you are using Gradle, you will need something like this in your build.gradle file:
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
Or, if the JARs can be found in a Maven or Ivy repository, you can reference those as well, by defining the repository in the repositories block and then simply specifying the artifact in the compile directive:
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.guava:guava:11.0.2'
}

Proper way to add global library in android-studio/gradle

First of all, I know how to add a local library to the build.gradle file, it was discussed in several questions here already (which are all basically the same), see here, here and here. But you have to hardcode the paths in the compile files('/path/to/lib.jar') statements in the build.gradle file, which isn't nice, not redistributable, etc, IF you use a library not within the project's folder structure. I prefer to maintain this library for all my projects in the same place (so it is always up to date for all projects etc.). So I would like to know how to add a library, which is not available via Maven, to an Android-Studio project using gradle, in a sane way, given that the library is added as a global library in AS's preferences.
What I have done so far:
I use Google's new Android-Studio, which uses gradle for the build management, to build an Xposed framework module. For that, I have to include an external library, XposedLibrary, which I downloaded from the respective Github repository to keep it up-to-date.
It contains the jar XposedLibrary/XposedBridgeApi.jar, which I added in AS as a global library (Ctrl+Shift+Alt+S -> Global Libraries -> green plus to add the folder XposedLibrary). The compilation failed, complaining that it doesn't know the imported classes. So I had to manually add the library to the build.gradle file, adding the respective line in the dependencies like so:
dependencies {
compile files('libs/android-support-v4.jar')
compile files('/home/sebastian/dev/android/XposedMods/XposedLibrary/XposedBridgeApi.jar')
}
I tried out to just add compile files('XposedBridgeApi.jar') or compile files('XposedLibrary/XposedBridgeApi.jar') but this didn't work.
So, what is a nice way to add an AS global library to the dependencies without using full paths? (I don't like the idea of symlinking to the jar file from within the lib/ folder ;) )
when referencing a file via
files("relative/path/to/a.jar")
the relative path is evaluated relative to the buildscript this snippet is in. so when your build.gradle file is located in let's say '/a/project/build.gradle' then the jar should be in '/a/project/relative/path/to/a.jar'. In a multiproject gradle build you can put the the jar in a folder relative to the root - project and reference it in all subprojects via
rootProject.files("relative/to/root/a.jar")
hope that helps,
cheers,
René
This post describes how to get XposedBridgeApi.jar working with Gradle in Android Sudio: http://forum.xda-developers.com/showpost.php?p=41904291&postcount=1570
I think here is the proper way:
Import Xposed in Android Studio
Edit the /app/build.gradle like this:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
provided fileTree(dir: 'deps', include: ['*.jar'])
}
The best way is to use "provided files('src/XposedBridgeApi-54.jar')" as the lib isn't allowed to be included in the module, because the XposedBridge is already installed on the phone.
With Android Studio, you have to first understand that the IDE uses the same model for a project that your command line build (gradle) uses. That is why the Project Structure dialog has a pop up that says edits here will have no effect. So adding a global library will also have no effect.
The correct way to fix such issues is to edit your gradle build scripts so that the command line gradle build works properly. Then you should just have to click on "Tools | Android | Sync Project with Gradle files" menu item to refresh the project structure in the IDE.
Finally, if your dependencies are not going to be in Maven Central, then you'd have to create a local maven repository. Read the thread here: https://groups.google.com/d/msg/adt-dev/eCvbCCZwZjs/vGfg-4vNy9MJ for background.

Categories

Resources