项目迁移到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 ':@Launcher3DefaultConfig/compileClasspath': 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。
解决方案详解在这里 : https://asyoulook.com/computers%20&%20internet/android-unable-to-merge-dex/82120
解决方案:将电脑中的.gradle/cache
目录删除掉(清除掉gradle缓存)重新build。
解决方案详解在这里 : 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升级到了
classpath 'com.android.tools.build:gradle:3.0+'
- Error:All flavors must now belong to a named flavor dimension.
- The flavor 'flavor_name' is not assigned to a flavor dimension.
- // Specifies a flavor dimension.
- flavorDimensions "color"
- productFlavors {
- red {
- // Assigns this product flavor to the 'color' flavor dimension.
- // This step is optional if you are using only one dimension.
- dimension "color"
- ...
- }
- blue {
- dimension "color"
- ...
- }
- }
minSdkVersion :***
问题
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