How to configure gradle to work "offline" (using cached dependencies)
Asked Answered
W

6

74

I have a command line apk generator which compiles a lot of APKs using the same source code, so these apps have the same dependencies.

In the gradle documentation I can see this:

The Gradle project needs network connectivity to download dependencies.

I know that it is possible to configure gradle to work offline and not download the same dependencies that it has downloaded for other apks. How can this offline mode be activated?

Wroughtup answered 23/8, 2015 at 21:5 Comment(0)
S
89

Gradle does a good job of avoiding re-downloading artifacts, but you can pass --offline to Gradle to prevent from accessing the network during builds.

e.g.

gradle --offline build

If it needs something from the network that it doesn't have, instead of attempting to fetch it, your build will fail.

Study answered 24/8, 2015 at 1:55 Comment(3)
oh, so it will not download any dependence that i allready haves downloaded? like for example google play services 7.5.0 etc..? are you 100% sure?Wroughtup
Can't get it to work. With --offline it just complains "No cached version listing .. available for offline mode". Even though moments earlier I've run the same build online, successfully. How can I get Gradle to actually cache these things?Moonset
But how do you tell it to prepare to go offline?Laidlaw
R
58

Android Studio

In Android Studio you can make gradle to build your apps fully offline by activating this option:

Settings -> Build, Execution, Deployment -> Build tools -> Gradle

enter image description here


Gradle CLI

In Gradle command line interface you can get this done by using --offline flag.

Specifies that the build should operate without accessing network resources.

Reseat answered 24/8, 2015 at 2:8 Comment(2)
Where does "Gradle home" Path must be set?Evalynevan
Settings menu is under File menu - so much for novices as me !Erinnerinna
C
42

Android Studio (v. 3.6.1 and above)

You can make gradle to build your apps fully offline by activating this option:

Here is the screenshot from the Android Studio Gradle Toolbar 👇

disconnect gradle sync

Closestool answered 12/3, 2020 at 13:39 Comment(2)
Oh Boy, I just retired mentally finding this option like old times. Thank you! It worked! Editing the screenshot view here.Nocturn
Thank you for the editing, it's better now. I am happy to hear that it helps.Foliar
B
4

Gradle build offline ( Build fast from cache or local repo) (Android Studio v3.0+)

Configure offline build dependencies (gradle + maven)

Important Note: The library or android gradle plugin version which is not present in offline repo then it will download from remote.

If you'd like to build your project without a network connection, follow the steps below to configure the IDE to use offline versions of the Android Gradle Plugin and Google Maven dependencies.

If you haven't already done so,download the offline components from the downloads page.

Download and unzip offline components

After you have downloaded the offline components, unzip their contents into the following directory, which you might need to create if it doesn’t already exist:

  1. On Windows: %USER_HOME%/.android/manual-offline-m2/
  2. On macOS and Linux: ~/.android/manual-offline-m2/

To update the offline components, proceed as follows:

  1. Delete the content inside the manual-offline-m2/ directory.
  2. Re-download the offline components.
  3. Unzip the contents of the ZIP files you downloaded into the <code>manual-offline-m2/ directory.

Include offline components in your Gradle project

To tell the Android build system to use the offline components you've downloaded and unzipped, you need to create a script, as described below. Keep in mind, you need to create and save this script only once, even after updating your offline components.

  1. Create an empty text file with the following path and file name:

    On Windows: %USER_HOME%/.gradle/init.d/offline.gradle

    On macOS and Linux: ~/.gradle/init.d/offline.gradle

  2. Open the text file and include the following script:

    def reposDir = new File(System.properties['user.home'], ".android/manual-offline-m2")
    def repos = new ArrayList()
    reposDir.eachDir {repos.add(it) }
    repos.sort()
    allprojects {
      buildscript {
        repositories {
              for (repo in repos) {
                    maven {
                    name = "injected_offline_${repo.name}"
                    url = repo.toURI().toURL()
                          }
          }
        }
      }
      repositories {
        for (repo in repos) {
          maven {
            name = "injected_offline_${repo.name}"
            url = repo.toURI().toURL()
          }
        }
      }
    }
    
  3. Save the text file.

  4. (Optional) If you’d like to verify that the offline components are working as intended, remove the online repositories from your project’s build.gradle files, as shown below. After you've confirmed that your project builds correctly without these repositories, you can put them back into your build.gradle files.

    buildscript {
        repositories {
    // Hide these repositories to test your build against
    // the offline components. You can include them again after
    // you&#39;ve confirmed that your project builds ‘offline’.
    // google()
    // jcenter()
        }
        ...
    }
    allprojects {
        repositories {
            // google()
            // jcenter()
        }
        ...
    }
    

Note: This script applies to all Gradle projects you open on the workstation.

Source: https://developer.android.com/studio/intro/studio-config#offline

  • Download Gradle plugin and Maven and setup from above documentation
  • If any problem occurs then follow my solution in below stackoverflow link

Android Studio: Configure offline build dependencies


Basanite answered 8/4, 2020 at 19:16 Comment(0)
C
0

I tried this approach to build in offline mode.

  1. I have copied the gradle distribution ZIP and kept it locally.
  2. Edited gradle-wrapper.properties for "distributionUrl" to point to the local file
  3. When I tried executing the build command in linux "./gradlew --offline build", it throws error:
A problem occurred configuring project ':MyApp'.
> Could not resolve all artifacts for configuration ':MyApp:classpath'.
   > Could not resolve com.android.tools.build:gradle:4.1.1
     Required by:
         project :MyApp
      > No cached version of com.android.tools.build:gradle:4.1.1 available for offline mode
Collocation answered 7/7, 2021 at 15:4 Comment(1)
Please start a new question, dont ask in answers!Gallard
B
0

To build upon https://mcmap.net/q/243445/-how-to-configure-gradle-to-work-quot-offline-quot-using-cached-dependencies, it's worth noting that if you've got an AAR and a .pom file and a handful of no documentation whatsoever from Google like me, the process in the linked answer above requires a few more steps:

  1. Create a directory under the ~/.android/manual-offline-m2 that will serve as your local repo e.g. my_local_deps
  2. Create one or more directories under your local repo folder to represent the artifact group name as manually nested folders (e.g. com/my/thing instead of one folder called com.my.thing)
  3. Create one folder whose name matches your artifact's module name or artifact id e.g. mylib
  4. Create one folder under the module folder from the previous step to serve as the version specifier e.g. a folder called 1.2.3
  5. Place your AAR and pom file under the version folder from the previous step

The end result should be something like ~/.android/manual-offline-m2/com/my/thing/mylib/1.2.3/mylib-1.2.3.aar and mylib-1.2.3.pom for an artifact that is referred to in Gradle parlance as "implementation 'com.my.thing:mylib:1.2.3'"

Now when you re-run Gradle and it runs the offline.gradle script from the linked answer, it should pick up your offline artifact. Easy, right? smh

Bisect answered 14/12, 2021 at 23:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.