- 1. Tree
- 2. Built with
- 3. How to configure
- 4. Guideline
- 5. Extras
- 6. Authors
- 7. Technical documentation
- 8. Releases
- 9. Contributing
Tree is a native application which contains all the essential codes (boiler-plate) to kick start an Android project.
Kotlin
Java
XML
MarkDown
MVP (Model View Presenter)
![Visual](readme-images/mvp.png)
No | Action | Screenshot |
---|---|---|
1 | Change app_name from string.xml of all the languages |
No | Action | Screenshot |
---|---|---|
1 | Overwrite regular.ttf from font directory in order to achieve text changes globally in the application of regular fonts. Additional fonts can be added; i.e. bold , italic etc. |
- Visit here and complete step 1-3
- Activate at
build.gradle
implementation 'com.google.firebase:firebase-core:16.0.8'
- Configure firebase core
- Uncomment at
build.gradle
fileimplementation 'com.google.firebase:firebase-messaging:17.6.0'
apply plugin: 'com.google.gms.google-services'
- Uncomment at
NotificationService.kt
- All the commented lines
- Uncomment at
AndroidManifest.xml
com.lusosmile.main.data.remote.service.NotificationService
- To know about getting the device registration token, visit here
- Configure firebase core
- Configure crashlytics at firebase console. To know more, visit here
- Uncomment at
build.gradle
apply plugin: 'io.fabric'
implementation 'com.crashlytics.sdk.android:crashlytics:2.9.9'
apply plugin: 'com.google.gms.google-services'
- Uncomment at
BaseApplication.kt
import com.google.firebase.analytics.FirebaseAnalytics
FirebaseAnalytics.getInstance(context)
Class names are written in UpperCamelCase. For classes that extend an Android component, the name of the class should end with the name of the component; for example: SignInActivity
, SignInFragment
, ImageUploaderService
, ChangePasswordDialog
.
Resources file names are written in lowercase_underscore.
Naming conventions for drawables:
Asset Type | Prefix | Example |
---|---|---|
Action bar | ab_ |
ab_stacked.png |
Button | btn_ |
btn_send_pressed.png |
Dialog | dialog_ |
dialog_top.png |
Divider | divider_ |
divider_horizontal.png |
Icon | ic_ |
ic_star.png |
Menu | menu_ |
menu_submenu_bg.png |
Notification | notification_ |
notification_bg.png |
Tabs | tab_ |
tab_pressed.png |
Naming conventions for icons (taken from Android iconography guidelines):
Asset Type | Prefix | Example |
---|---|---|
Icons | ic_ |
ic_star.png |
Launcher icons | ic_launcher |
ic_launcher_calendar.png |
Menu icons and Action Bar icons | ic_menu |
ic_menu_archive.png |
Status bar icons | ic_stat_notify |
ic_stat_notify_msg.png |
Tab icons | ic_tab |
ic_tab_recent.png |
Dialog icons | ic_dialog |
ic_dialog_info.png |
Naming conventions for selector states:
State | Suffix | Example |
---|---|---|
Normal | _normal |
btn_order_normal.png |
Pressed | _pressed |
btn_order_pressed.png |
Focused | _focused |
btn_order_focused.png |
Disabled | _disabled |
btn_order_disabled.png |
Selected | _selected |
btn_order_selected.png |
Layout files should match the name of the Android components that they are intended for but moving the top level component name to the beginning. For example, if we are creating a layout for the SignInActivity
, the name of the layout file should be activity_sign_in.xml
.
Component | Class Name | Layout Name |
---|---|---|
Activity | UserProfileActivity |
activity_user_profile.xml |
Fragment | SignUpFragment |
fragment_sign_up.xml |
Dialog | ChangePasswordDialog |
dialog_change_password.xml |
AdapterView item | --- | item_person.xml |
Partial layout | --- | partial_stats_bar.xml |
A slightly different case is when we are creating a layout that is going to be inflated by an Adapter
, e.g to populate a RecyclerView
. In this case, the name of the layout should start with item_
.
Note that there are cases where these rules will not be possible to apply. For example, when creating layout files that are intended to be part of other layouts. In this case you should use the prefix partial_
.
Similar to layout files, menu files should match the name of the component. For example, if we are defining a menu file that is going to be used in the UserActivity
, then the name of the file should be activity_user.xml
. A good practice is to not include the word menu
as part of the name because these files are already located in the menu
directory.
Resource files in the values folder should be plural, e.g. strings.xml
, styles.xml
, colors.xml
, dimens.xml
, attrs.xml
You must never do the following:
class ServerUtils {
void setServerPort(String value) {
try {
serverPort = Integer.parseInt(value);
} catch (NumberFormatException e) {
// Handle error
}
}
}
While you may think that your code will never encounter this error condition or that it is not important to handle it, ignoring exceptions like above creates mines in your code for someone else to trip over some day. You must handle every Exception in your code in some principled way. The specific handling varies depending on the case. - (Android code style guidelines)
See alternatives here.
You should not do this:
class Driver {
void test() {
try {
someComplicatedIOFunction(); // may throw IOException
someComplicatedParsingFunction(); // may throw ParsingException
someComplicatedSecurityFunction(); // may throw SecurityException
// phew, made it all the way
} catch (Exception e) { // I'll just catch all exceptions
handleError(); // with one generic handler!
}
}
}
See the reason why and some alternatives here
This is bad: import foo.*;
This is good: import foo.Bar;
See more info here
Fields should be defined at the top of the file and they should follow the naming rules listed below.
- Private, non-static field names start with m.
- Private, static field names start with s.
- Other fields start with a lower case letter.
- Static final fields (constants) are ALL_CAPS_WITH_UNDERSCORES.
Example:
public class MyClass {
public static final int SOME_CONSTANT = 42;
public int publicField;
private static MyClass sSingleton;
int mPackagePrivate;
private int mPrivate;
protected int mProtected;
}
Good | Bad |
---|---|
XmlHttpRequest |
XMLHTTPRequest |
getCustomerId |
getCustomerID |
String url |
String URL |
long id |
long ID |
Use 4 space indents for blocks:
class Condition {
void test() {
if (x == 1) {
x++;
}
}
}
Use 8 space indents for line wraps:
class LongExpression {
void test() {
Instrument i =
someLongExpression(that, wouldNotFit, on, one, line);
}
}
Braces go on the same line as the code before them.
class MyClass {
int func() {
if (something) {
// ...
} else if (somethingElse) {
// ...
} else {
// ...
}
}
}
Braces around the statements are required unless the condition and the body fit on one line.
If the condition and the body fit on one line and that line is shorter than the max line length, then braces are not required, e.g.
class Condition {
void test() {
if (condition) body();
}
}
This is bad:
class Condition {
void test() {
if (condition)
body(); // bad!
}
}
The scope of local variables should be kept to a minimum (Effective Java Item 29). By doing so, you increase the readability and maintainability of your code and reduce the likelihood of error. Each variable should be declared in the innermost block that encloses all uses of the variable.
Local variables should be declared at the point they are first used. Nearly every local variable declaration should contain an initializer. If you don't yet have enough information to initialize a variable sensibly, you should postpone the declaration until you do. - (Android code style guidelines)
If you are using an IDE such as Android Studio, you don't have to worry about this because your IDE is already obeying these rules. If not, have a look below.
The ordering of import statements is:
- Android imports
- Imports from third parties (com, junit, net, org)
- java and javax
- Same project imports
To exactly match the IDE settings, the imports should be:
- Alphabetically ordered within each grouping, with capital letters before lower case letters (e.g. Z before a).
- There should be a blank line between each major grouping (android, com, junit, net, org, java, javax).
More info here
Use the logging methods provided by the Timber
class to print out error messages or other information that may be useful for developers to identify issues:
Timber.v(String message)
(verbose)Timber.d(String message)
(debug)Timber.i(String message)
(information)Timber.w(String message)
(warning)Timber.e(Throwable error)
(error)
There is no single correct solution for this but using a logical and consistent order will improve code learning ability and readability. It is recommendable to use the following order:
- Constants
- Fields
- Constructors
- Override methods and callbacks (public or private)
- Public methods
- Private methods
- Inner classes or interfaces
Example:
public class MainActivity extends Activity {
private String mTitle;
private TextView mTextViewTitle;
public void setTitle(String title) {
mTitle = title;
}
@Override
public void onCreate() {
// ...
}
private void setUpView() {
// ...
}
static class AnInnerClass {
}
}
If your class is extending an Android component such as an Activity or a Fragment, it is a good practice to order the override methods so that they match the component's lifecycle. For example, if you have an Activity that implements onCreate()
, onDestroy()
, onPause()
and onResume()
, then the correct order is:
public class MainActivity extends Activity {
//Order matches Activity lifecycle
@Override
public void onCreate() {}
@Override
public void onResume() {}
@Override
public void onPause() {}
@Override
public void onDestroy() {}
}
When programming for Android, it is quite common to define methods that take a Context
. If you are writing a method like this, then the Context must be the first parameter.
The opposite case are callback interfaces that should always be the last parameter.
Examples:
interface Callback {
// Context always goes first
User loadUser(Context context, int userId);
// Callbacks always go last
void loadUserAsync(Context context, int userId, UserCallback callback);
}
Many elements of the Android SDK such as SharedPreferences
, Bundle
, or Intent
use a key-value pair approach so it's very likely that even for a small app you end up having to write a lot of String constants.
While using one of these components, you must define the keys as a static final
fields.
Code lines should not exceed 80 characters. If the line is longer than this limit there are usually two options to reduce its length:
- Extract a local variable or method (preferable).
- Apply line-wrapping to divide a single line into multiple ones.
There are two exceptions where it is possible to have lines longer than 80:
- Lines that are not possible to split, e.g. long URLs in comments.
package
andimport
statements.
There isn't an exact formula that explains how to line-wrap and quite often different solutions are valid. However there are a few rules that can be applied to common cases.
Break at operators
When the line is broken at an operator, the break comes before the operator. For example:
class LineWrapper {
int longName = anotherVeryLongVariable + anEvenLongerOne - thisRidiculousLongOne
+ theFinalOne;
}
Assignment Operator Exception
An exception to the break at operators
rule is the assignment operator =
, where the line break should happen after the operator.
class LineWrapper {
int longName =
anotherVeryLongVariable + anEvenLongerOne - thisRidiculousLongOne + theFinalOne;
}
Method chain case
When multiple methods are chained in the same line - for example when using Builders - every call to a method should go in its own line, breaking the line before the .
class MethodChain {
void test() {
Picasso.with(context).load("http://ribot.co.uk/images/sexyjoe.jpg").into(imageView);
}
}
class MethodChain {
void test() {
Picasso.with(context)
.load("http://ribot.co.uk/images/sexyjoe.jpg")
.into(imageView);
}
}
Long parameters case
When a method has many parameters or its parameters are very long, we should break the line after every comma ,
class LongParameter {
void test() {
loadPicture(context, "http://ribot.co.uk/images/sexyjoe.jpg", mImageViewProfilePicture, clickListener, "Title of the picture");
}
}
class LongParameter {
void test() {
loadPicture(context,
"http://ribot.co.uk/images/sexyjoe.jpg",
mImageViewProfilePicture,
clickListener,
"Title of the picture");
}
}
Rx chains of operators require line-wrapping. Every operator must go in a new line and the line should be broken before the .
class RxJavaExample {
public Observable<Location> syncLocations() {
return mDatabaseHelper.getAllLocations()
.concatMap(new Func1<Location, Observable<? extends Location>>() {
@Override
public Observable<? extends Location> call(Location location) {
return mRetrofitService.getLocation(location.id);
}
})
.retry(new Func2<Integer, Throwable, Boolean>() {
@Override
public Boolean call(Integer numRetries, Throwable throwable) {
return false;
}
});
}
}
When an XML element doesn't have any contents, you must use self closing tags.
This is good:
<TextView
android:id="@+id/text_view_profile"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
This is bad :
<!-- Don't do this! -->
<TextView
android:id="@+id/text_view_profile"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TextView>
Resource IDs and names are written in lowercase_underscore.
IDs should be prefixed with the name of the element in lowercase underscore. For example:
Element | Prefix |
---|---|
TextView |
text_view |
ImageView |
image_view |
Button |
button_ |
Menu |
menu_ |
Image view example:
<ImageView
android:id="@+id/image_view_profile"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Menu example:
<menu>
<item
android:id="@+id/menu_done"
android:title="Done" />
</menu>
String names start with a prefix that identifies the section they belong to. For example registration_email_hint
or registration_name_hint
. If a string doesn't belong to any section, then you should follow the rules below:
Prefix | Description |
---|---|
error_ |
An error message |
msg_ |
A regular information message |
title_ |
A title, i.e. a dialog title |
action_ |
An action such as "Save" or "Create" |
Unlike the rest of resources, style names are written in UpperCamelCase.
As a general rule you should try to group similar attributes together. A good way of ordering the most common attributes is:
- View Id
- Style
- Layout width and layout height
- Other layout attributes, sorted alphabetically
- Remaining attributes, sorted alphabetically
- Add the path of
keytool
fromJDK
to System VariablePATH
- Go to
keystore
folder - Press
Shift + Right
Click - Start command prompt / PowerShell
- Put command
keytool -list -v -alias ALIAS_NAME -keystore KEYSTORE_NAME_WITH_EXTENSION
- Go to right side of Android Studio.
- Execute
Gradle > root > Tasks > android > signingReport
- Add the path of
keytool
fromJDK
to System VariablePATH
- Go to
keystore
folder - Press
Shift + Right
Click - Start command prompt / PowerShell
- Put command
keytool -list -v -keystore "debug.keystore" -alias androiddebugkey -storepass android -keypass android
- You can get SHA-1 depending on build variant and put it here to get the key hash. Or follow the below,
- Uncomment at
BaseApplication.kt
import com.boilerplate.utils.helper.DataUtils
DataUtils.getAndroidHashKey()
atonCreate()
- Connect a device via
adb
and install the application into it - Open the application at device
- Open Logcat from Android Studio
- Filter the
Info
logs - Search for the tag
Hash
- Get the hash key depending on the build variant; i.e:
debug, release
- Uncomment at
build.gradle
debugImplementation 'com.awesomedroidapps:inappstoragereader:1.0.2'
- Visit
App Data
from your device. It will have similar icon as the app launcher.
Mohd. Asfaq-E-Azam Rifat, Executive Software Engineer - Rifat
The technical documentation is located here.
Please visit this link to get the latest build.
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.