Android开发环境

在墙内,搭个Android开发环境其实有点难,也有点慢。

gradle.properties

如下内容可以配置在全局/home/andy/.gradle/gradle.properties

1
2
3
4
5
6
7
8
9
10
11
#systemProp.https.proxyPort=1080
#systemProp.http.proxyHost=127.0.0.1
#systemProp.https.proxyHost=127.0.0.1
#systemProp.http.proxyPort=1080

#gradle ss socks代理
org.gradle.jvmargs=-DsocksProxyHost=127.0.0.1 -DsocksProxyPort=1080

#系统中的jdk可能不是我们想要的
org.gradle.java.home=/home/andy/apps/Android/android-studio/jre

build.gradle

https://maven.aliyun.com/mvn/view

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
maven { url 'https://maven.aliyun.com/repository/google/' }
maven { url 'https://maven.aliyun.com/repository/public/'}
maven { url 'https://maven.aliyun.com/repository/gradle-plugin'}


buildscript {
ext.kotlin_version = '1.2.51'

repositories {
maven{ url rootProject.file("repo-local") }
maven { url 'https://maven.aliyun.com/repository/google/' }
maven { url 'https://maven.aliyun.com/repository/public/'}
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
}
}

~/.gradle/下创建init.gradle文件

这种方式只会全局替换allprojects可的内容,对buildscript下无效。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
allprojects{
repositories {
def ALIYUN_JCENTER_URL = 'https://maven.aliyun.com/repository/public/'
def ALIYUN_GOOGLE_URL = 'https://maven.aliyun.com/repository/google/'
all { ArtifactRepository repo ->
if(repo instanceof MavenArtifactRepository){
def url = repo.url.toString()
project.logger.lifecycle "cat url = $url"

if (url.startsWith('https://jcenter.bintray.com/')) {
project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_JCENTER_URL."
remove repo
}
if (url.startsWith('https://dl.google.com/dl/android/maven2/')) {
project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_GOOGLE_URL."
remove repo
}
}
}
maven { url ALIYUN_JCENTER_URL }
maven { url ALIYUN_GOOGLE_URL }
}
}

国内全局加速Gradle依赖下载速度配置,把这个文件放到~/.gradle目录下既可文件名:init.gradle.kts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
* Created by Seal.Wu on 2020/7/11
* Description: Set up mirrors for Gradle Globally
*/
val MAVEN_REPOSITORY_URL = "https://maven.aliyun.com/repository/central"
val JCENTER_REPOSITORY_URL = "https://maven.aliyun.com/repository/jcenter"
val GOOGLE_REPOSITORY_URL = "https://maven.aliyun.com/repository/google"
val GRADLE_PLUGIN_REPOSITORY_URL = "https://maven.aliyun.com/repository/gradle-plugin"

gradle.settingsEvaluated {
pluginManagement {
// Print repositories collection
println("Plugins Repositories names: " + repositories.names)

// Clear repositories collection
repositories.clear()

// Add my Artifactory mirror
repositories {
maven {
name = "Aly Gradle Plugin Repo"
url = uri(GRADLE_PLUGIN_REPOSITORY_URL)
}
}

// Print repositories collection
println("Now Plugins Repositories names : " + repositories.names)
}
}

