1. 程式人生 > >android studio 3.0 升級問題,遇到的坑;

android studio 3.0 升級問題,遇到的坑;

專案遷移到Android studio 3.0.0問題,官方提供資料連結:
https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration.html

1、Error:(33, 0) Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated{apkData=Main{type=MAIN, fullName=Launcher3Debug, filters=[]}},的錯誤;
(1)在專案app下的build.gradle中的android標籤中做如下配置:


android.applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
// 這裡修改apk名稱
def fileName = outputFile.name.replace("app", "xxoo_${defaultConfig.versionName}.${defaultConfig.versionCode}_${releaseTime()}")

output.outputFile = new File(outputFile.parent, fileName)
}
}
}

(2)在專案app下的build.gradle中的android標籤中做如下配置,如果碰到以下錯誤:


Cannot set the value of read-only property 'outputFile’….

參考文章:Android Gradle 3.0.0-alpha2 plugin, Cannot set the value of read-only property 'outputFile'

請嘗試下邊的操作方式:
android.applicationVariants.all { variant ->

variant.outputs.all { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
// 這裡修改apk名稱
def fileName = outputFile.name.replace("app", "xxoo_${defaultConfig.versionName}.${defaultConfig.versionCode}_${releaseTime()}")
outputFileName = new File(outputFile.parent, fileName)
}
}
}
2、Unable to resolve dependency for:


 Error:Unable to resolve dependency for ':@Launcher3DefaultConfig/compileClasspath': Could not resolve project :textlibs.
<a href="openFile:E:/launcher/native_launcher/native_launcher/build.gradle">Open File</a><br><a href="Unable to resolve dependency for &#39;:@Launcher3DefaultConfig/compileClasspath&#39;: Could not resolve project :leagoolibs.">Show Details</a>

(1)setting.gradle中沒有依賴appCommon和textlibs看完之後明明確實依賴了,而且這是一個老專案,在AS2.3版本的時候很正常然後我就知道這又是一個坑,無奈的又去上了一把Internet,然後試過網上說的在buildTypes中加入preview節點,然而並沒有什麼卵用,查了半天還是沒有找到解決的方法後來看著這段報錯資訊的時候,我看到了signingConfigs和DefaultConfig,我想到gradle中好像有這麼個節點,抱著試一試的態度我刪了那個節點,然後,臥槽臥槽臥槽,可以成功編譯了


3、app:transformDexArchiveWithExterLibsDexMergeForDebug

這個問題是Android studio升級到3.0之後,執行的時候會提示gradle要升級到3.5版本才能編譯。於是我把我的gradle升級到了

 gradle-4.1-milestone-1 版本,是2017年7月份最新版本了。 於是我把主程式的build.gradle中的gradle版本改成了這個,具體指定哪個版本我也不知道,於是就寫了個3.0+  dependencies {
        classpath 'com.android.tools.build:gradle:3.0+'  } 然後再次編譯,又發現了毒。 提示:Error:All flavors must now belong to a named flavor dimension.Learn more at https://d.android.com/r/tools/flavorDimensions-missing-error-message.html  這個一個錯誤,意思是:所有的flavors都必須屬於同一個風格。 =。=懵逼 去翻牆看了它提供的地址才知道:  Plugin 3.0.0 includes a new dependency mechanism that automatically matches variants when consuming a library. This means an app's debug variant automatically consumes a library's debug variant, and so on. It also works when using flavors—an app's redDebug variant will consume a library's redDebug variant. To make this work, the plugin now requires that all flavors belong to a named flavor dimension —even if you intend to use only a single dimension. Otherwise, you will get the following build error:
  1. Error:All flavors must now belong to a named flavor dimension.  
  2. The flavor 'flavor_name' is not assigned to a flavor dimension.  

 To resolve this error, assign each flavor to a named dimension, as shown in the sample below. Because dependency matching is now taken care of by the plugin, you should name your flavor dimensions carefully. For example, if all your app and library modules use the foo dimension, you'll have less control over which flavors are matched by the plugin. 
  1. // Specifies a flavor dimension.
  2. flavorDimensions "color"
  3. productFlavors {  
  4.      red {  
  5.       // Assigns this product flavor to the 'color' flavor dimension.
  6.       // This step is optional if you are using only one dimension.
  7.       dimension "color"
  8.       ...  
  9.     }  
  10.     blue {  
  11.       dimension "color"
  12.       ...  
  13.     }  
  14. }  

大致是說,Plugin 3.0.0之後有一種自動匹配消耗庫的機制,便於debug variant 自動消耗一個庫,然後就是必須要所有的flavor 都屬於同一個維度。 為了避免flavor 不同產生誤差的問題,應該在所有的庫模組都使用同一個foo尺寸。 = 。=還是懵逼。說一堆依然不是很理解。 但是我們從中已經知道解決方案了: 在主app的build.gradle裡面的  defaultConfig {  targetSdkVersion:***
minSdkVersion :*** versionCode:***
 versionName :*** //版本名後面新增一句話,意思就是flavor dimension 它的維度就是該版本號,這樣維度就是都是統一的了 flavorDimensions "versionCode"
} 5、android studio 3.0 升級問題:原來module中的包沒法引用

問題

Android studio升級到3.0以上之後,也隨之升級到了3.0.0版本。

classpath 'com.android.tools.build:gradle:3.0.0'
  • 1

在3.0版本中,compile 指令被標註為過時方法,而新增了兩個依賴指令,一個是implement 和api,這兩個都可以進行依賴新增,但是有什麼區別呢?

implementation和api的區別。

新建工程預設生成的app的build.gradle檔案中的依賴:

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    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'
}

api 指令

完全等同於compile指令,沒區別,你將所有的compile改成api,完全沒有錯。

implementation指令

這個指令的特點就是,對於使用了該命令編譯的依賴,對該專案有依賴的專案將無法訪問到使用該命令編譯的依賴中的任何程式,也就是將該依賴隱藏在內部,而不對外部公開。

簡單的說,就是使用implementation指令的依賴不會傳遞。
例如,有一個module為testsdk,testsdk依賴於gson:

implementation 'com.google.code.gson:gson:2.8.2'

這時候,在testsdk裡邊的java程式碼是可以訪問gson的。

另一個module為app,app依賴於testsdk:

implementation project(':testsdk')

這時候,因為testsdk使用的是implementation 指令來依賴gson,所以app裡邊不能引用gson。

但是,如果testsdk使用的是api來引用gson:

api 'com.google.code.gson:gson:2.8.2'

則與gradle3.0.0之前的compile指令的效果完全一樣,app的module也可以引用gson。這就是api和implementation的區別。

建議

在Google IO 相關話題的中提到了一個建議,就是依賴首先應該設定為implementation的,如果沒有錯,那就用implementation,如果有錯,那麼使用api指令。使用implementation會使編譯速度有所增快。

參考: