Android Studio官方推薦內網離線配置


 先分析清楚問題。需要聯網的主要有兩部分。首先,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>
      
    3. 當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>
      
    4. 打開命令行,進入${EMPTY_ROOT}目錄,運行mvn clean compile

    5. 如果運行正常,你需要的庫及其依賴庫就下載到${USER_ROOT}/.m2/repository目錄下了。

    6. 有些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
      

      猜測應該有轉換工具能直接轉換這兩種格式。現在需求不大,沒有去找。

    7. 復制本地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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM