android studio离线升级 linux,Android Studio 离线编译

引言

我们日常工作中在网络畅通的情况下,大家都会选择在线下载依赖。但是,几乎每个人都遇到过被墙或者网络掉链子的情况。这时android studio 可能会因为依赖包下载不了而出现编译报错。配置离线组件就可以用来应付这种紧急情况,不至于因为依赖包无法下载而没法工作。本文就介绍如何配置android studio离线组件。

1. 离线组件

下载后,解压到%USER_HOME%/.android路径:

Windows:

C:\Users[user_name]\.android\manual-offline-m2\gmaven_stable

Mac/Linux:

~/.android/manual-offline-m2/gmaven_stable

其中manual-offline-m2和gmaven_stable两级目录需要自行新建,命名不是强制固定的,但一定要确保存放在%USER_HOME%/.android下,并且要确保.android到具体的依赖包如:android、androidx、com、org等中间还有两级目录。

2. 离线配置文件

在%USER_HOME%/.gradle目录下新建init.d目录,然后在init.d目录下新建offline.gradle文件。

Windows:

C:\Users[user_name]\.gradle\init.d\offline.gradle

Mac/Linux:

~/.gradle/init.d/offline.gradle

offline.gradle文件内容如下:

def reposDir = new File("C:/Users/administrator", ".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()

}

}

}

}

注意:

offline.gradle中的路径,需要改为自己电脑的离线组件路径

这里整理了一个android studio 3.2.1实测可用的离线依赖包,理论上android studio 3.5及其以下版本都可以使用。

链接: https://pan.baidu.com/s/1mN3IRw4kYOg6PYxrvyqEcw

提取码: wi5e

其中含离线组件和配置文件,下载后解压到%USER_HOME%,将下载的.android和.gradle与原有.android和.gradle合并,然后修改一下offline.gradle中的路径即可。

3. 离线编译

配置好离线组件,我们就可以离线编译了。离线编译前需要把Project的build.gradle文件中的google()和jcenter()注释掉,这样就会默认使用离线组件。除此之外,在使用上离线编译和在线编译没有任何区别。

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {

repositories {

/*google()

jcenter()*/

}

dependencies {

classpath 'com.android.tools.build:gradle:3.2.1'

// NOTE: Do not place your application dependencies here; they belong

// in the individual module build.gradle files

}

}

allprojects {

repositories {

/*google()

jcenter()*/

}

}

task clean(type: Delete) {

delete rootProject.buildDir

}

另外,再分别贴上app中support版本和androidx版本的build.gradle版本配置,android studio 3.2.1实测离线编译通过。

support:

dependencies {

implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation 'com.android.support:appcompat-v7:28.0.0'

implementation 'com.android.support.constraint:constraint-layout:1.1.0'

testImplementation 'junit:junit:4.12'

androidTestImplementation 'com.android.support.test:runner:1.0.2'

androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

}

androidx:

dependencies {

implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation 'androidx.appcompat:appcompat:1.0.2'

implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

testImplementation 'junit:junit:4.12'

androidTestImplementation 'androidx.test:runner:1.1.1'

androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'

}

注意,离线编译只能使用离线组件目录中已有的组件,基本上只有android studio的默认组件,很少第三方组件。如果项目需要第三方组件,需要自己手动下载。当然,你也可以在网络好的情况下,下载好项目所需的全部组件,然后拷贝到离线组件目录下,这样下次网络不好的情况下,你的项目仍然能正常离线编译。显然,这样是非常麻烦的。我的建议是:在网络畅通的情况下,要毫不犹豫地选择在线编译。只有在网络完全不行的情况下,才考虑使用离线编译。

4. 更新离线组件

更新离线组件的步骤:

b. 解压下载的新的离线组件压缩包,将解压出的内容:android、androidx、com、org等等拷贝到%USER_HOME%\manual-offline-m2\gmaven_stable 目录中。

这里为了保持版本兼容性,没有按官网教程直接删掉原有依赖组件,而是选择将新旧版本合并,这样合并之后,新旧版本的依赖包,我们就都有了,这样将会有更好的兼容性。

5. 参考

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值