先分析清楚问题。需要联网的主要有两部分。首先,SDK、NDK更新有可能会联网。其次,Gradle需要联网下载项目使用的版本,或者查找项目中依赖的库。SDK、NDK的下载、配置很简单,不多介绍。这里重点说一下Gradle离线配置。

  • 按照本文下方复制的官方推荐方法配置好后,基础问题就解决了。
  • 接下来会遇到Gradle版本问题。自行下载所需的Gradle版本。解压到本地目录${GRADLE_ROOT}。打开设置File > Settings > Build, Execution, Deployment > Gradle,选中Use local Gradle distribution,然后在下面填写${GRADLE_ROOT}。这样Android Studio就会使用你自己的Gradle版本。
  • 下面遇到的是依赖库版本问题。build过程中,可能会遇到大量找不到对应版本依赖库的问题。
    Android Studio使用自动构建工具Gradle来管理项目的依赖库。在能连接到中心服的情况下,Android Studio会根据配置自行下载缺失的依赖库,但内网离线做不到这一点。我们先来看一下Android Studio使用的Gradle库来源:
  • 中心服。Maven服为https://repo.maven.apache.org/maven2/
  • 安装包内置的库:${INSTALL_ROOT}/gradle/m2repository/。
  • Gradle的缓存库${USER_ROOT}/.gradle/caches/。(Android Studio自行下载的依赖库都放在这里)
  • 官方提供的离线库组件:${USER_ROOT}/.android/manual-offline-m2/。
  • 自己创建的本地Maven库:${USER_ROOT}/.m2/repository。

一种简单粗暴的方式是在外网打开同一个工程,让Android Studio解决完依赖问题,然后复制上面的二、三、四项到内网的相同目录下。这种方法能解决当前的问题。如果开发中产生了新的库依赖,还是要整个再拷一遍。这里我们使用一种比较灵活的方法,即上面的最后一项,使用本地的Maven库。步骤如下:

  1. 下载Maven,解压,运行mvn -v。如果能打印出mvn的版本信息,表示mvn可以使用。
  2. 新建空目录${EMPTY_ROOT},目录下新建一个pom.xml文件,内容如下:
<project xmlns = "http://maven.apache.org/POM/4.0.0"
    xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.companyname.project-group</groupId>
    <artifactId>project</artifactId>
    <version>1.0</version>
    
    <dependencies>
        <!-- 复制Maven声明到这里。 -->   
    <dependencies>
</project>
  1. 当Android Studio构建报错的时候,按照报错信息到mvnrepository中搜索缺失的库,从版本列表中找到需要的版本,点击进入相应版本,把Maven页签下的内容复制到pom.xml添加注释的地方去。示例:
<!-- https://mvnrepository.com/artifact/org.jbpm/jbpm-flow -->
<dependency>
    <groupId>org.jbpm</groupId>
    <artifactId>jbpm-flow</artifactId>
    <version>7.28.0.Final</version>
</dependency>
  1. 打开命令行,进入${EMPTY_ROOT}目录,运行mvn clean compile
  2. 如果运行正常,你需要的库及其依赖库就下载到${USER_ROOT}/.m2/repository目录下了。
  3. 有些Android Studio库不在这个库里。如果下载不到,那就新建一个Android Studio工程,把这个库添加到工程依赖里,让Gradle去下载,下载完毕后,你需要的库存在${USER_ROOT}/.gradle/caches/module-2/files-2.1/${LIB_NAME}/${VERSION_NAME}目录下。这个目录下的内容和本地Maven库的内容不太一样。不要慌,它只是把同一个版本的文件分别放到一个唯一ID命名的目录下了。直接把这些文件从它们的目录中复制出来就好了。以guava为例,它的版本信息是:
    com.google.guava:guava:18.0它在Gradle缓存中的目录结构为:
caches/
	modules-2/
		files-2.1/
			com.google.guava/
				guava/
					18.0/
						abcdefgh..ijkl/
							guava-18.0.pom
						abcdefgh..ijkm/
							guava-18.0.jar

我们只需要复制guava-18.0.pom和guava-18.0.jar两个文件到本地Maven库目录中就好了:

repository/
	com/
		google/
			guava/
				guava/
					18.0/
						guava-18.0.pom
						guava-18.0.jar

猜测应该有转换工具能直接转换这两种格式。现在需求不大,没有去找。

  1. 复制本地Maven库到内网相同目录下。

你可能会觉得直接复制Gradle缓存目录会更容易。这种方法太繁琐,不过能让你非常清楚你的项目依赖了哪些库。而且也让你更熟悉这到底是怎么一回事,下次就不用再翻遍帖子求解决方法了。

官方提供方法

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, do the following:
开启Gradle的离线模式:如果你的带宽小或者不能联网,开启Gradle离线模式防止它在构建的时候下载缺失的依赖库。当开启离线模式时,如果你的项目缺失了某些依赖库,Gradle会抛出一个构建失败的报错,而不是尝试下载它们。按照以下步骤开启离线模式:

Click File > Settings (on macOS, 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.
单击File > Settings(Mac平台上为Android Studio > Preferences)打开设置对话框。
展开左侧面板的Build, Execution, Deployment选项,并单击Gradle
Global Gradle settings下,选中Offline work复选框。
单击Apply或者OK让你的修改生效。

Configure offline build dependencies
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.
配置离线构建依赖
如果你需要在没有网络的情况下构建项目,按照以下步骤配置IDE来使用Android Gradle Plugin和Google Maven依赖库这两个离线版本。

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:
下载解压离线组件
当你下载完离线组件的时候,解压到以下目录。如果以下目录不存在,你需要手动去创建它们。

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

To update the offline components, proceed as follows:
按照以下步骤更新离线组件:

Delete the content inside the manual-offline-m2/ directory.
Re-download the offline components.
Unzip the contents of the ZIP files you downloaded into the 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.
删除manual-offline-m2目录下的内容。
重新下载离线组件。
解压下载的离线组件到manual-offline-m2目录下。
包含离线组件到你的Gradle工程中。
为了让Android构建系统使用你下载的离线组件,你需要创建下面这样一个脚本。记住,你只需要创建一次这个脚本,后续更新离线组件不需要修改它。

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

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()
      }
    }
  }
}

Save the text file.
保存这个文件。

(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.
(可选的)如果你想验证离线组件是否按照预期工作。按照下面的示例,从你的工程的build.gradle文件中移除所有在线库。在确认你的工程在没有这些库的情况下构建正确后,你可以再把它们放回build.gradle文件中。

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()
    }
    ...
}

参考:https://developer.android.com/studio/intro/studio-config

参考:https://www.jianshu.com/p/e08467801869

Android Studio版本:Android Studio 3.5.1
官方离线组件版本:android-gradle-plugin-3.5.0-beta01@20191023, gmaven_stable@20191023