ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Follow publication

Android Studio Live Code Templates to Save Your Time Coding

Tomáš Repčík
ProAndroidDev
Published in
6 min readJul 5, 2023

Photo by ilgmyzin on Unsplash

Built-in templates:

Some pure Kotlin templates:

Some Android specific for Jetpack Compose:

Custom templates

How to add a new template

Example of the Live Template

Advanced behaviours

Template ideas

MutableStateFlow and StateFlow

private val _$StateFlow$: MutableStateFlow<$StateType$> = MutableStateFlow($StateDefault$)
val $StateFlow$ = _$StateFlow.asStateFlow()

HiltViewModel

@HiltViewModel
class $VmName$ViewModel @Inject constructor($Parameters$) : ViewModel() {

private val _$StateName$State: MutableStateFlow<$StateType$> = MutableStateFlow($StateDefault$)
val $StateName$State = _$StateName$State.asStateFlow()

}

Scoped Coroutine function to ViewModel

private fun $MethodName$() = viewModelScope.launch(Dispatchers.$Dispatcher$) {
Log.i(TAG, "$MethodName$: invoked")
$END$
}

Module Class with Binds in Hilt

@Module
@InstallIn($Component$::class)
abstract class $Module$DI {

@Binds
@$Injector$
abstract fun provides$Module$($ModuleCamelCase$: $Module$): $Module$Template

}

Binds in Hilt

@Binds
@$Injector$
abstract fun provides$Module$($ModuleCamelCase$: $Module$): $Module$Template

Module Class with Provides in Hilt

@Module
@InstallIn($Component$::class)
object $Module$DI {

@Provides
@$Injector$
fun provides$Module$($ModuleCamelCase$: $Module$): $Module$Template = $END$
}

Provides in Hilt

@Provides
@$Injector$
fun provides$Module$($ModuleCamelCase$: $Module$): $Module$Template = $END$

Definition of Dao for Room database

@Dao
interface $Type$Dao {
@Query("SELECT * FROM $Type$")
fun getAll(): List<$Type$>

@Query("SELECT * FROM $Type$ WHERE id = :id")
fun getById(id: Int): $Type$?

@Insert(onConflict = OnConflictStrategy.REPLACE)
fun addItem(item: $Type$)

@Update(onConflict = OnConflictStrategy.REPLACE)
fun updateItem(item: $Type$)

@Delete
fun deleteItem(item: $Type$)
}

Test class for unit testing

@RunWith(JUnit4::class)
class $TestClassName$Test {

@get:Rule
val mockitoRule: MockitoRule = MockitoJUnit.rule()

@Before
fun setUp() {

}

@After
fun tearDown() {

}

@Test
fun `Initial test`() {
val sut: $TestModule$ = $TestModule$($Parameters$)
}

@Test
fun `$TestDescription$`() {
// ARRANGE
val sut: $TestModule$ = $TestModule$($Parameters$)
$END$
// ACTION
// CHECK
}
}

Single unit test

@Test
fun `$TestDescription$`() {
// ARRANGE
val sut: $TestModule$ = $TestModule$($Parameters$)
$END$
// ACTION
// CHECK
}

Mock creation

@Mock
private var $variable$: $variableType$

Conclusion

More articles:

Android development

23 stories

Resources:

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

The latest posts from Android Professionals and Google Developer Experts.

https://tomasrepcik.dev/ - Flutter app developer with experience in native app development and degree in biomedical engineering.

Responses (3)

Write a response