allprojects {
repositories {
all {
if (this is MavenArtifactRepository) {
val url = url.toString()
when {
url.startsWith("https://repo1.maven.org/maven2")|| url.startsWith("https://repo.maven.apache.org/maven2/") -> {
setUrl(MAVEN_REPOSITORY_URL)
}
url.startsWith("https://jcenter.bintray.com/") -> {
setUrl(JCENTER_REPOSITORY_URL)
}
url.startsWith("https://dl.google.com/dl/android/maven2") -> {
setUrl(GOOGLE_REPOSITORY_URL)
}
}
}
}
}
buildscript {
repositories {
all {
if (this is MavenArtifactRepository) {
val url = this.url.toString()
when {
url.startsWith("https://repo1.maven.org/maven2")||url.startsWith("https://repo.maven.apache.org/maven2/") -> {
setUrl(MAVEN_REPOSITORY_URL)
}
url.startsWith("https://jcenter.bintray.com/") -> {
setUrl(JCENTER_REPOSITORY_URL)
}
url.startsWith("https://dl.google.com/dl/android/maven2") -> {
setUrl(GOOGLE_REPOSITORY_URL)
}
}
}
}
}
}
afterEvaluate {
repositories {
val lastUsedRepos = filterIsInstance<MavenArtifactRepository>().map {
it.name + "(${it.url})"
}
if (lastUsedRepos.isNotEmpty()) {
println("Use these repositories at last :\n $lastUsedRepos")
}
}
buildscript {
repositories {
val lastUsedRepos = filterIsInstance<MavenArtifactRepository>().map {
it.name + "(${it.url})"
}
if (lastUsedRepos.isNotEmpty()) {
println("Use these repositories at last in build script:\n $lastUsedRepos")
}
}
}
}
}

as 3.6编译输出中文乱码

打开D:\Android\Android Studio\bin\studio64.exe.vmoptions,添加:

1
-Dfile.encoding=UTF-8

修改.gradle目录位置

默认.gradle~/.gradle/,可以通过添加环境变量GRADLE_USER_HOME

1
2
3
export GRADLE_HOME=/usr/local/gradle
export PATH=$GRADLE_HOME/bin:$PATH
export GRADLE_USER_HOME=$GRADLE_HOME/.gradle
1
GRADLE_USER_HOME=D:\cache\.gradle

修改mavenLocal()目录位置

默认mavenLocal()~/.m2/repository下,修改方法:

1
2
3
4
5
6
7
8
9
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">

<localRepository>D:/cache/.m2/repo</localRepository>

</settings>

修改.android目录位置

默认.android~/.android/,可以通过添加环境变量ANDROID_SDK_HOME

1
export ANDROID_SDK_HOME=/usr/local/Android
1
ANDROID_SDK_HOME=D:\Android

修改.AndroidStudio目录位置

默认.AndroidStudio~/.AndroidStudio/,可以通过修改Android Studio的安装目录下bin/idea.properties

1
2
idea.config.path=D:/Android/.AndroidStudio3.6/config
idea.system.path=D:/Android/.AndroidStudio3.6/system

github加速(推荐)

国内github慢原因是dns污染,所以最直接的办法就是指定hosts。可以借助工具SwitchHosts

添加一个github方案:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# GitHub Start
52.74.223.119 github.com
13.250.177.223 gist.github.com
185.199.111.153 assets-cdn.github.com
151.101.76.133 raw.githubusercontent.com
151.101.76.133 gist.githubusercontent.com
151.101.76.133 cloud.githubusercontent.com
151.101.76.133 camo.githubusercontent.com
151.101.76.133 avatars0.githubusercontent.com
151.101.76.133 avatars1.githubusercontent.com
151.101.76.133 avatars2.githubusercontent.com
151.101.76.133 avatars3.githubusercontent.com
151.101.76.133 avatars4.githubusercontent.com
151.101.76.133 avatars5.githubusercontent.com
151.101.76.133 avatars6.githubusercontent.com
151.101.76.133 avatars7.githubusercontent.com
151.101.76.133 avatars8.githubusercontent.com
185.199.110.154 github.githubassets.com
31.13.83.8 github.global.ssl.fastly.net

# GitHub End

github加速(如果你的科学上网渠道很快,可以用代理方案)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 只指定github走代理
git config --global http.https://github.com.proxy socks5://127.0.0.1:7891
git config --global https.https://github.com.proxy socks5://127.0.0.1:7891

# 全都走代理
git config --global http.proxy socks5://127.0.0.1:7891
git config --global https.proxy socks5://127.0.0.1:7891

# 查看所有配置
git config -l

# reset 代理设置
git config --global --unset http.https://github.com.proxy
git config --global --unset https.https://github.com.proxyy
git config --global --unset http.proxy
git config --global --unset https.proxy