Automatische Codeverbesserung bei Android Studio Commit

Wie Sie wissen, gibt es in Android Studio zwei Möglichkeiten, mit Git zu arbeiten.



Der erste klassische Weg ist die Verwendung der Konsole. Der Vorteil dieses Ansatzes ist vor allem die ZuverlÀssigkeit. Die GUI kann stecken bleiben und beispielsweise in der Rebase-Phase einfrieren, sodass das Abbrechen / Fortfahren / Wechseln zu einem anderen Zweig nicht hilft. Konsolenbefehle helfen Ihnen, sie sind immer problemlos.



Die zweite Möglichkeit besteht darin, die von Android Studio bereitgestellte BenutzeroberflĂ€che zu verwenden. Die Vorteile liegen auf der Hand - bei einer niedrigeren Einstiegsschwelle ist klarer, was ĂŒberhaupt getan werden kann. Außerdem gibt es verschiedene Brötchen aus dem Studio, um bequem mit Git arbeiten zu können. Einer von ihnen wird diskutiert. Über die GUI können Sie ĂŒbrigens auch die Maus in Ruhe lassen und den Hotkey verwenden



Im Artikel verwendete Hotkeys



Umschalt + Umschalt (Doppelklick-Umschalt) - Suchfenster. Ermöglicht die Suche nach Code / Ressourcen sowie nach verschiedenen Aktionen und Einstellungen.



Strg + Alt + L (⌘ + ⌄ + L) -> Code-Formatierung



Umschalt + Strg + Alt + L (⇧ + ⌘ + ⌄ + L) → Code-Formatierung → Code-Formatierung mit Parametern.



ruft dieses Fenster auf

Bild



Worum geht es und wo ist es zu finden?



, , — , , pull request. , hotkey Ctrl+Alt+L, .

Idea/AndroidStudio ,



commit' Android Studio :



, , Before Commit. ,



?



✓ Reformat code



code style. ctrl+shift+alt+L clean up.



code style Settings → Editor → Code Style



Tab and Indents

Wendet den Zeileneinzug gemĂ€ĂŸ dem Codestil an (Einzelheiten finden Sie unter Codestil im gleichnamigen Abschnitt).



Im Beispiel werden die zur Bildung der EinrĂŒckung verwendeten Leerzeichen (mit Punkten markiert) durch Tabulatoren (mit einem Pfeil markiert) ersetzt.

Vor dem Festschreiben



class CleanTab(context: Context) {

....val date = Date()

....val button = Button(context)
....val textView = TextView(context)
....val ratingBar = RatingBar(context)
....val imageView = ImageView(context)
}


Nach dem Festschreiben



class CleanTab(context: Context) {

-> val date = Date()

-> val button = Button(context)
-> val textView = TextView(context)
-> val ratingBar = RatingBar(context)
-> val imageView = ImageView(context)
}


Spaces

code-style.



