【Android Studio安裝部署系列】三十、從Android studio2.2.2升級到Android studio3.0之路


版權聲明:本文為HaiyuKing原創文章,轉載請注明出處!

前言

Android Studio 3.0的新功能

https://mp.weixin.qq.com/s/2XmVG4mKEDX6-bvZ0vvmcw

升級前的本地環境

Android studio2.2.2

如何查看

Help——About

 

Gradle 2.14.1(后來導入新項目的時候也下載了3.3版本)

如何查看

File——Project Structure——Project

自己的開發環境新建的項目:

別人的項目:

 

體現在項目中

 

sdk tools 26.0.1

如何查看

File——Settings——Appearance&Behavior——System Settings——Android SDK——SDK Tools

 

體現在項目中

暫時不知

sdk build tools 25.0.3

如何查看

File——Settings——Appearance&Behavior——System Settings——Android SDK——SDK Tools

 

體現在項目中

 

platform-tools 25.0.5

如何查看

File——Settings——Appearance&Behavior——System Settings——Android SDK——SDK Tools

 

體現在項目中

暫時不知

前期准備

下載IDE和SDK

http://www.wanandroid.com/tools/ide

因為本地電腦中存在舊版本Android Studio,也就已經存在SDK,則不需要下載sdk-tools。

下載gradle的最新版本

官網地址:https://gradle.org/

下載地址:https://gradle.org/releases/

 

如果不需要手動更新gradle版本,則不需要下載。可以通過Android Studio下載安裝最新版本(需要翻牆)

備份舊版本的gradle【對於舊版本還是使用2.14.1的情況,這個步驟有點兒用處】

Android Studio安裝目錄/gradle

 

將gradle-2.14.1和gradle-3.3壓縮然后進行備份。將來安裝完AS3.0之后再復制回來。

因為舊項目的gradle home指向的還是舊的數據:

如何查看

File—Settings—Bundle,Execution,Deployment

舊版本的AS最后一次關閉時停留的項目是一個簡單的項目

可以是舊版本的AS新建的一個空項目,這樣的目的是減少升級后碰到的坑。等升級成功后再分別打開舊項目進行填坑。

升級步驟

AS升級到3.0

雙擊下載的AS的IDE安裝包進行安裝。

 

此處會完全刪除Android studio的安裝目錄。

此處需要修改Android Studio安裝路徑。選中Android/Android Studio文本並復制,點擊“Browse”選擇安裝目錄,然后粘貼到選中的目錄后面。效果如下:

打開后會彈出下面的提示

 

保留之前的舊版本AS的配置。

如果之前設置過AS密碼,則需要輸入正確的密碼【這個密碼主要用於打包時的二次確認】:

 

等待編譯完成。就會提示一些必要的操作。

提示修改項目中的gradle plugin和gradle的版本號

 

點擊Update,等待一段時間后,提示如下的信息【升級SDK Bundle Tools(如果已經升級到26以上的話,不會出現這個提示)

 

點擊finish后,編譯一段時間后,提示修改buildToolsVersion版本號

 

按照提示,點擊update Bundle Tools Version and Sync project

 

至此,AS升級到3.0 了。

升級AS3.0后新建一個項目

按照提示新建項目,創建項目后出現下面的提示【安裝Android-26版本的platform

 

點擊“Install missing platform(s) and sync project”

 

至此,Android studio3.0環境就可以正常新建項目了。

關閉整個Android Studio並復制回去gradle舊版本【如果想要舊項目正常運行,在不更新項目的Gradle版本的情況下】

將之前備份的gradle2.14.1和gradle3.3解壓並復制到Android Studio/gradle目錄中

 

注意,這樣操作后對於修改了gradle plugin和gradle版本號的舊項目存在下面的問題

 

解決方案:

其實,保證項目gradle正常運行的是下面路徑下是否存在項目中的gradle版本【這個路徑下的每一個版本的文件夾,都代表下載安裝的gradle版本】:

我想,可能Android Studio安裝目錄/gradle目錄中存在gradle版本文件夾,可以實現快速安裝,不用下載安裝。

C:\Users\Administrator\.gradle\wrapper\dists

升級AS3.0之后打開舊項目的常規操作

不升級Gradle plugin和gradle版本號(不建議)

當AS升級到3.0之后,Gradle Plugin和Gradle不升級也是可以繼續使用的,但很多新的特性如:Java8支持、新的依賴匹配機制、AAPT2等新功能都無法正常使用~ 所以長期看來,最后還是得升級的。

如果不想升級的話,打開舊項目時選擇不更新即可。

 

Gradle Plugin版本升級到3.0.0及以上(略過)

方式一:安裝AS3.0的IDE的時候自動安裝了。

