Android Studio 2.0 to Android Studio 3.0

时间:2025-02-08 07:59:53

本文转自 /android/android-studio-2-0-to-android-studio-3-0/


When importing Android project that was using gradle 2+ and Android Studio 2+ into Android Studio 3.0, Android Studio 3.0 might run into this error.

Error:Could not initialize class

Replace following project gradle file which uses gradle 2.2.2

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath ':gradle:2.2.2'
        classpath ':android-apt:1.8'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module  files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

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

with this project gradle file to use grade 3.0.0 to work with Android Studio 3.0+

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

buildscript {
    ext.kotlin_version = '1.1.60'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath ':gradle:3.0.0'
        classpath ":kotlin-gradle-plugin:$kotlin_version"

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

allprojects {
    repositories {
        google()
        jcenter()
    }
}

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

In the app gradle file, replace these plugins with

apply plugin: ''
apply plugin: ''
apply plugin: '-apt'

these plugins.

apply plugin: ''
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: ''

In the dependencies tag in the app gradle file, replace compile to implementation, replace testCompile with testImplementation, replace androidTestCompile with androidTestImplementation

For projects using dagger 2. replace

compile ':dagger:2.7'
apt ':dagger-compiler:2.7'
provided ':jsr250-api:1.0'
<pre>

with
<pre>
implementation ":dagger:2.11"
implementation ":dagger-android:2.11"
kapt ":dagger-compiler:2.11"

Now you will probably get this error if you haven’t upgraded the gradle library in the file.
Could not find method google() for arguments [] on repository container.

So, let’s replace this line in the

distributionUrl=https\:///distributions/gradle-2.14.

with this line

distributionUrl=https\:///distributions/gradle-4.

Then do a clean and rebuild, everything should work now.