Skip to content

Instantly share code, notes, and snippets.

View mehdiyari's full-sized avatar
💻
Working

Mehdi Yari mehdiyari

💻
Working
View GitHub Profile
@mehdiyari
mehdiyari / blogpost_gist_ZygoteInit_Preload_Functions_List.md
Last active July 20, 2025 15:12
This gist is part of Zygote article by mehdiyari.
Function Description
preloadClasses Loads core Java classes listed in /system/etc/preloaded-classes, which are necessary for the system and apps.
cacheNonBootClasspathClassLoaders Loads libraries and classes commonly used by many apps but not part of the boot classpath, such as HIDL libraries, android.test.base, apache.http and certain AndroidX window manager components.
preloadResources Loads frequently used resources like drawables, colors, strings, and typed arrays so they can be shared across processes.
nativePreloadAppProcessHALs Loads HAL native libraries, including Audio HAL, Camera HAL, and other shared native components, to prepar
class Application : Application() {
override fun onCreate() {
super.onCreate()
if (BuildConfig.DEBUG) {
applyStrictMode()
}
}
private fun applyStrictMode() {
/**
* This is simple processor that use KSP API to generate
* extension function for annotated classes with [HelloWorldAnnotation]
*/
class HelloWorldProcessor(
private val logger: KSPLogger,
private val codeGenerator: CodeGenerator
) : SymbolProcessor {
override fun process(resolver: Resolver): List<KSAnnotated> {
@mehdiyari
mehdiyari / MetaData.kt
Last active April 22, 2022 16:48
This gist is part of meta-programming with kotlin articles
@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.CLASS)
@SinceKotlin("1.3")
annotation class Metadata(
@get:JvmName("k") val kind: Int = 1,
@get:JvmName("mv") val metadataVersion: IntArray = [],
@get:JvmName("bv") val bytecodeVersion: IntArray = [1, 0, 3],
@get:JvmName("di") val data1: Array<String> = [],
@get:JvmName("d2") val data2: Array<String> = [],
@get:JvmName("xs") val extraString: String = "",
@mehdiyari
mehdiyari / HelloWorldAnnotation.kt
Created April 22, 2022 16:43
This gist is part of meta-programming with kotlin articles
// Declare Annotation in Kotlin
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.SOURCE)
@Repeatable
annotation class HelloWorldAnnotation
@mehdiyari
mehdiyari / HelloWorldProcessor.kt
Created March 31, 2022 16:37
This gist is part of meta-programming with kotlin articles
/**
* This is simple processor that use JSR-269 API and kotlinpoet library to generate
* extension function for annotated classes with [HelloWorldAnnotation]
*/
class HelloWorldProcessor : AbstractProcessor() {
override fun process(annotations: MutableSet<out TypeElement>?, roundEnv: RoundEnvironment?): Boolean {
val typeElement = annotations?.firstOrNull() ?: return false
for (annotatedElement in roundEnv?.getElementsAnnotatedWith(typeElement) ?: setOf()) {
@mehdiyari
mehdiyari / RefelectionNotAllowed.kt
Created March 1, 2022 08:55
This gist is part of meta-programming with kotlin articles
/**
* Simply security manager that check the packages and if equal
* to "java.lang.reflect" or "kotlin.reflect" throws [SecurityException]
*/
class AppSecurityManager : SecurityManager() {
override fun checkPackageAccess(pkg: String?) {
if (pkg == "java.lang.reflect" || pkg == "kotlin.reflect") {
throw SecurityException("Reflection is not allowed in this program")
}
@mehdiyari
mehdiyari / Singleton.kt
Created March 1, 2022 08:19
This gist is part of meta-programming with kotlin articles
/**
* Simple singleton class with double-check pattern
*/
class Singleton private constructor() {
private val data: String = "data property value"
@Volatile
private var singleton: Singleton? = null
@mehdiyari
mehdiyari / RefelectionSideEffect.kt
Created March 1, 2022 08:17
This gist is part of meta-programming with kotlin articles
fun main() {
val singletonKClass = Singleton::class
singletonKClass.constructors.firstOrNull { it.visibility == KVisibility.PRIVATE }?.also { privateConstructor ->
/**
* We give access to private constructor and we can create instance
* from [Singleton] class [privateConstructor.call] method
*/
privateConstructor.isAccessible = true
val singletonNewObject = privateConstructor.call()
dependencies {
implementation "org.jetbrains.kotlin:kotlin-reflect:{latest_version}"
}