一台没有网络的空白电脑,需要配置Android 开发环境,只有一个u盘,怎么办?
一、准备Android studio ,sdk , gradle , jre,相关的依赖库
在线利用网络下载好Android studio相关的内容Android studio ,sdk , gradle , jre,相关的依赖库
依赖库
下载的依赖库位置:
打开发现在 C:\Users\77635.gradle\caches\modules-2\files-2.1 存在大量的依赖库,有jar 或者 aar 包,如下:
我们可以利用python脚本将files-2.1文件夹中的jar 和 aar 包复制到指定的目录中,脚本如下:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
import shutil
def cp_tree_ext(types, src, dest):
fp = {}
typearr = types.lower().split()
for root, dirs, files in os.walk(src):
for file in files:
if os.path.splitext(file.lower())[1][1:] in typearr:
if root not in fp.keys():
fp[root] = []
fp[root].append(file)
for k, v in fp.items():
for f in v:
oldfile = os.path.join(k, f)
print("拷贝" + oldfile + "] 至 [" + dest + "]")
if not os.path.exists(dest):
os.makedirs(dest)
shutil.copy(oldfile, dest)
cp_tree_ext('jar aar', 'C:\\Users\\77635\\.gradle\\caches\\modules-2\\files-2.1', 'C:\\Users\\77635\\Desktop\\test666')
sdk
在Android studio中直接配置好sdk所需要的版本
目录如下:
gradle 与 jre
gradle 目录:
二、导入
将Android studio(包含jre 、gradle) 、 sdk 、 依赖库导入到内网的电脑中指定目录下,jar和aar我们用python脚本放到指定文件夹中可直接一键复制进去。
三、配置
Android studio中jre 、 sdk等路径要设置正确
app的gradle文件:
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.myapplication"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
//解决Annotation processors must be explicitly declared now
javaCompileOptions {
annotationProcessorOptions {
includeCompileClasspath true
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
//解决Invoke-customs are only supported starting with Android O (--min-api 26)
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
//解决More than one file was found with OS independent path 'assets/ap1.data'
packagingOptions {
exclude 'assets/ap1.data'
}
}
//项目依赖库的三种方式
//第一种:在线gradle的dependencies
//第二种:离线gradle的缓存依赖文件
//第三种:自己拷贝jar或者aar包到项目的lib中,并dependencies设置
// (因为离线无网络使用别的电脑缓存会有问题,所以这里我们使用第三种方式)
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'])//添加aar
// implementation 'com.android.support:appcompat-v7:28.0.0'
// implementation 'com.android.support.constraint:constraint-layout:1.1.3'
// 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'
}
问题
Program type already present: com.squareup.javawriter.JavaWriter$Scope
解决
快捷键Ctrl+N 打开类搜索框,搜索类名JavaWriter,会发现出现了多个版本,删掉一个即可
问题
Unknown host 'dl.google.com'. You may need to adjust the proxy settings in Gradle.
<a href="toggle.offline.mode">
Enable Gradle 'offline mode' and sync project
</a><br><a href="https://docs.gradle.org/current/userguide/
userguide_single.html#sec:accessing_the_web_via_a_proxy">
Learn about configuring HTTP proxies in Gradle</a>
解决:将project的gradle文件中的Google仓库去掉
// 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.3.2'
// 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
}
gradle
如果Android studio内有gradle文件,可以直接选择use default gradle wrapper(recommended),没有则需要自己导入,并选择use local gradle distribution
问题:
ERROR: Could not find method javaCompileOptions() for arguments
[build_bv02tmsacc8azaesk2s2nvtte$_run_closure1$_closure6@2874cbc]
on object of type com.android.build.gradle.internal.dsl.BaseAppModuleExtension.
解决:
javaCompileOptions要放在defaultConfig中
问题
The number of method references in a .dex file cannot exceed 64K
解决:
public class MyAppliation extends MultiDexApplication {
}
<application
android:name=".MyAppliation"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
</application>
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.myapplication"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
//解决:
//The number of method references in a .dex file cannot exceed 64K
multiDexEnabled true
}