转载

Android Studio 3.1.X 导入项目的正确姿势

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

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

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

修改app/build:gradle

  • 修改compileSdkVersion和buildToolsVersion

修改前,

compileSdkVersion 23
buildToolsVersion '25.0.0'

修改后:

compileSdkVersion 27
buildToolsVersion '27.0.3'

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

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

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
  • 修改依赖关键字 compile(implementation/api),provided(compileOnly),apk(runtimeOnly)

修改前:

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'
}

修改后:

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

修改前:

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
}

修改后:

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”问题以有解决方法

Android Studio 3.1.X 导入项目的正确姿势

如果有这个错误:这需要更新SDK,点击蓝色文字下载就好。

Android Studio 3.1.X 导入项目的正确姿势

原文  http://www.androidchina.net/8940.html
正文到此结束
Loading...