源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

详解Android Studio 3.0的新特性与适配

  • 时间:2021-06-02 20:57 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:详解Android Studio 3.0的新特性与适配
[b]简介[/b] Android Studio升级到3.0后,有不少的改动和新特性,先贴出[url=https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration.html#variant_api]官方的迁移说明[/url]。 本文会持续收集与总结本人在使用Android Studio 3.0进行开发的过程中所遇到的问题。 [b]版本配置[/b] [b]Gradle版本[/b] [list=1] [*]Android Studio 3.0需要的Gradle版本至少为4.1。[/*] [*]如果是使用gradle wrapper,则工程根目录/gradle/wrapper/gradle-wrapper.properties中的distributionUrl字段为https\://services.gradle.org/distributions/gradle-4.1-all.zip。[/*] [/list] [b]Android Gradle插件版本[/b] Android Studio 3.0需要Android Gradle插件版本为3.0.0。 Android Studio 3.0默认使用Google's Maven Repository来下载Android Support Library,所以在脚本中要使用google()来加入谷歌仓库。 工程根目录/build.gradle的相关配置如下。
buildscript {
  repositories {
    google()
  }
  dependencies {
    classpath 'com.android.tools.build:gradle:3.0.0'
  }
}
[b]使用annotationProcessor[/b] 从Android Studio 3.0开始,使用annotationProcessor代替apt。不可再使用apt,否则会编译报错。 Error:android-apt plugin is incompatible with the Android Gradle plugin.  Please use 'annotationProcessor' configuration instead. 比如在Android Studio 3.0之前在application模块导入ButterKnife 8.4.0的gradle配置如下。
buildscript {
  dependencies {
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
  }
}
apply plugin: 'com.neenbedankt.android-apt'

dependencies {
  compile 'com.jakewharton:butterknife:8.4.0'
  apt 'com.jakewharton:butterknife-compiler:8.4.0'
}
而在Android Studio 3.0中,使用annotationProcessor代替apt,不用再导入android-apt插件。
dependencies {
  compile 'com.jakewharton:butterknife:8.4.0'
  annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
}
[b]修改apk名称[/b] 常用的修改输出的apk文件的名称的脚本如下。
def apkBaseName() {
  // 先查找project.ext.apkName变量,若无则使用项目名
  if(project.hasProperty("apkName")) {
    return project.apkName
  } else {
    return project.name
  }
}

def buildTime() {
  return new Date().format("yyyyMMdd")
}

def delUnderline(String str) {
  def result = str.startsWith("_") ? str.substring(1) : str
  return result.endsWith("_") ? result.substring(0, result.length() - 1) : result
}

android.applicationVariants.all { variant -> // ApplicationVariant
  variant.outputs.each { output -> // BaseVariantOutput
    def file = output.outputFile
    if(file != null && file.name.endsWith(".apk")) {
      def flavorName = delUnderline(variant.flavorName)
      def buildTypeName = delUnderline(variant.buildType.name)
      def apkFile = new File(file.parent, "${apkBaseName()}_" +
          "${buildTypeName.empty ? "" : buildTypeName + "_"}" +
          "${flavorName.empty ? "" : flavorName + "_"}" +
          "v${variant.versionName}_" +
          "${buildTime()}.apk")
      output.outputFile = apkFile
    }
  }
}

在Android Studio 3.0中执行此脚本会报错如下,原因是ApkVariantOutputImpl的outputFile属性改为只读。 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 不再设置outputFile属性,而是设置outputFileName。同时把each()改为all()。
android.applicationVariants.all { variant -> // ApplicationVariant
  variant.outputs.all {
    if (outputFileName.endsWith(".apk")) {
      def flavorName = delUnderline(variant.flavorName)
      def buildTypeName = delUnderline(variant.buildType.name)
      outputFileName = "fileName"
    }
  }
}
[b]AAPT2[/b] 为了改进增量资源处理,Android Gradle插件3.0默认开启AAPT2。 在旧项目中开启AAPT2,有时候会报错,如: Error: java.util.concurrent.ExecutionException: com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details 可在gradle.properties中加入以下配置来禁用AAPT2。
android.enableAapt2=false
新的依赖配置 Gradle 3.4推出了[url=https://docs.gradle.org/current/userguide/java_library_plugin.html#sec:java_library_configurations_graph]新的Java Library Plugin配置[/url],而Android Gradle插件3.0是使用Gradle 4.1的,因此,需要注意更改为新的依赖配置。 旧的依赖配置,如compile project(':base-library'),会导致如下错误。应该修改为implementation project(':base-library')。
Error:Cannot choose between the following configurations of project :base-library:
 - debugApiElements
 - debugRuntimeElements
 - releaseApiElements
 - releaseRuntimeElements
[b]flavor[/b] 从Android Gradle插件3.0开始,如果build.gradle中有自定义的productFlavors配置,需要添加自定义的flavorDimensions(风味维度),否则会编译报错。 Error:All flavors must now belong to a named flavor dimension. The flavor 'flavor_name' is not assigned to a flavor dimension. 解决方法是:先定义一个flavorDimensions,之后在每个flavor中指定为这个dimension。
android {
  flavorDimensions 'core'
  
  productFlavors {
    beta {
      dimension 'core'
    }
    
    production {
      dimension 'core'
    }
  }
}
在设置flavorDimensions之前,最终的Build Variant = Product Flavor + Build Type。而设置之后,最终的Build Variant = 维度1 + 维度2 + ... + 维度n + Build Type。 [b]Kotlin支持[/b] 在Android Studio 3.0之前,使用Kotlin需要进行额外的配置。而Android Studio 3.0开始,默认内置支持Kotlin,无需额外配置。 使用Android Studio工具栏中的Code -> Convert Java File To Kotlin File,可将.java文件转为.kt文件。 [b]Java8支持[/b] 从Android Studio 2.1起,官方通过Jack来支持Java8,从而开发者能使用Lambda等特性。
android {
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
  
  defaultConfig {
    jackOptions {
      enabled true
    }
  }
}
可在Android Studio工具栏,File -> Project Structure,修改Source Compatibility和Target Compatibility为1.8。 [img]http://files.jb51.net/file_images/article/201711/2017110314151638.jpg[/img] Project Structure 从Android Studio 3.0起,默认支持Java8,无需额外进行JackOptions配置。 [b]Android Profiler[/b] 从Android Studio 3.0起,新增Android Profiler来代替旧的Android Monitor工具。 Android Profiler提供了CPU、Memory和network等三个调试分析工具。 [img]http://files.jb51.net/file_images/article/201711/2017110314151639.jpg[/img] Android Profiler Android Profiler的详细使用方法参考官方文档。 [url=https://developer.android.google.cn/studio/profile/cpu-profiler.html#method_traces]CPU Profiler[/url] [url=https://developer.android.com/studio/profile/memory-profiler.html]Memory Profiler[/url] [url=https://developer.android.google.cn/studio/profile/network-profiler.html]Network Profiler[/url] [b]Device File Explorer[/b] 在Android Studio 3.0主界面的右下角,点开"Device File Explorer",可访问当前连接设备的文件。 [img]http://files.jb51.net/file_images/article/201711/2017110314151640.jpg[/img] 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程素材网。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部