Date, button =, args main {



class CleanSpaces(context:Context) {

    val date = Date( )

    val button= Button(context)

    fun main(arg: Args){

    }
}




class CleanSpaces(context: Context) {

    val date = Date()

    val button = Button(context)

    fun main(arg: Args) {

    }
}


Wrapping and Braces

. , / .



else, catch , ( code style)

manyArguments 80 ( code style),



class CleanWrappingAndBraces {

    fun condition() {
        if (liveData != null) {
            <..>
        }
        else {
            <..>
        }
    }

    fun catching() {
        try {
            <..>
        }
        catch (e: Exception) {
            <..>
        }
    }

    fun manyArguments(userId: Int, issuerCountryId: String, sendingCountryId: String, receivingCountryId: String) {

    }
}




class CleanWrappingAndBraces {

    fun condition() {
        if (liveData != null) {
            <..>
        } else {
            <..>
        }
    }

    fun catching() {
        try {
            <..>
        } catch (e: Exception) {
            <..>
        }
    }

    fun manyArguments(userId: Int, issuerCountryId: String,
                    sendingCountryId: String,
                    receivingCountryId: String) {

}


Blank Lines

. ( Code Style, )



}, 2



class CleanBlank {

    fun foo() {

    }

    fun bar() {

    }

}




class CleanBlank {

    fun foo() {

    }

    fun bar() {

    }

}


Rearrange code



, -. XML HTML . Setting → Editor → CodeStyle → XML/HTML → Arrangement



! . Rearrange , , . : LinearLayout, , Button TextView, . —


xmlns , code-style(xmlns:android , xmlns:<...> .

, xmlns:tools xmlns:app , . , rearrange



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    xmlns:tools="http://schemas.android.com/tools" android:layout_height="match_parent"
    android:orientation="vertical" xmlns:app="http://schemas.android.com/apk/res-auto">

</LinearLayout>




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

</LinearLayout>


android:padding code-style. style android:id



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="8dp"
        android:paddingBottom="8dp"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        tools:srcCompat="@tools:sample/avatars" />

    <TextView
        style="@style/TextAppearance.MaterialComponents.Body1"
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello World" />

</LinearLayout>




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="8dp"
        android:paddingTop="8dp"
        android:paddingRight="8dp"
        android:paddingBottom="8dp"
        tools:srcCompat="@tools:sample/avatars" />

    <TextView
        android:id="@+id/title"
        style="@style/TextAppearance.MaterialComponents.Body1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello World" />

</LinearLayout>


Optimize import



, code style. ctrl+shift+alt+L optimize import.



optimize imports import java.util.* , ,



import android.content.Context
import android.widget.Button
import java.util.*

class RemoveUnused(context: Context) {

    val button = Button(context)
}




import android.content.Context
import android.widget.Button

class RemoveUnused(context: Context) {

    val button = Button(context)
}


n , .

m enum java static ,



Settings → Editor → Code Style → Kotlin → Imports



android.widget android.widget.*



import android.content.Context
import android.widget.Button
import android.widget.ImageButton
import android.widget.ImageView
import android.widget.TextView
import android.widget.RatingBar
import java.util.*

class MergeImport(context: Context) {

    <..>
}




import android.content.Context
import android.widget.*
import java.util.*

class MergeImport(context: Context) {

    <..>
}


, (m = 0, n=0), ( ) // .

import android.widget. import java.util.



import android.content.Context
import android.widget.*
import java.util.*

class MergeImport(context: Context) {

    <..>




import android.content.Context
import android.widget.Button
import android.widget.ImageButton
import android.widget.ImageView
import android.widget.TextView
import android.widget.RatingBar
import java.util.*

class MergeImport(context: Context) {

    <..>
}


, ,



import android.content.Context
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import java.util.*
import android.widget.RatingBar

class SortImport(context: Context) {

    <..>
}




import android.content.Context
import android.widget.Button
import android.widget.ImageView
import android.widget.RatingBar
import android.widget.TextView
import java.util.*

class SortImport(context: Context) {

    <..>
}


Perform code analysis



. . — , , , Deprecated



fun TextView.format(message: String) {
   val htmlText = Html.fromHtml(message)
}


, :



Review :



Check TODO



, TODO, . , //TODO, //FIXME, todo, Setting → Editor → TODO.



todo, .



, todo. review todo



Clean up



. (. ), deprecated . Actions → Code cleanup. , .



:



  • deprecated ( )
  • view


! , !


Redutant code
-, .. non-nullable



data class User(val firstName: String, val secondName: String)

val user = User("", "")

fun getUserFullName(): String {
    val firstName = user?.firstName
    val secondName = user.secondName ?: return firstName

    return "$firstName $secondName"
}




data class User(val firstName: String, val secondName: String)

val user = User("", "")

fun getUserFullName(): String {
    val firstName = user.firstName
    val secondName = user.secondName

    return "$firstName $secondName"
}


public, ..



data class User(val firstName: String, val secondName: String)

val user = User("", "")

public fun getUserFullName(): String {
    val firstName = user.firstName
    val secondName = user.secondName

    return "$firstName $secondName"
}




data class User(val firstName: String, val secondName: String)

val user = User("", "")

fun getUserFullName(): String {
    val firstName = user.firstName
    val secondName = user.secondName

    return "$firstName $secondName"
}




class ExecutionClass(val exec: () -> Unit)

val exec = ExecutionClass() {
    doIt()
}




class ExecutionClass(val exec: () -> Unit)

val exec = ExecutionClass {
    doIt()
}


Deprecated

deprecated ReplaceWith(), cleanup . , — , ReplaceWith(). . / . / , ( ), , , . ,





@Deprecated(" ", ReplaceWith("newMethod()"))
fun oldMethod() = Unit

fun newMethod() = Unit

@Deprecated(" ", ReplaceWith("NewClass"))
class OldClass

class NewClass

class GetDeprecatedCodeUseCase(val clazz: OldClass) {

    init {
        val initData = oldMethod()
    }
}




@Deprecated(" ", ReplaceWith("newMethod()"))
fun oldMethod() = Unit

fun newMethod() = Unit

@Deprecated(" ", ReplaceWith("NewClass"))
class OldClass

class NewClass

class GetDeprecatedCodeUseCase(val clazz: NewClass) {

    init {
        val initData = newMethod()
    }
}




, Android Studio . , , , . , .



:



Optimize import Reformat code . .



Rearrange Clean up . , - , (Rearrange) (Clean up)



ÜberprĂŒfen Sie TODO und fĂŒhren Sie eine Codeanalyse durch. Sie können auch furchtlos verwendet werden. Sie beeinflussen den Code in keiner Weise, sie geben nur nervige Hinweise. Ja, wenn alles in Ihrem Projekt vollstĂ€ndig auf TODO und veraltetem Code basiert, werden sie kein Ende haben und sie werden Ă€rgerlicher. Wenn Sie jedoch einen ziemlich sauberen Code in Ihrem Projekt haben und versuchen, solche Momente zu minimieren, bieten die Helfer eine hervorragende Möglichkeit, den Code zu ĂŒberarbeiten, wenn Sie eine Auslassung hĂ€tten vornehmen können.




All Articles