LoginSignup
0
0

More than 5 years have passed since last update.

用 Android Studio 3 beta 碰到的兩個問題

Posted at

發表大會之後,決定把電腦裡長草的 Android Studio 草除一除,裝 3 beta 來用用。
但是建立新專案之後,碰到兩個主要的問題,在這邊筆記一下。

Kotlin 版本問題

第一個是新建專案之後, 預設的設定黨 Gradle 就會無法成功同步
在猜可能是前一版本 Android Studio 我也有裝 Kotlin plugin 的關係吧(純猜測)
具體原因還不清楚

我碰到的錯誤訊息如下:

Error:Unable to find method 'com.android.build.gradle.internal.variant.BaseVariantData.getOutputs()Ljava/util/List;'. Possible causes for this unexpected error include:

  • Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.) Re-download dependencies and sync project (requires network)
  • The state of a Gradle build process (daemon) may be corrupt. Stopping all Gradle daemons may solve this problem. Stop Gradle build processes (requires restart)
  • Your project may be using a third-party plugin which is not compatible with the other plugins in the project or the version of Gradle requested by the project.

In the case of corrupt Gradle processes, you can also try closing the IDE and then killing all Java processes.

稍微查了一下,問題發生的原因是 gradle 設定檔裡面的 Kotlin 版本有誤

build.gradle(Project)
... (省略) ...


buildscript {
    ext.kotlin_version = '1.1.2-3'


... (省略) ...

1.1.2-3 改成 1.1.2-4 即可, Gradle 就可以順利同步了 :tada::tada::tada:

Kotlin Extensions

這可以說是神器(?)吧,
可以很方便地在程式碼裡面取用 layout 檔裡面有取好名字的 UI ,
但是這不能靠 Kotlin 預設的 library 就可以達成,必須有兩個步驟:

在 "Project" 的 Gradle 設定檔 (跟上一節用到的是同一個檔案)的 dependencies 加上另外一個資源

build.gradle(Project)
... (省略) ...


dependencies {
    classpath 'com.android.tools.build:gradle:3.0.0-alpha1'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version" // 加上這一行


... (省略) ...

在 Module: app 的 Gradle 裡面採用 Kotlin Android Extensions

build.gradle(Module)
... (省略) ...


apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions' // 加上這一行


... (省略) ...

在想要使用的 kt 檔的檔頭加上 import 程式碼引入該 UI Layout , prefix 是 kotlinx

接著,假如 layout 的名稱叫 activity_main ,在 kt 檔裡面就可以這樣子 import

MainActivity.kt
... (省略) ...


import kotlinx.android.synthetic.main.activity_main.*


... (省略) ...

如果再 layout 檔裡面有一個這樣子名稱的 TextView

activity_main.xml
... (省略) ...


android:id="@+id/nameTextView


... (省略) ...

在使用的時候,就可以直接這樣用,不需要先抓取資源、再放到某個變數裡面才可以使用。 :tada::tada::tada:

MainActivity.kt
... (省略) ...


nameTextView.text = "John Doe"


... (省略) ...

以上,
繼續往下練習~

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0