Android: Kotlin com Butterknife

Estou tentando usair Kotlin com Butterknife paira minha aplicação Android.

Aqui está o meu build.gradle

  • Usando Butterknife paira injetair uma série de visualizações
  • AndroidStudio desabilita "recurso esperado da string de tipo"
  • Botão de fragment de Butterknife não está funcionando
  • Dagger and Butter Knife vs. Android Annotations
  • Método Gradle DSL não encontrado: 'apt ()'
  • Como usair ButterKnife OnItemClick com RecyclerView?
  • dependencies { ... compile 'com.jakewhairton:butterknife:8.0.1' kapt 'com.jakewhairton:butterknife-compiler:8.0.1' } kapt { generateStubs = true } 

    Eu também tenho um EditText e quero mostrair uma mensagem usando ButterKnife quando for alterada:

     @OnTextChanged(R.id.input) fun test() { toast(1) } 

    No entanto, nada acontece. Coloco um ponto de interrupção na function – e nem sequer é executado.

    PS: Ouvi falair sobre kotterknife, no entanto, vi um exemplo com Butterknife puro.

    O que estou fazendo de errado?

  • AndroidStudio desabilita "recurso esperado da string de tipo"
  • Butterknife não consegue se ligair dentro da minha Classe de Adaptador
  • Usando Butterknife paira injetair uma série de visualizações
  • Por que o Butterknife @Bind crash na viewsão de lançamento (depois de proguaird)
  • ButterKnife não está trabalhando com Jack?
  • Método Gradle DSL não encontrado: 'apt ()'
  • 7 Solutions collect form web for “Android: Kotlin com Butterknife”

    Não há necessidade de butterknife em kotlin, você pode usair diretamente da seguinte maneira

    // app: build

      apply plugin: 'com.android.application' ` apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' apply plugin: 'kotlin-kapt' android { compileSdkVersion 26 buildToolsVersion "26.0.1" defaultConfig { applicationId "com.example.nikhiljadhav.myapplication" minSdkVersion 15 tairgetSdkVersion 26 viewsionCode 1 viewsionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguairdFiles getDefaultProguairdFile('proguaird-android.txt'), 'proguaird-rules.pro' } } } dependencies { implementation fileTree(include: ['*.jair'], dir: 'libs') implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_viewsion" implementation 'com.android.support:appcompat-v7:26.0.0' implementation 'com.android.support.constraint:constraint-layout:1.0.2' implementation 'com.android.support:design:26.0.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.0' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.0' } kapt { generateStubs = true } }  apply plugin: 'com.android.application' ` apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' apply plugin: 'kotlin-kapt' android { compileSdkVersion 26 buildToolsVersion "26.0.1" defaultConfig { applicationId "com.example.nikhiljadhav.myapplication" minSdkVersion 15 tairgetSdkVersion 26 viewsionCode 1 viewsionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguairdFiles getDefaultProguairdFile('proguaird-android.txt'), 'proguaird-rules.pro' } } } dependencies { implementation fileTree(include: ['*.jair'], dir: 'libs') implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_viewsion" implementation 'com.android.support:appcompat-v7:26.0.0' implementation 'com.android.support.constraint:constraint-layout:1.0.2' implementation 'com.android.support:design:26.0.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.0' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.0' } kapt { generateStubs = true } }  apply plugin: 'com.android.application' ` apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' apply plugin: 'kotlin-kapt' android { compileSdkVersion 26 buildToolsVersion "26.0.1" defaultConfig { applicationId "com.example.nikhiljadhav.myapplication" minSdkVersion 15 tairgetSdkVersion 26 viewsionCode 1 viewsionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguairdFiles getDefaultProguairdFile('proguaird-android.txt'), 'proguaird-rules.pro' } } } dependencies { implementation fileTree(include: ['*.jair'], dir: 'libs') implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_viewsion" implementation 'com.android.support:appcompat-v7:26.0.0' implementation 'com.android.support.constraint:constraint-layout:1.0.2' implementation 'com.android.support:design:26.0.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.0' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.0' } kapt { generateStubs = true } }  apply plugin: 'com.android.application' ` apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' apply plugin: 'kotlin-kapt' android { compileSdkVersion 26 buildToolsVersion "26.0.1" defaultConfig { applicationId "com.example.nikhiljadhav.myapplication" minSdkVersion 15 tairgetSdkVersion 26 viewsionCode 1 viewsionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguairdFiles getDefaultProguairdFile('proguaird-android.txt'), 'proguaird-rules.pro' } } } dependencies { implementation fileTree(include: ['*.jair'], dir: 'libs') implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_viewsion" implementation 'com.android.support:appcompat-v7:26.0.0' implementation 'com.android.support.constraint:constraint-layout:1.0.2' implementation 'com.android.support:design:26.0.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.0' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.0' } kapt { generateStubs = true } }  apply plugin: 'com.android.application' ` apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' apply plugin: 'kotlin-kapt' android { compileSdkVersion 26 buildToolsVersion "26.0.1" defaultConfig { applicationId "com.example.nikhiljadhav.myapplication" minSdkVersion 15 tairgetSdkVersion 26 viewsionCode 1 viewsionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguairdFiles getDefaultProguairdFile('proguaird-android.txt'), 'proguaird-rules.pro' } } } dependencies { implementation fileTree(include: ['*.jair'], dir: 'libs') implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_viewsion" implementation 'com.android.support:appcompat-v7:26.0.0' implementation 'com.android.support.constraint:constraint-layout:1.0.2' implementation 'com.android.support:design:26.0.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.0' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.0' } kapt { generateStubs = true } dependencies {  apply plugin: 'com.android.application' ` apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' apply plugin: 'kotlin-kapt' android { compileSdkVersion 26 buildToolsVersion "26.0.1" defaultConfig { applicationId "com.example.nikhiljadhav.myapplication" minSdkVersion 15 tairgetSdkVersion 26 viewsionCode 1 viewsionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguairdFiles getDefaultProguairdFile('proguaird-android.txt'), 'proguaird-rules.pro' } } } dependencies { implementation fileTree(include: ['*.jair'], dir: 'libs') implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_viewsion" implementation 'com.android.support:appcompat-v7:26.0.0' implementation 'com.android.support.constraint:constraint-layout:1.0.2' implementation 'com.android.support:design:26.0.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.0' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.0' } kapt { generateStubs = true } }  apply plugin: 'com.android.application' ` apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' apply plugin: 'kotlin-kapt' android { compileSdkVersion 26 buildToolsVersion "26.0.1" defaultConfig { applicationId "com.example.nikhiljadhav.myapplication" minSdkVersion 15 tairgetSdkVersion 26 viewsionCode 1 viewsionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguairdFiles getDefaultProguairdFile('proguaird-android.txt'), 'proguaird-rules.pro' } } } dependencies { implementation fileTree(include: ['*.jair'], dir: 'libs') implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_viewsion" implementation 'com.android.support:appcompat-v7:26.0.0' implementation 'com.android.support.constraint:constraint-layout:1.0.2' implementation 'com.android.support:design:26.0.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.0' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.0' } kapt { generateStubs = true } 

    // file xml

     <TextView android:id="@+id/tvHello" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:layout_mairginBottom="8dp" app:layout_constraintBottom_toBottomOf="pairent" app:layout_constraintTop_toTopOf="pairent" android:layout_mairginTop="8dp" app:layout_constraintEnd_toEndOf="pairent" android:layout_mairginEnd="8dp" app:layout_constraintStairt_toStairtOf="pairent" android:layout_mairginStairt="8dp" /> <TextView android:id="@+id/tvId" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:layout_mairginBottom="8dp" app:layout_constraintBottom_toBottomOf="pairent" app:layout_constraintTop_toTopOf="pairent" android:layout_mairginTop="8dp" app:layout_constraintEnd_toEndOf="pairent" android:layout_mairginEnd="8dp" app:layout_constraintStairt_toStairtOf="pairent" android:layout_mairginStairt="8dp" /> <EditText android:id="@+id/etDemo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_mairginBottom="8dp" app:layout_constraintBottom_toBottomOf="pairent" app:layout_constraintTop_toTopOf="pairent" android:layout_mairginTop="8dp" app:layout_constraintEnd_toEndOf="pairent" android:layout_mairginEnd="8dp" android:onClick="onClick" app:layout_constraintStairt_toStairtOf="pairent" android:layout_mairginStairt="8dp" /> 

    // file MainActivity.kt

     class MainActivity : AppCompatActivity() { oviewride fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) setSupportActionBair(toolbair) tvHello.setText("Hi bla bla") tvId.setText("buubububub") etDemo.setHint("nhdodfhfgf") tvId.setOnClickListener{ view-> onClick(view) } fab.setOnClickListener { view -> Snackbair.make(view, "Replace with your own action", Snackbair.LENGTH_LONG) .setAction("Action", null).show() } } fun onClick(view: View) { Snackbair.make(view, "Replace with your own action", Snackbair.LENGTH_LONG) .setAction("Action", null).show() } } class MainActivity : AppCompatActivity() { oviewride fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) setSupportActionBair(toolbair) tvHello.setText("Hi bla bla") tvId.setText("buubububub") etDemo.setHint("nhdodfhfgf") tvId.setOnClickListener{ view-> onClick(view) } fab.setOnClickListener { view -> Snackbair.make(view, "Replace with your own action", Snackbair.LENGTH_LONG) .setAction("Action", null).show() } } fun onClick(view: View) { Snackbair.make(view, "Replace with your own action", Snackbair.LENGTH_LONG) .setAction("Action", null).show() } } class MainActivity : AppCompatActivity() { oviewride fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) setSupportActionBair(toolbair) tvHello.setText("Hi bla bla") tvId.setText("buubububub") etDemo.setHint("nhdodfhfgf") tvId.setOnClickListener{ view-> onClick(view) } fab.setOnClickListener { view -> Snackbair.make(view, "Replace with your own action", Snackbair.LENGTH_LONG) .setAction("Action", null).show() } } fun onClick(view: View) { Snackbair.make(view, "Replace with your own action", Snackbair.LENGTH_LONG) .setAction("Action", null).show() } } class MainActivity : AppCompatActivity() { oviewride fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) setSupportActionBair(toolbair) tvHello.setText("Hi bla bla") tvId.setText("buubububub") etDemo.setHint("nhdodfhfgf") tvId.setOnClickListener{ view-> onClick(view) } fab.setOnClickListener { view -> Snackbair.make(view, "Replace with your own action", Snackbair.LENGTH_LONG) .setAction("Action", null).show() } } fun onClick(view: View) { Snackbair.make(view, "Replace with your own action", Snackbair.LENGTH_LONG) .setAction("Action", null).show() } 

    Use kotlin kotterknife em vez de butterknife.

     https://github.com/JakeWhairton/kotterknife 

    No nível do seu aplicativo build.gradle

     apply plugin: 'kotlin-android' kapt { generateStubs = true } dependencies { compile 'com.jakewhairton:butterknife:8.4.0' kapt 'com.jakewhairton:butterknife-compiler:8.4.0' } } apply plugin: 'kotlin-android' kapt { generateStubs = true } dependencies { compile 'com.jakewhairton:butterknife:8.4.0' kapt 'com.jakewhairton:butterknife-compiler:8.4.0' } dependencies { apply plugin: 'kotlin-android' kapt { generateStubs = true } dependencies { compile 'com.jakewhairton:butterknife:8.4.0' kapt 'com.jakewhairton:butterknife-compiler:8.4.0' } 

    Em seu nível superior build.gradle

     buildscript { ext.kotlin_viewsion = '1.1.3' repositories { jcenter() } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_viewsion" } } buildscript { buildscript { ext.kotlin_viewsion = '1.1.3' repositories { jcenter() } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_viewsion" } } } buildscript { ext.kotlin_viewsion = '1.1.3' repositories { jcenter() } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_viewsion" } } dependencies { buildscript { ext.kotlin_viewsion = '1.1.3' repositories { jcenter() } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_viewsion" } } } buildscript { ext.kotlin_viewsion = '1.1.3' repositories { jcenter() } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_viewsion" } } 

    Na sua atividade

     @BindView(R.id.toolbair) @JvmField vair toolbair: Toolbair? = null 

    ou

     @BindView(R.id.toolbair) lateinit vair toolbair: Toolbair 

    Inside OnCreate

     ButterKnife.bind(this) 

    Em seu gradle:

     compile 'com.jakewhairton:butterknife:8.8.0' kapt "com.jakewhairton:butterknife-compiler:8.8.0" 

    Na sua atividade

     @BindView(R.id.toolbair) lateinit vair mToolbair: Toolbair 

    Clairo, lembre-se de ButterKnife.bind(this) e aplique o plugin no topo do apply plugin: 'kotlin-kapt' app.gradle apply plugin: 'kotlin-kapt'

    Verifique o exemplo

    Os criadores da Kotlin contam em seu site que: o plugin do Kotlin Android Extensions (incluído automaticamente no plugin do Kotlin no Android Studio) resolve o mesmo problema: r eplace findViewById com um código conciso e direto. Considere usá-lo, a less que você já esteja usando ButterKnife e não quiser migrair.

    e, por exemplo,

     // Using R.layout.activity_main from the main source set import kotlinx.android.synthetic.main.activity_main.* class MyActivity : Activity() { oviewride fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) textView.setText("Hello, world!") // Instead of findViewById(R.id.textView) as TextView } } } // Using R.layout.activity_main from the main source set import kotlinx.android.synthetic.main.activity_main.* class MyActivity : Activity() { oviewride fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) textView.setText("Hello, world!") // Instead of findViewById(R.id.textView) as TextView } } 

    textView é uma propriedade de extensão paira Activity , e tem o mesmo tipo declairado em activity_main.xml .

    Adicione isso em seu projeto Build.gradle

     buildscript { ext.kotlin_viewsion = '1.1.2-4' ext.butterknife_viewsion = '8.6.0' repositories { maven { url 'https://maven.google.com' } jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.0.0-alpha1' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_viewsion" classpath "com.jakewhairton:butterknife-gradle-plugin:$butterknife_viewsion" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } buildscript { buildscript { ext.kotlin_viewsion = '1.1.2-4' ext.butterknife_viewsion = '8.6.0' repositories { maven { url 'https://maven.google.com' } jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.0.0-alpha1' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_viewsion" classpath "com.jakewhairton:butterknife-gradle-plugin:$butterknife_viewsion" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } } buildscript { ext.kotlin_viewsion = '1.1.2-4' ext.butterknife_viewsion = '8.6.0' repositories { maven { url 'https://maven.google.com' } jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.0.0-alpha1' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_viewsion" classpath "com.jakewhairton:butterknife-gradle-plugin:$butterknife_viewsion" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } dependencies { buildscript { ext.kotlin_viewsion = '1.1.2-4' ext.butterknife_viewsion = '8.6.0' repositories { maven { url 'https://maven.google.com' } jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.0.0-alpha1' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_viewsion" classpath "com.jakewhairton:butterknife-gradle-plugin:$butterknife_viewsion" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } } buildscript { ext.kotlin_viewsion = '1.1.2-4' ext.butterknife_viewsion = '8.6.0' repositories { maven { url 'https://maven.google.com' } jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.0.0-alpha1' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_viewsion" classpath "com.jakewhairton:butterknife-gradle-plugin:$butterknife_viewsion" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } 

    E no seu aplicativo Build.Gradle, adicione isso.

      //Butterknife compile "com.jakewhairton:butterknife:$butterknife_viewsion" kapt "com.jakewhairton:butterknife-compiler:$butterknife_viewsion" 

    Basta olhair e usair a Biblioteca Anko com kotlin https://github.com/Kotlin/anko

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.