Gradle offline how to cache the dependencies - android

I am doing nightly gradle builds on a server where I checkout the repositories from git and build them.However, there is a proxy where gradle cannot download any repository. I tried running gradlew offline mode then I get an error telling me that
"No cached version of" and then the name of the dependency.
This is obviously because I never downloaded the dependency, I was thinking of manually downloading the dependencies cache them and use that gradle cache on the server where builds can access it. My question is as follows :
Is there any way I can download a .jar file or .pom file manually and then cache them ?_
How would i cache a jar file in the $Home/.gradle/caches directory ? I tried just putting it there but it doesn't work .
any ideas?

Usually Gradle retrieves dependencies on demand, only if and when they are need. To make Gradle download all dependencies beforehand (and thus populate the local cache), you can use a task like
task resolveAllDependencies {
description "Resolves all transitive dependencies (e.g. to build offline later)."
doLast {
configurations.all {
it.resolve()
}
}
}
After running this task, you should be able to successfully build with --offline.

In Android Studio sync the dependencies once then go to
File > Settings > Build & Execution > Gradle > Enable Offline Mode > Ok!

Related

Add android dependencies manually to gradle caches

I need to add android dependencies manually to gradle caches directory, as the gradle build can not get it but I can download it from my browser. This is the error:
> Could not find aapt2-7.0.4-7396180-osx.jar (com.android.tools.build:aapt2:7.0.4-7396180).
Searched in the following locations:
https://dl.google.com/dl/android/maven2/com/android/tools/build/aapt2/7.0.4-7396180/aapt2-7.0.4-7396180-osx.jar
I manually downloaded aapt2-7.0.4-7396180-osx.jar file and put it in the following directory:
username/.gradle/caches/modules-2/files-2.1/com.android.tools.build/aapt2/7.0.4-7396180/18ae77ce0ff3ba9da85e4674c2c92751cd1dc973/
I found the directory name: "18ae77ce0ff3ba9da85e4674c2c92751cd1dc973" from this command:
shasum aapt2-7.0.4-7396180-osx.jar
After this, I run the gradle build again, But I got the same error again and the build process tries to download the library and did not noticed that I have already downloaded it. Should I do anything else to let the build process know that this file is already downloaded?
If this is not your case.
Should I do anything else to let the build process know that this
file is already downloaded?
Try in your build.gradle append explicitly path to lib. For me
it's look like this:
Path to:
AndroidStudioProjects/DiceRoller/app/build.gradle
Source:
dependencies {
...
implementation files("~/Downloads/aapt2-7.0.4-7396180-osx.jar")
}
Run:
gradle clean

Android Gradle - How to include local .jar files as dependencies from outside the project directory

I'm working on a project that's suppossed to be running offline and I'm trying to include local .jar files as dependencies inside the build.gradle (app)
The structure of the repo can be seen here:
The main project is inside the dev folder.
In the submodules folder I have all the app and build dependencies along with the android-sdk, gradle plugin and jdk.
Trying to include all my jar files inside the build.gradle(app) like this:
implementation fileTree(include: ['*.jar'], dir: '../../submodules/app_dependencies')
Doesn't work because I'm referencing a path outside of the project's directory and when I try to build the app I get The following error
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:androidDependencies'.
> Could not resolve all artifacts for configuration ':app:developDebugCompileClasspath'.
> Could not resolve org.jetbrains.kotlin:kotlin-android-extensions-runtime:1.3.61.
Required by:
project :app
> No cached version of org.jetbrains.kotlin:kotlin-android-extensions-runtime:1.3.61 available for offline mode.
> No cached version of org.jetbrains.kotlin:kotlin-android-extensions-runtime:1.3.61 available for offline mode.
> No cached version of org.jetbrains.kotlin:kotlin-android-extensions-runtime:1.3.61 available for offline mode.
> No cached version of org.jetbrains.kotlin:kotlin-android-extensions-runtime:1.3.61 available for offline mode.
Debugging this I see the message No meta-data file or artifact found for module 'org.jetbrains.kotlin:kotlin-android-extensions-runtime:1.3.61' in repository 'maven' when it's looking in the following path:
file:/C:/Users/my-user-name/Desktop/android_repo/client-android/dev/submodules/dependencies/build_dependencies/org/jetbrains/kotlin/kotlin-android-extensions-runtime/1.3.61/kotlin-android-extensions-runtime-1.3.61.jar
for some reason, even if I added ../../ in the filetree(dir), gradle is still searching inside the dev directory and my dependencies are above in the submodules.
Is there a way to specify a dynamic path inside build.gradle that's outside of the project's directory?
It works with files (in kotlin notation):
dependencies {
implementation(files("../../submodules/app_dependencies/your-library.jar"))
}
If you need to include all files from some folder, you can just build a list of all jars in some directory and use it as files() argument.

Can't build in offline mode. "No cached version listing for androidx.fragment:fragment:[1.2.0] available for offline mode"

