移植别人环境下的Android Studio项目,本地部环境遇到的坑总结

他人项目中的各项配置、文件路径等都与我的不同,应如何进行配置?

0. sdk路径配置

进入local.properties设置为本地SDK路径

在这里插入图片描述
此处的配置,AS会自动同步到SDK Manager中

1. sdk版本问题

首先面对的问题就是sdk的版本管理,使用SDK Manager勾选好需要的SDK

坑1
在这里插入图片描述
进入SDK Manager发现除了已经安装的,没有可供选择的SDK CheckBox
进入图中红框的SDK Update Sites,看到所有的SDK Updates Sites冒红框,提示无法加载
在这里插入图片描述

解决: 请检查代理设置。经过多次实验,此处不用翻墙也应能加载不同版本SDK。请按照下面关闭Http Proxy
在这里插入图片描述

在这里插入图片描述

2. Module下的build.gradle配置

坑1:buildToolsVersion与compileSdkVersion的版本对齐问题

buildToolsVersion: 构建工具版本,需要版本号>=compileSdkVersion

例如compileSdkVersion为27,我的buildToolsVersion不一定需要27开头,也可以是30.0.2

坑2: targetSdkVersion下划红线报错: Google Play requires that apps target API level 29 or higher.

如果targetSdkVersion报错,在上面加一行注释// noinspection ExpiredTargetSdkVersion即可

坑3: 注意本模块中的Dependencies都应与SdkVersion对齐。

解决: AndroidStudio对此提供了智能的管理,对implementation字段报错提示的地方,鼠标放在上面,再点击change to xxx即可

我的一个示例项目,配置如下

apply plugin: 'com.android.application'

android {
    compileSdkVersion 27 // 29
    buildToolsVersion '30.0.2'  // 29.0.2
    defaultConfig {
        applicationId "com.uasier.autotest"
        minSdkVersion 14
        // noinspection ExpiredTargetSdkVersion
        targetSdkVersion 27  // 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner 'android.test.runner.AndroidTestRunner'
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility = 1.8
        targetCompatibility = 1.8
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'], excludes: ['android-support-v4.jar'])

    implementation 'junit:junit:4.13.2'
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support:design:27.1.1'
    // 添加csv解析依赖包
    implementation  'net.sourceforge.javacsv:javacsv:2.0'
    // 添加fastjson依赖包
    implementation  'com.alibaba:fastjson:1.2.6'
}

3. Project目录下build.gradle配置

全部换源就OK,个人配置如下

ext {
    var = "1.8"
    var = var
}// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
        mavenCentral{url 'http://maven.aliyun.com/nexus/content/repositories/central/'}
        // google()
        google{url 'https://maven.aliyun.com/repository/google/'}
        jcenter { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter' }
        maven{ url'http://maven.aliyun.com/nexus/content/groups/public/' }
        maven{ url'http://maven.aliyun.com/nexus/content/repositories/jcenter'}
        // maven{ url"http://jcenter.bintray.com"}
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.2.2'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
        mavenCentral{url 'http://maven.aliyun.com/nexus/content/repositories/jcenter'}
        google{url 'https://maven.aliyun.com/repository/google/'}
        jcenter { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter' }
        
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

3. JRE配置

坑1: AndroidStudio安装目录下提供了一个JRE,在未手动配置时有时自动配置为那个JRE环境,但有时候也会报错。
Build栏中提示: OpenJDK xxxxxx,按照提示可以打开一个配置页面

解决: 自己装个JDK完事,请自行百度(

4. gradle.properties配置

注意这个是一个本地Android Studio下(与项目无关)的代理配置文件。请根据自行需要配置。如果挂了VPN不用配
但就算挂了VPN,此处有时候也有一些玄学问题,可能配为如下内容会解决

## For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
#
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# Default value: -Xmx1024m -XX:MaxPermSize=256m
# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
#
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
#Fri Jul 30 14:49:43 CST 2021
# systemProp.http.proxyHost=127.0.0.1
# systemProp.https.proxyHost=127.0.0.1
# systemProp.https.proxyPort=10808
# systemProp.http.proxyPort=10808
systemProp.https.nonProxyHosts=localhost
# systemProp.https.proxyPort=1080
systemProp.http.proxyHost=127.0.0.1
systemProp.http.nonProxyHosts=localhost
# systemProp.https.proxyHost=127.0.0.1
systemProp.http.proxyPort=10808

5. gradle版本配置

在点击Sync Project时,出现如下问题:

21:43	Gradle sync failed: Minimum supported Gradle version is 6.7.1. Current version is 6.6.
			Please fix the project's Gradle settings.
			Change Gradle version in Gradle wrapper to 6.7.1 and re-import project
			Open Gradle wrapper properties
			Gradle Settings. (382 ms)

不知道是啥原因导致Gradle版本不对(在其他人电脑上这个版本没问题),跟着Android Studio来就完事了。点击Change Gradle version in Gradle wrapper to 6.7.1 and re-import project Open Gradle wrapper properties
点击后会自动更新,并且同步到gradle-wrapper.properties配置文件中和AS配置选项中

6. 挂vpn

挂vpn的话可以不用换源了,而且这是一个更加通用的方法,可以避免一些不必要的问题(例如有的包能同步下来有的却又不行)。
例如v2ray的vmess协议下,在idea的http proxy中进行如下配置即可

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值