修改項目的Grade bundle版本號

一、對於升級時打開的項目,會自動更改。

二、對於升級到AS3.0后新打開的項目則需要手動修改。

更改項目下的build.grade文件中的Grade bundle版本與當前AS版本保持一致,並且在repositories里面添加google()

參考AS3.0新建的項目的bundle.gradle文件

// 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.build:gradle:3.0.0'
        

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

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

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

Gradle版本升級4.1及以上(略過)

方式一:安裝AS3.0的時候自動下載安裝。

方式二:可以離線下載,把下載好的zip包解壓后放在Android studio安裝目錄的gradle文件夾下。【特殊情況下使用,比如手動升級Gradle版本】

修改項目的gradle版本號

一、對於升級時打開的項目,會彈出一個提示框,自動修改。

二、對於升級到AS3.0后新打開的項目則需要手動修改。

修改gradle/ wrapper/gradle-wrapper.properties文件的gradle版本號

 

distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip

改為:

distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip

SDK bundle tools需要升級到26或更高的版本(略過)

compilesdkversion最好升級到26或更高的版本,以免grade編譯出錯。

方式一:安裝AS3.0的時候按照提示下載安裝。

方式二:手動下載安裝

File——Settings——Appearance&Behavior——System Settings——Android SDK——SDK Tools

選擇其中一個下載安裝。比如26.0.2

修改項目的buildToolsVersion版本號,可以順便更換引用依賴的方式

更改項目app或者其他module下build.grade文件中的sdkbundle版本號

android {
    compileSdkVersion 26
    buildToolsVersion '26.0.2'
   ...
}

參考AS3.0新建的項目的app目錄中的bundle.gradle文件

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.why.project.astestdemo"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

更改gradle home(非必須但建議)

注意:對於AS安裝目錄的gradle文件夾中沒有gradle-2.14.1的情況不會出現該提示。

 

碰到的問題

Error:(2) Error: "app_name" is not translated in "ar" (Arabic), "en" (English), "es" (Spanish), "fr" (French), "ru" (Russian) [MissingTranslation]

Error:(2) Error: "app_name" is not translated in "ar" (Arabic), "en" (English), "es" (Spanish), "fr" (French), "ru" (Russian) [MissingTranslation]
Error:Execution failed for task ':app:lintVitalRelease'.
> Lint found fatal errors while assembling a release target.
  To proceed, either fix the issues identified by lint, or modify your build script as follows:
  ...
  android {
      lintOptions {
          checkReleaseBuilds false
          // Or, if you prefer, you can continue to check for errors in release builds,
          // but continue the build even when errors are found:
          abortOnError false
      }
  }
  ...

解決方案:

在項目的APP中的bundle.gradle文件中添加以下代碼,然后重新編譯下

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion '26.0.2'
    defaultConfig {
        applicationId "com.why.project.changelanguagedemo"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    lintOptions { checkReleaseBuilds false // Or, if you prefer, you can continue to check for errors in release builds, // but continue the build even when errors are found: abortOnError false }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

Error:Unable to load class 'org.gradle.api.internal.component.Usage'.

 

Error:Unable to load class 'org.gradle.api.internal.component.Usage'.
Possible causes for this unexpected error include:<ul><li>Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.)
<a href="syncProject">Re-download dependencies and sync project (requires network)</a></li><li>The state of a Gradle build process (daemon) may be corrupt. Stopping all Gradle daemons may solve this problem.
<a href="stopGradleDaemons">Stop Gradle build processes (requires restart)</a></li><li>Your project may be using a third-party plugin which is not compatible with the other plugins in the project or the version of Gradle requested by the project.</li></ul>In the case of corrupt Gradle processes, you can also try closing the IDE and then killing all Java processes.

解決方案:

將項目根目錄的 build.gradle 中的classpath 'com.novoda:bintray-release:0.3.4'修改為classpath 'com.novoda:bintray-release:0.5.0'

buildscript {
    …
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0'
        //解決Plugin with id 'com.novoda.bintray-release' not found.
        classpath 'com.novoda:bintray-release:0.5.0'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

參考資料

玩Android

Android studio開發

Android Studio 3.0正式版填坑之路

Android Studio3.0更新之路(遇坑必入)

Android Studio3.0新特性及安裝詳解

Android Studio3下多渠道打包遇到的坑

升級到AndroidStudio3.0指南

Android Studio 3.0及Gradle Plugin 3.0升級注意事項

老項目遷移AndroidStudio3.0帶你踩完所有坑~

Android Studio 3.0升級--Unable to load class ‘org.gradle.api.internal.component.Usage’.

AndroidStudio - 手動更新gradle及關聯項目詳解


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM