Android 3.0及以上刨坑集锦

2018-05-31  本文已影响102人  lkuo

前言

本来想多挖点坑总结再分享出来的,但最近的坑折磨的我够呛,想提前分享出来帮助其它受难的小伙伴,哎,不说了。。。

持续刨坑中...

1.配置ButterKnife

只需设置如下代码即可:

    implementation 'com.jakewharton:butterknife:8.8.1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'

在module的build.gradle 文件中设置支持插件 ,添加如下代码

apply plugin: 'com.jakewharton.butterknife'  

否则无法使用R.id.R2

2.组件化项目升级3.0以上后报错

Could not find runtime.aar (android.arch.lifecycle:runtime:1.0.0).
Searched in the following locations:
https://jcenter.bintray.com/android/arch/lifecycle/runtime/1.0.0/runtime-1.0.0.aar

    // 组件化Architecture Components
    compile "android.arch.lifecycle:runtime:1.1.1"
    compile "android.arch.lifecycle:extensions:1.1.1"
    annotationProcessor "android.arch.lifecycle:compiler:1.1.1"

3.依赖方式改变

implementation、api、compileOnly等

https://blog.csdn.net/yuzhiqiang_1993/article/details/78366985?locationNum=6&fps=1

4.打开一个3.0的GitHub项目报错(使用的是3.0以下AS)

QError:This Gradle plugin requires a newer IDE able to request IDE model level 3. For Android Studio this means version 3.0+
A
(1): 升级android studio IDE的版本。
(2):在项目的gradle.properties配置文件中加入以下这句:
gradle.properties中:android.injected.build.model.only.versioned = 3

5.Gradle自定义apk名称报错

    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:(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.

由于3.0中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参数文件。

6.AS3.0多渠道打包报错

Error:All flavors must now belong to a named flavor dimension. The flavor 'xiaomi' is not assigned to a flavor dimension. Learn more at https://d.android.com/r/tools/flavorDimensions-missing-error-message.html

解决方法:

flavorDimensions "default"  
    productFlavors {  
        kuan {  
            dimension "default"  
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "kuan"]  
        }  
        xiaomi {  
            dimension "default"  
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "xiaomi"]  
        }  
        qh360 {  
            dimension "default"  
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "qh360"]  
        }  
        baidu {  
            dimension "default"  
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "baidu"]  
        }  
        wandoujia {  
            dimension "default"  
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "wandoujia"]  
        }  
    }  

或者写成:

flavorDimensions "default"  
    productFlavors {  
        kuan {dimension "default"}  
        xiaomi {dimension "default"}  
        qh360 {dimension "default"}  
        baidu {dimension "default"}  
        wandoujia {dimension "default"}  
    }  
  
    productFlavors.all {  
        flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]  
    }  

7.第三方库导致support版本冲突

一般这种问题解决方法是:在指定的有冲突的库的依赖处,添加exclude group: 'com.android.support',可以将冲突库不包含在编译,如:

compile('xx.xxx.xxxxx:xxxxx:1.5.5') {
    exclude group: 'com.android.support'
}

但是问题是我不知道哪个第三方库冲突,不可能一个个检查吧?

这时候只需要在gradle文件中添加如下代码,让所有的第三方包强制使用指定版本的support包:

configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        def requested = details.requested
        if (requested.group == 'com.android.support') {
            if (!requested.name.startsWith("multidex")) {
                details.useVersion '26.1.0'
            }
        }
    }
} 

同时,需要注意的是,在自己写第三方库给别人用的时候,对于support包的依赖方式改成provided(或者compileOnly,gradle3.0),这样不会把support打包,方便其他人使用。

8.组件化项目报错 Manifest merger failed

这个错误是我手贱升级AS3.1.2后,报出来的


崩溃啊
什么玩意

上网搜索好几个夜晚。。。没有找到解决方法,直到我发现了

好开心
新大陆,骚操作啊!!!
可以发现因为我设置了tools:replace="android:exported",而我在Manifest中没有给四大组件设置过android:exported属性,没升级前没报这个错。。。 这里!就是这里

去掉后就可以正常运行啦!!:)

额外说一句哈,关于这个android:exported属性的

  • 四大组件 Activity,Service,Provider,Receiver 四大组件中都会有这么一个属性。
  • 它的主要作用是:是否支持其它应用调用当前组件。
  • 默认值:如果包含有intent-filter 默认值为true; 没有intent-filter默认值为false。

详细的可以看这里:
https://blog.csdn.net/watermusicyes/article/details/46460347

9.矢量图报错

Q: A failure occurred while executing com.android.build.gradle.tasks.MergeResources$FileGenerationWorkAction. Error while processing C:\androidProject\xxx\app\src\main\res\drawable\ic_storage_24dp.xml : Can't process attribute android:fillColor="@color/color_storage": references to other resources are not supported by build-time PNG generation. See http://developer.android.com/tools/help/vector-asset-studio.html for details.

A: 问题出在矢量图,2.0+的Android Studio需要如下进行设置:

defaultConfig{  
   vectorDrawables.useSupportLibrary = true  
} 

10.分包multidex问题

在依赖中添加了MultiDex后,报错提示包重复。

// 添加MultiDex支持库的依赖
api 'com.android.support:multidex:1.0.1'

找了好久,也没发现在哪加了1.0.2的包啊。。。尼玛

后来终于发现,在3.0中只要设置

multiDexEnabled true

就会自动添加上述两个1.0.2的包。

Android 5.0(API leve 21)之前的系统使用Dalvik执行应用程序代码。默认情况下,Dalvik限制一个apk只有一个Dex文件。为了绕过这个限制, 我们可以使用multidex support library,它成为我们APK的主要DEX文件的一部分,负责管理我们APK访问其他DEX文件和代码。
注意: 如果咱的项目minSdkVersion是20或更低,运行到Android 4.4(API leve 20)或者更低版本的设备上时需要禁用AndroidStudio的即时运行

Android 5.0(API leve 21)和更高的系统使用runtime是ART ,原生支持从应用的apk文件加载多个DEX文件。ART在安装应用时预编译应用程序,会扫描多个classes(..N).dex文件编译成一个.oat的文件。更多Android5.0 runtime的更多信息,请参见即时运行-instant-run。
注意: 如果你使用即时运行 , AndroidStudio自动配置你的应用程序,你应用程序的minSdkVersion应该设置为21或更高。因为即时只工作在你APP的Debug版本,你任然需要配置你的release版本构建时用multidex避免64k的限制。

综上,multiDexEnabled true即可解决问题。另外,别忘了

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(base);
    }

11.Android Studio下载插件超时

File->Settings->Apparence & Behavior->System Settings->Updates->use secure connnection 勾去掉


12.

上一篇 下一篇

猜你喜欢

热点阅读