8

Currently I'm developing my first app, and I'm noticing certain workflow patterns that are emerging. Specifically, android apps follow a kind of tree-like user activity flow, where every user action usually either proceeds down into a deeper branch of the tree, or goes back up towards the root.

So the development pattern that appears to emerge from this is: add actionable widgets -> add listener methods for them -> which send intents to start new activities & pass data to them -> construct the new activity layouts and classes to receive this data -> repeat.

Then in certain activities there will be widgets that show database data (so you have to set up CursorAdapters etc), and some that modify database data (so you'll have to implement update/add/delete methods etc).

My question is: is there some way to automate this workflow, or to otherwise make it more efficient? I.e. is there some kind of schema-based scripting somewhere where you draw out the tree-structure of your activities, and what items will need CursorAdapters etc, and then it writes out skeleton classes for that schema? It seems that the coding 'dog work', so to speak, such as hammering out the skeleton aspects of the application, take up a large amount of typing work versus the meaty functionality.

I guess what I'm looking for is insight into how the power coders or 'elite' normally write android apps.

Cheers

2 Answers2

8

I'm not aware of a schema-based scripting language that will create project skeletons for you, but something similar might be possible with IntelliJ's UML to Java code generation (I've never tried this).


However, this is how I typically cut down on boiler plate code in Android Projects:

  1. Android Studio will do a lot of work for you. For example, creating a new "Blank Activity with Fragment" will create the activity, fragment, and xml layouts for you with basic boiler plate.

  2. In addition to the built in templates, you can create your own templates in Android Studio using "Tools>>Save File As Template". Also checkout "Code>>Generate" for your POJOs.

  3. Create Fragments and Views that can be reused by multiple activities, orientations, and screen sizes.

  4. Use 3rd party libraries to reduce some of the boiler plate and get around some inefficient paradigms in Android -- specifically:

    • ButterKnife, Icepick, Parceler, Auto, and AndroidAnnotations will generate code for you using annotations
    • EventBus and RxJava are good for cutting down on code for getting and passing data/events between threads.
    • Retrofit and OkHttp will reduce code if you are trying to connect to a Rest API.
    • GreenDao (or many others e.g. OrmLite) will help with database by handling basic CRUD methods and giving you ORM capabilities.
    • Picasso greatly reduces code for loading and caching of images from any URI (local or remote)

In general, just be careful with the annotation code generators. Make sure they generate code at compile time and not run time (reflection), or that the reflection does not cause serious problems. For a larger list of good libraries see:

https://github.com/codepath/android_guides/wiki/Must-Have-Libraries

bcorso
  • 45,608
  • 10
  • 63
  • 75
1

As you said, many apps in Android can share the same software architecture and code organization.

I'm not aware of some code generators that can generate Android Studio templates based on configurable preferences, but I've created a tutorial/template Android Studio project that you can clone/fork and build your app on top of it.

The architectural pattern employed in this template is MVC - a widely accepted architectural pattern for building a "user-facing" software, which was overlooked by AOSP core architects (IMHO). Building your apps on top of a single template using single architectural pattern has a huge advantage - once accustomed to the template, you'll be much more efficient in writing and debugging your apps.

In addition, the code organization employed in this template separates between the actual business logic (which you called "meaty functionality") and the "boring" UI handling stuff.

You can find the tutorial/template MVC project here: https://github.com/techyourchance/android_mvc_template

Vasiliy
  • 16,221
  • 11
  • 71
  • 127