Android Studio 3.3.X 导入项目的正确姿式

使用Android Studio 3.1.2导入之前的项目遇到一些坑,借此机会把相关处理方法分享出来。html

下面以导入Android Studio2.3.3项目为例:java

 

 

在此以前先建议你用Android Studio 3.1.2建立一个新的项目,看看有哪些变化,这对你颇有帮助。android

 

修改app\build:gradle

 

  • 修改compileSdkVersion和buildToolsVersion

修改前,git

compileSdkVersion 23
buildToolsVersion '25.0.0'

 

修改后:github

compileSdkVersion 27
buildToolsVersion '27.0.3'

 

其中buildToolsVersion 是在Android Studio 3.0以后取消了,你能够保留也能够注释掉,api

在defaultConfig方法中将targetSdkVersion 为27并增长一下代码。app

 

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

 

 

  • 修改依赖关键字 compile(implementation/api),provided(compileOnly),apk(runtimeOnly)

修改前:maven

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile name: 'SMSSDK-3.0.0', ext: 'aar'
    compile name: 'SMSSDKGUI-3.0.0', ext: 'aar'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support:design:23.4.0'
    compile 'com.android.support:recyclerview-v7:23.4.0'
    compile 'de.hdodenhof:circleimageview:2.1.0'
    compile 'com.youth.banner:banner:1.4.9'
    compile 'com.facebook.fresco:fresco:0.13.0'
    compile 'com.squareup.okhttp3:okhttp:3.4.1'
    compile 'com.google.code.gson:gson:2.8.0'
    compile 'com.github.bumptech.glide:glide:3.7.0'
    compile 'com.android.support:support-v4:23.4.0'
    compile 'com.foamtrace:photopicker:1.0'
    compile 'com.github.chrisbanes.photoview:library:1.2.4'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
    compile project(':ZXingAndroid')
    compile 'com.google.zxing:core:3.3.1'
}

 

修改后:ide

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.1'
    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'
    implementation name: 'SMSSDK-3.0.0', ext: 'aar'
    implementation name: 'SMSSDKGUI-3.0.0', ext: 'aar'
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support:design:27.1.1'
    implementation 'com.android.support:recyclerview-v7:27.1.1'
    implementation 'de.hdodenhof:circleimageview:2.1.0'
    implementation 'com.youth.banner:banner:1.4.9'
    implementation 'com.facebook.fresco:fresco:0.13.0'
    implementation 'com.squareup.okhttp3:okhttp:3.4.1'
    implementation 'com.google.code.gson:gson:2.8.0'
    implementation 'com.github.bumptech.glide:glide:3.7.0'
    implementation 'com.android.support:support-v4:27.1.1'
    implementation 'com.foamtrace:photopicker:1.0'
    implementation 'com.github.chrisbanes.photoview:library:1.2.4'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.google.zxing:core:3.3.1'
}

 

修改项目下的XXX(项目名)\build:gradle

 

修改前:gradle

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

buildscript {
    repositories {
     jcenter() 
} dependencies {
       classpath 'com.android.tools.build:gradle:2.3.3' 

        // NOTE: Do not place your application odependencies here; they belong 
        // in the individual module build.gradle files 
}
}

allprojects {
repositories { 
    jcenter() 
    maven {url "https://jitpack.io" }
}
}

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

 

 

修改后:

// 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.buiwld:gradle:3.1.3'//与AS版本号保持一致

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        maven { url "https://jitpack.io" }
    }
}

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

 

repositories方法中都增长了google(),build:gradle改和当前AS版本号一致。

 

 

修改gradle-wrapper.properties

修改前:

#Tue Aug 29 08:07:34 CST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip

 

修改后:

 
#Tue Aug 29 08:07:34 CST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
 

主要修改了:distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip

 

这里告一段落,上面都改好以后同步项目(sync)。我为何建议你把上面的都改好以后再同步,这样省事儿,刚开始的时候我也是改一点同步一下,问题多且很浪费时间,若是其中有些问题没能解决就容易走偏。

 

 

若是报错:

Error: java.util.concurrent.ExecutionException: com.android.builder.internal.aapt.v2.Aapt2Exception: AAPT2 error: check logs for details
 

修改gradle.properties,增长以下代码

android.enableAapt2=false

添加android.enableAapt2=false报以下错误请移步Android Studio 3.0后出现AAPT2和“android.enableAapt2”问题以有解决方法

若是有这个错误:这须要更新SDK,点击蓝色文字下载就好。