How to create an android studio project offline?

Viewed 1807

I want to create my android projects offline. I've tried several techniques but none of them worked for me! I've downloaded offline components (Android Gradle Plugin & Google Maven dependencies) from developers.android.com website for creating offline projects, but the problem is that I don't know how to make them work with android studio? Please help!

3 Answers

As said here https://developer.android.com/studio/build/optimize-your-build#offline_mode (Android Studio < 3.6)

If you are on a slow network connection, your build times may suffer when Gradle attempts to use network resources to resolve dependencies. You can tell Gradle to avoid using network resources by using only the artifacts that it has cached locally.

To use Gradle offline when building with Android Studio, proceed as follows:

  • Open the Preferences window by clicking File > Settings (on Mac, Android Studio > Preferences).
  • In the left pane, click Build, Execution, Deployment > Gradle.
  • Check the Offline work checkbox.
  • Click Apply or OK.

If you're building from the command line, pass the --offline option.

[EDIT]

https://developer.android.com/studio/releases/#3.6-gradle-offline-ui (Android Studio > 3.6)

New location to toggle Gradle's offline mode To enable or disable Gradle's offline mode, first select View > Tool Windows > Gradle from the menu bar.

Then, near the top of the Gradle window, click Toggle Offline Mode Gradle offline button in the Gradle panel..

Whenever you add a dependency, you need to download it. If so, you need to have a connection.

You can also import all dependencies once with a network connection and then switch to offline mode.

The instructions you are looking for is in the README file included in the zip file downloaded from Android Developers website.

But before you proceed with the instructions you might want to know that:

Even though it is in the main download page and looks new and up to date, it is not! Its last modified is 2019/05/07 and accordingly the last Android Gradle Plugin Version included is 3.4.0 which is pretttttty old... (This is checked today 2021/06/27)

Use the Android Gradle Plugin offline

This document describes how to use Android Studio with downloads of offline Android Gradle plugin and Google Maven repository dependencies.

Download and unzip offline components

If you haven’t already done so, download the offline components from the official Android Studio website.

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: %USER_HOME%/.android/manual-offline-m2/.

To update these components, simply re-download the offline components, unzip their contents into your %USER_HOME%/.android/manual-offline-m2/ directory, and replace any files, as needed.

Include offline components in your Gradle project

After you download and unzip the offline components, you need to create a script to tell Gradle to use include the offline components you downloaded when it searches for your project’s Android Gradle plugin and Google Maven dependencies.

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

To create the script, proceed 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: %USER_HOME%/.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()
      }
    }
  }
}
  1. Save the text file.
  2. (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’ve confirmed that your project builds ‘offline’.
        // google()
        // jcenter()
    }
    …
}
allprojects {
    repositories {
        // google()
        // jcenter()
    }
    ...
}

Theoretically you can work offline by going to File > Settings > Build, Execution, Deployment > Gradle and checking Offline Work. You will unfortunately have to sync the project while on a working internet connection at least once to have the necessary dependencies downloaded for your project.

Related