Android Studio 3.0前後的差異,彙總
1. Gradle版本不匹配
修改專案下 gradle/wrapper/gradle-wrapper.propertie 檔案中的distributionUrl
AS 3.0 ~ 3.0.1改為:distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
AS 3.1.1 ~3.1.3 改為:distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
2. Gradle外掛不匹配
1)專案根目錄下的 build.gradle檔案中兩個repositories
google()
- E.G
buildscript{
repositories {
google() //新增
}
dependencies {
classpath'com.android.tools.build:gradle:3.0'//與AS版本一致
//classpath'com.android.tools.build:gradle:3.1.1'
}
}
allprojects {
repositories{
google() //新增
}
}
2)專案app下 build.gradle檔案中,修改相關支援庫版本
AS 3.0 ~ 3.0.1:
android { compileSdkVersion 26 buildToolsVersion "26.0.2" ... } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:26.1.0' implementation 'com.android.support:design:26.1.0' testImplementation 'junit:junit:4.12' ... }
AS 3.1.1 ~ 3.1.3:
android {
compileSdkVersion 27
buildToolsVersion "27.0.3"
...
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:design:27.1.1'
testImplementation 'junit:junit:4.12'
...
}
3. Gradle編譯報flaovr配置錯誤
- Error
Error:A problem occurred configuring project ':app'.> All flavors must now belong to a named flavor dimension.
- Solution
AS 3.0後Gradle添加了flavorDimensions
屬性,用來控制多個版本的程式碼和資源,缺失就會報錯。在專案app下build.gradle檔案中,新增flavorDimensions
:
android {
...
flavorDimensions "tier","minApi"
productFlavors{
fees{
dimension"tier"
...
}
minApi23{
dimension"minApi"
...
}
}
}
如果不需要多版本控制只需新增:flavorDimensions "code"
(隨意定義)
android {
...
defaultConfig {
...
flavorDimensions "code"
}
...
}
4. Gradle自定義apk名稱報錯(Cannot set the value of read-only property 'outputFile' )
- E.G
AS 3.0之前自定義apk名稱:
applicationVariants.all { variant ->
variant.outputs.each { output ->
def fileName = "${variant.versionName}_release.apk"
def outFile = output.outputFile
if (outFile != null && outFile.name.endsWith('.apk')) {
output.outputFile =newFile(outFile.parent, fileName)
}
}
AS 3.0之後,同樣程式碼自定義apk名稱卻會報錯:
- Error
Error:(56, 0) Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated{apkData=Main{type=MAIN, fullName=debug, filters=[]}} of type com.android.build.gradle.internal.api.ApkVariantOutputImpl.
- Solution
outputFile
變為只讀,不能修改輸出的名稱所以報錯。修改為:
applicationVariants.all { variant ->
variant.outputs.all { output -> // each 改為 all
def fileName = "${variant.versionName}_release.apk"
def outFile = output.outputFile
if (outFile != null && outFile.name.endsWith('.apk')) {
outputFileName = fileName // output.outputFile 改為 outputFileName
}
}
把each
修改為all
,然後通過outputFileName
修改生成apk的名稱。此外,AS 3.0後打包完,除了apk包檔案,還會多一個 output.json 引數檔案。
5. AS 3.0後關鍵字依賴變化
- E.G
AS 3.0之前依賴關鍵字:compile
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:26.1.0'
compile files('libs/gson-2.3.1.jar')
compile project(':mylibrary')
...
}
AS 3.0之後依賴關鍵字:implementation
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation files('libs/gson-2.3.1.jar')
implementation project(':mylibrary')
...
}
AS 3.0後Gradle關鍵字依賴發生變化:compile(implementation/api)
,provided(compileOnly)
,apk(runtimeOnly)
AS 3.0後,在使用新依賴配置項時,引用本地庫使用
implementation
指令時,若出現找不到導包或資源問題報錯,可以更換依賴指令為api
重新編譯。關於implementation
和api
的區別,請移駕:AS Gradle依賴項配置
6. AAPT2編譯報錯
- Error
Error: java.util.concurrent.ExecutionException: com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details
- Solution
在專案根目錄下gradle.properties檔案中關閉APPT2編譯:
...
android.enableAapt2 = false
7. PNG 圖片錯誤,AAPT err(Facade for):Unable to open PNG file
- Error
AAPT err(Facade for):……Unable to open PNG file
- Solution
專案app下build.gradle檔案中新增下面屬性:
android{
...
aaptOptions{
cruncherEnabled = false
useNewCruncher = false
}
...
}
用來關閉AS圖片PNG合法性檢查的,直接不讓它檢查。
- Note
如果還是有錯誤,請檢查:
1 ) .9.png圖片放在
res/drawable
資料夾下
2 ) .9.png圖片四邊都要有黑線,確保圖片是標準的.9.png
圖片
8. 輸入法中文狀態下無法選詞
- Question
AS 3.0後在輸入中文時候會出現鍵盤不顯示,無法篩選詞輸入中文 - Solution
首先要說不是你的輸入法問題,而是AS 3.0後的一個BUG,下面提供幾種解決方案:
1 ) 如果你還在2.3.x的環境下開發,為了避免輸入法問題,建議你暫時不要升級到3.x;
2 ) 如果你想2.3.x升級使用3.x,那麼不建議你使用2.x的jre替換3.x的jre方式去處理輸入法問題,雖然暫時可以解決輸入問題,但是後面升級的時候你還得把2.x 的jre換回3.x的jre,否則升級後將無法正常使用AS;
3 ) 最簡單最有效的解決辦法就是在使用 AS的時候,切換到 windows自帶的中文輸入法就可以正常輸入中文篩選詞語了,相對而言,這樣方便很多。雖然沒有第三方輸入法用起來那麼順手,但是可以有效解決輸入法問題和避免以後升級的問題;
4 ) 終極方案:升級到 AS 3.1.1即可解決,AS 3.1.1已經修復了輸入法中文狀態下無法選詞的BUG。
9. 移除無用資源問題
- Error
Error: Removing unused resources requires unused code shrinking to be turned on.
- Solution
android {
...
buildTypes {
debug {
signingConfig signingConfigs.release
debuggable true
zipAlignEnabled true
minifyEnabled true //是否混淆
shrinkResources true //是否去除無效的資原始檔
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
release {
signingConfig signingConfigs.release
zipAlignEnabled true
debuggable true
minifyEnabled true //是否混淆
shrinkResources true //是否去除無效的資原始檔
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
...
}
AS 3.0.1後,如果使用shrinkResources
來移除未引用資源,必須要先開啟混淆minifyEnabled
,才能通過資源壓縮器將它們移除,否則編譯會報錯。
10. 軟體升級安裝衝突
-
Error
AS線上升級後,安裝重啟軟體時會出現部分檔案安裝衝突。如下圖所示:更新問題圖
-
Solution
方法一:
1)找到 AS 安裝目錄下的 uninstall.exe 解除安裝檔案,執行解除安裝當前舊版本AS(解除安裝前記得備份程式碼和配置檔案);
2)下載最新版本的AS安裝包,執行安裝並匯入配置檔案和專案。
方法二:
1)點選 Cancel 取消安裝並關閉AS;
2)找到 AS 安裝目錄下的 studio64.exe 啟動檔案,右鍵 —— 以管理員身份執行 開啟AS(提高AS的許可權);
3)點選 Help —— Checkout for Updates —— Update and Restart 重新更新下載安裝。