詳解AndroidStudio3.0的新特性與適配

簡(jiǎn)介

創(chuàng)新互聯(lián)建站是一家專業(yè)提供巴中企業(yè)網(wǎng)站建設(shè),專注與成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、H5網(wǎng)站設(shè)計(jì)、小程序制作等業(yè)務(wù)。10年已為巴中眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)絡(luò)公司優(yōu)惠進(jìn)行中。

Android Studio升級(jí)到3.0后,有不少的改動(dòng)和新特性,先貼出官方的遷移說明。

本文會(huì)持續(xù)收集與總結(jié)本人在使用Android Studio 3.0進(jìn)行開發(fā)的過程中所遇到的問題。

版本配置

Gradle版本

  1. Android Studio 3.0需要的Gradle版本至少為4.1。
  2. 如果是使用gradle wrapper,則工程根目錄/gradle/wrapper/gradle-wrapper.properties中的distributionUrl字段為https\://services.gradle.org/distributions/gradle-4.1-all.zip。

Android Gradle插件版本

Android Studio 3.0需要Android Gradle插件版本為3.0.0。

Android Studio 3.0默認(rèn)使用Google's Maven Repository來下載Android Support Library,所以在腳本中要使用google()來加入谷歌倉庫。

工程根目錄/build.gradle的相關(guān)配置如下。

buildscript {
  repositories {
    google()
  }
  dependencies {
    classpath 'com.android.tools.build:gradle:3.0.0'
  }
}

使用annotationProcessor

從Android Studio 3.0開始,使用annotationProcessor代替apt。不可再使用apt,否則會(huì)編譯報(bào)錯(cuò)。

Error:android-apt plugin is incompatible with the Android Gradle plugin.  Please use 'annotationProcessor' configuration instead.

比如在Android Studio 3.0之前在application模塊導(dǎo)入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,不用再導(dǎo)入android-apt插件。

dependencies {
  compile 'com.jakewharton:butterknife:8.4.0'
  annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
}

修改apk名稱

常用的修改輸出的apk文件的名稱的腳本如下。

def apkBaseName() {
  // 先查找project.ext.apkName變量,若無則使用項(xiàng)目名
  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中執(zhí)行此腳本會(huì)報(bào)錯(cuò)如下,原因是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

不再設(shè)置outputFile屬性,而是設(shè)置outputFileName。同時(shí)把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"
    }
  }
}

AAPT2

為了改進(jìn)增量資源處理,Android Gradle插件3.0默認(rèn)開啟AAPT2。

在舊項(xiàng)目中開啟AAPT2,有時(shí)候會(huì)報(bào)錯(cuò),如:

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推出了新的Java Library Plugin配置,而Android Gradle插件3.0是使用Gradle 4.1的,因此,需要注意更改為新的依賴配置。

舊的依賴配置,如compile project(':base-library'),會(huì)導(dǎo)致如下錯(cuò)誤。應(yīng)該修改為implementation project(':base-library')。

Error:Cannot choose between the following configurations of project :base-library:
 - debugApiElements
 - debugRuntimeElements
 - releaseApiElements
 - releaseRuntimeElements

flavor

從Android Gradle插件3.0開始,如果build.gradle中有自定義的productFlavors配置,需要添加自定義的flavorDimensions(風(fēng)味維度),否則會(huì)編譯報(bào)錯(cuò)。

Error:All flavors must now belong to a named flavor dimension.
The flavor 'flavor_name' is not assigned to a flavor dimension.

解決方法是:先定義一個(gè)flavorDimensions,之后在每個(gè)flavor中指定為這個(gè)dimension。

android {
  flavorDimensions 'core'
  
  productFlavors {
    beta {
      dimension 'core'
    }
    
    production {
      dimension 'core'
    }
  }
}

在設(shè)置flavorDimensions之前,最終的Build Variant = Product Flavor + Build Type。而設(shè)置之后,最終的Build Variant = 維度1 + 維度2 + ... + 維度n + Build Type。

Kotlin支持

在Android Studio 3.0之前,使用Kotlin需要進(jìn)行額外的配置。而Android Studio 3.0開始,默認(rèn)內(nèi)置支持Kotlin,無需額外配置。

使用Android Studio工具欄中的Code -> Convert Java File To Kotlin File,可將.java文件轉(zhuǎn)為.kt文件。

Java8支持

從Android Studio 2.1起,官方通過Jack來支持Java8,從而開發(fā)者能使用Lambda等特性。

android {
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
  
  defaultConfig {
    jackOptions {
      enabled true
    }
  }
}

可在Android Studio工具欄,F(xiàn)ile -> Project Structure,修改Source Compatibility和Target Compatibility為1.8。

詳解Android Studio 3.0的新特性與適配

Project Structure

從Android Studio 3.0起,默認(rèn)支持Java8,無需額外進(jìn)行JackOptions配置。

Android Profiler

從Android Studio 3.0起,新增Android Profiler來代替舊的Android Monitor工具。

Android Profiler提供了CPU、Memory和network等三個(gè)調(diào)試分析工具。

詳解Android Studio 3.0的新特性與適配

Android Profiler

Android Profiler的詳細(xì)使用方法參考官方文檔。

CPU Profiler
Memory Profiler
Network Profiler

Device File Explorer

在Android Studio 3.0主界面的右下角,點(diǎn)開"Device File Explorer",可訪問當(dāng)前連接設(shè)備的文件。

詳解Android Studio 3.0的新特性與適配

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

當(dāng)前標(biāo)題:詳解AndroidStudio3.0的新特性與適配
轉(zhuǎn)載注明:http://bm7419.com/article32/igsesc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營(yíng)銷、建站公司面包屑導(dǎo)航、做網(wǎng)站、微信公眾號(hào)、關(guān)鍵詞優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司