拿到一個別人的Android項目,經常無法直接很順利的在自己的Android Studio中運行,如果不想重新下載一堆額外的插件,只需要修改三個文件的內容即可。
我們假設項目名稱叫master,需要修改的文件如下:
master\build.gradle
master\app\build.gradle
master\gradle\wrapper\gradle-wrapper.properties
master\build.gradle
// 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/' }
maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter' }
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.2.0"
// 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/' }
maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter' }
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
classpath "com.android.tools.build:gradle:4.2.0"
只需要把這句換成自己本機的Android Studio版本即可
kotlin同理,改成自己編譯器的版本,如:ext.kotlin_version = '1.3.71'
master\app\build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 30
defaultConfig {
applicationId "com.xinghe.course.mydata"
minSdkVersion 16
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
對於這個文件,我們應該關注的是:
compileSdkVersion 30
minSdkVersion 16
targetSdkVersion 30
統統改成本機Android Studio預設的值
master\gradle\wrapper\gradle-wrapper.properties
#Tue May 05 20:26:29 CST 2020
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
這里要修改的是gradle的版本,一般直接將自己本機Android Studio的這個文件內容直接全部復制過來就行或者單獨修改Url:
distributionUrl=https://services.gradle.org/distributions/gradle-6.7.1-bin.zip
如何快速獲取本機Android Studio相關預設和版本
直接自己新建一個項目,然后去對應的文件里把對應的需要更改的內容直接復制過來即可