I'd like to be able to work on my project offline. So I disable offline mode, press the "sync project with gradle files" button, then enable offline mode and try to build. However, every time I do, I get the error:
FAILURE: Build failed with an exception.
*What went wrong:
Could not determine the dependencies of task ':app:compileDebugRenderscript'.
> Could not resolve all task dependencies for configuration ':app:debugCompileClasspath'.
> Could not resolve androidx.fragment:fragment:[1.2.0].
Required by
project :app > androidx.fragment:fragment-ktx:1.2.0
> No cached version listing for androidx.fragment:fragment:[1.2.0] available for offline mode.
> No cached version listing for androidx.fragment:fragment:[1.2.0] available for offline mode.
I've also tried invalidate cache/restart and then gradle sync before enabling offline mode, with the same result.
I'm running Android Studio 3.6.3
gradle-wrapper.properties contains the fololowing:
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip
How can I make androidx.fragment:fragment available for offline mode?
There is most likely a dependency resolution problem. When multiple components use the same dependency but with a different version, Gradle goes online to figure out which version to use. That obviously doesn't work in offline mode.
I assume you're using a different fragment dependency (androidx.fragment:fragment) somewhere else, that is selected by default in online mode. Now in offline mode, since no such inspection is possible, Gradle looks for version 1.2.0 (needed by androidx.fragment:fragment-ktx:1.2.0) and can't find it because if was never downloaded/cached.
Better solution is to try making your dependencies refer to the same versions of conflicting sub-dependencies. You can inspect your dependency tree with ./gradlew app:dependencies and update dependencies that are clashing so they use the same versions (if possible).
The other solution is to enforce dependencies in Gradle. You can add something like this to your build.gradle file:
configurations.all {
resolutionStrategy {
if (project.gradle.startParameter.offline) {
// x.y.z being the version that Gradle picks in the online mode.
force 'androidx.fragment:fragment:x.y.z'
}
}
}

Gradle Dependencies Download Jars

I'm trying to find a gradle task that will ensure that all of the dependency artifacts are downloaded into the gradle cache. If I run ./gradlew :project:dependencies, I can look at $GRADLE_USER_HOME/caches, and I can see a lot of com.x.package/artifact/version/hash/ directories, but they mostly (only?) contain .pom files. How do I ensure that .jar files are also downloaded?
For context, I'm attempting to build a docker image that has the gradle cache pre-built to avoid downloading artifacts every time.
Try
task showMeCache << {
configurations.compile.each { println it }
}
If you run it , it should download dependencies and print the location of the cached dependencies.
It may depend on your platform but the artifacts should be stored here:
~/.gradle/caches/modules-2/files-2.1

Android Studio offline add `Android plugin for Gradle`

I use Android Studio 3.1.2 with Gradle 4.4 and have to force Gradle to do off-line work, because when I use it normally (on-line), it takes to long to sync and fails. At first I got this error:
No cached version of com.android.tools.build:gradle:3.1.2 available for offline mode.
Disable Gradle 'offline mode' and sync project
So I downloaded gradle-3.1.2.jar, put it in <project root>/libs/ and change build.gradle of my project to this state:
buildscript {
repositories {
google()
jcenter()
flatDir {
dirs 'libs'
}
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
}
}
allprojects {
repositories {
google()
jcenter()
}
}
Now it seems that previous error is solved but another one is raised:
Unable to load class 'com.android.tools.lint.gradle.api.ToolingRegistryProvider'.
Possible causes for this unexpected error include:<ul><li>Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.)
Re-download dependencies and sync project (requires network)</li><li>The state of a Gradle build process (daemon) may be corrupt. Stopping all Gradle daemons may solve this problem.
Stop Gradle build processes (requires restart)</li><li>Your project may be using a third-party plugin which is not compatible with the other plugins in the project or the version of Gradle requested by the project.</li></ul>In the case of corrupt Gradle processes, you can also try closing the IDE and then killing all Java processes.
How I can solve this problem?
Edit
I know the normal way is working on-line, at least for the first build. But there must be a way to add it off-line. Isn't there such way?
This is from android studio manual from this link.
Turn on Offline Mode for Gradle: If you have limited bandwitch, turn
on Offline Mode to prevent Gradle from attempting to download missing
dependencies during your build. When Offline Mode is on, Gradle will
issue a build failure if you are missing any dependencies, instead of
attempting to download them. To turn on Offline Mode, proceed as
follows:
Click File > Settings (on a Mac, Android Studio > Preferences) to
open the Settings dialog.
In the left pane, expand Build, Execution, Deployment and then click
Gradle.
Under Global Gradle settings, check the Offline work checkbox.
Click Apply or OK for your changes to take effect.
In android studio Go to setting menu > Built,Execution and development option > Gradle > mark/unmark on your offline work option and sync your project.try this.

Categories

Resources