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

解决方案:将电脑中的.gradle/cache目录删除掉(清除掉gradle缓存)重新build。

参考stackoverflow  :https://stackoverflow.com/questions/46233560/getting-dexarchivemergerexception-when-adding-leanback-library

解决方案详解在这里 :  https://asyoulook.com/computers%20&%20internet/android-unable-to-merge-dex/82120

4.解决Error:All flavors must now belong to a named flavor dimension. Learn more at https://d.android.com

这个问题是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.   
  4. productFlavors {  
  5.      red {  
  6.       // Assigns this product flavor to the 'color' flavor dimension.  
  7.       // This step is optional if you are using only one dimension.  
  8.       dimension "color"  
  9.       ...  
  10.     }  
  11.   
  12.     blue {  
  13.       dimension "color"  
  14.       ...  
  15.     }  
  16. }  

大致是说,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会使编译速度有所增快。

参考:

http://blog.csdn.net/soslinken/article/details/73114637

https://zhuanlan.zhihu.com/p/27475883

https://stackoverflow.com/questions/44413952/gradle-implementation-vs-api-configuration

http://blog.csdn.net/JF_1994/article/details/52781751














  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值