by McDev · 6y ago · 0 comments
This really rings true to me. I almost cringe when I have to explain how to do some of the most basic things to non android developers.
Aside from the dozens of libraries you need to include, and need to know where to find and what version is compatible with what, the tooling itself is an insurmountable mess for new developers. It's also not getting any simpler.
Like I was interviewing a guy for an Android role, and he was walking me through a pet project of his which consisted of literally one list view which updated it's contents to filter results out of an SQLite database as you type into a text field, and the project had literally dozens of files. It just made me sad.
I mean maybe there is something I don't understand, but the state of the field seems to be heavily driven by following what the "cool kids" are doing, and the tooling seems to have more to do with signaling that you're in the in-crowd than it has to do with actually delivering value.
Or you can pull in a library that seems to be popular and maintained and solves the problem that you have.
At least it's better than some other ecosystems, where it's "this thing is deprecated now. Nothing replaces it and you can't do similar functionality any other way, it's just deprecated." Or maybe I just haven't gotten to that part yet.
That's most likely overengineering. The only things you need for this are:
- An Activity
- Eventually, a Fragment, but you can do your work in the activity directly
- A database, optionally in another file to abstract out direct queries, but it can simply be direct SupportSQLite queries. Just, not on the main thread.
- A viewmodel if you don't want to hate yourself and write logic in your Activity, making it basically untestable.
- A layout file for your activity/fragment.
- A layout file for your recyclerview list element
That's 5 files. 6 if you include build.gradle, 7 with AndroidManifest.xml
This can all be written in under 500 total lines.
Library wise, all you need for that is androidx and recyclerview.
After that, you can very well put your activity/fragment/database logic in the same file, if you know you're never going to maintain it.
In racket/gui this could be maybe 100 clean lines of code in one file.
Android is complex, yes. Sometimes for no reason (hello God Activities). But sometimes, it has good reasons to do so.
I actually wrote one, with the bare minimum. HNActivity.kt is 81 lines and handles updating the list, querying
import android.database.sqlite.SQLiteDatabase
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.widget.addTextChangedListener
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlin.concurrent.thread
class ListItemVH(val view: TextView) : RecyclerView.ViewHolder(view)
class HNActivity : AppCompatActivity() {
private lateinit var recycler: RecyclerView
private val adapter = object: RecyclerView.Adapter<ListItemVH>() {
private var _items = listOf<String>()
fun setItems(newItems: List<String>) {
DiffUtil.calculateDiff(object: DiffUtil.Callback() {
override fun getOldListSize(): Int = _items.size
override fun getNewListSize(): Int = newItems.size
override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int) =
_items[oldItemPosition] == newItems[newItemPosition]
override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int) =
_items[oldItemPosition] == newItems[newItemPosition]
}, true)
_items = newItems
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ListItemVH {
val v = TextView(parent.context)
return ListItemVH(v)
}
override fun getItemCount(): Int = _items.size
override fun onBindViewHolder(holder: ListItemVH, position: Int) {
holder.view.text = _items[position]
}
}
private var search: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_scrolling)
recycler = findViewById<RecyclerView>(R.id.fab)
recycler.adapter = adapter
recycler.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
findViewById<EditText>(R.id.toolbar).addTextChangedListener {
search = it?.toString()?.takeIf { str -> str != null }
}
}
private fun performSearch() {
thread {
val db = SQLiteDatabase.openOrCreateDatabase("items.sqlite", null)
val query = when (search) {
null -> "SELECT text FROM table"
else -> "SELECT text FROM table WHERE text = ?%"
}
val args = listOfNotNull(search).toTypedArray()
val res = db.rawQuery(query, args)
val text = mutableListOf<String>()
while (!res.isAfterLast) {
text += res.getString(0)
}
adapter.setItems(text)
res.close()
db.close()
}
}
override fun onResume() {
super.onResume()
performSearch()
}
}
Add in another 30 lines for the Activity layout, and you've got your thing.
You've also got every single developer you'll work with hating you, and you'll hate maintaining this code. Just in those few lines, so much can go wrong (thread gets started and activity gets backgrounded? Oops, you crashed.)I precached but it still required internet access to get some maven deps for android build. Updating is full of hassles and gradle, as always, unexpectedly fails somewhere eg in "assembleDebug" step. Maybe some version mismatch and I am not in a situation to do clean installs that require GiBs of internet. And no guarantee it works even on clean install.
I have come to a conclusion that front end (web or app development) is a mess and it is not for me. Look at Go, or Rust to know how to keep tooling simple and straightforward.