Rifat15913 / boiler-plate-android

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Table of contents

1. Tree

Tree is a native application which contains all the essential codes (boiler-plate) to kick start an Android project.

2. Built with

2.1 Languages

  1. Kotlin
  2. Java
  3. XML
  4. MarkDown

2.2 Architecture

  1. MVP (Model View Presenter)

![Visual](readme-images/mvp.png)

3. How to configure

3.1 Configure base project

3.1.1 Change application id

No Action Screenshot
1 Go to build.gradle(app) file and copy the applicationId Visual
2 Go to Title Bar Menu > Edit > Find > Replace in Path Visual
3 Paste the old application id in the first field and write down the new application id in the second field. Then press Replace All Visual
4 If alert dialog pops up, then click on Replace Visual
5 Wait -
6 Go to Build > Clean Project -
7 Sync gradle -

3.1.2 Change package name

No Action Screenshot
1 Open the project, Select the Project type as file structure(top left) Visual
2 Right click and go to Refactor > Rename Visual
3 Put new app name Visual
4 Click on Do Refactor if the dialog pops up Visual
5 Wait Visual

3.1.3 Change application launcher icon

No Action Screenshot
1 Open the project, Select the Android type as file structure(top left). Then right click on the res folder. Visual
2 Go to New > Image Asset Visual
3 There are two main tabs here, Foreground Layer & Background Layer. We can select assets for foreground and background from these tabs. Visual
4 (Optional) Browse images (vector is also acceptable) clicking on the folder icon Visual
5 Choose the asset Visual
6 Show Grid can be ticked in order to see the boundaries. Other settings can also be modified. Visual
7 Asset can be picked for background, in the Background Layer tab. Click on next if it seems okay. Visual
8 Finish the dialog and your launcher icon is changed now Visual

3.1.4 Change application name

No Action Screenshot
1 Change app_name from string.xml of all the languages Visual

3.1.5 Change font

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. Visual

3.2 Configure firebase core

  1. Visit here and complete step 1-3
  2. Activate at build.gradle
    • implementation 'com.google.firebase:firebase-core:16.0.8'

3.3 Configure push notification

  1. Configure firebase core
  2. Uncomment at build.gradle file
    • implementation 'com.google.firebase:firebase-messaging:17.6.0'
    • apply plugin: 'com.google.gms.google-services'
  3. Uncomment at NotificationService.kt
    • All the commented lines
  4. Uncomment at AndroidManifest.xml
    • com.lusosmile.main.data.remote.service.NotificationService
  5. To know about getting the device registration token, visit here

3.4 Configure crashlytics

  1. Configure firebase core
  2. Configure crashlytics at firebase console. To know more, visit here
  3. Uncomment at build.gradle
    • apply plugin: 'io.fabric'
    • implementation 'com.crashlytics.sdk.android:crashlytics:2.9.9'
    • apply plugin: 'com.google.gms.google-services'
  4. Uncomment at BaseApplication.kt
    • import com.google.firebase.analytics.FirebaseAnalytics
    • FirebaseAnalytics.getInstance(context)

4. Guideline

4.1 File naming guideline

4.1.1 Class files

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.

4.1.2 Resources files

Resources file names are written in lowercase_underscore.

4.1.2.1 Drawable files

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

4.1.2.2 Layout files

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_.

4.1.2.3 Menu files

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.

4.1.2.4 Values files

Resource files in the values folder should be plural, e.g. strings.xml, styles.xml, colors.xml, dimens.xml, attrs.xml

4.2 Code guideline

4.2.1 Java language rules

4.2.1.1 Don't ignore exceptions

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.

4.2.1.2 Don't catch generic exception

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

4.2.1.3 Fully qualify imports

This is bad: import foo.*;

This is good: import foo.Bar;

See more info here

4.2.2 Java style rules

4.2.2.1 Fields definition and naming

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;
}

4.2.2.2 Treat acronyms as words

Good Bad
XmlHttpRequest XMLHTTPRequest
getCustomerId getCustomerID
String url String URL
long id long ID

4.2.2.3 Use spaces for indentation

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);
    }
}

4.2.2.4 Use standard brace style

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!
    }
}

4.2.2.5 Limit variable scope

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)

4.2.2.6 Order import statements

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:

  1. Android imports
  2. Imports from third parties (com, junit, net, org)
  3. java and javax
  4. 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

4.2.2.7 Logging guidelines

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)

4.2.2.8 Class member ordering

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:

  1. Constants
  2. Fields
  3. Constructors
  4. Override methods and callbacks (public or private)
  5. Public methods
  6. Private methods
  7. 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() {}
}

4.2.2.9 Parameter ordering in methods

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);
}

4.2.2.10 String constants, naming, and values

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.

4.2.2.11 Line length limit

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 and import statements.
4.2.2.11.1 Line-wrapping strategies

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");
    }
}

4.2.2.12 RxJava chains styling

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;
                     }
                });
    }
}

4.2.3 XML style rules

4.2.3.1 Use self closing tags

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>

4.2.3.2 Resources naming

Resource IDs and names are written in lowercase_underscore.

4.2.3.2.1 ID naming

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>
4.2.3.2.2 Strings

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"
4.2.3.2.3 Styles and Themes

Unlike the rest of resources, style names are written in UpperCamelCase.

4.2.3.3 Attributes ordering

As a general rule you should try to group similar attributes together. A good way of ordering the most common attributes is:

  1. View Id
  2. Style
  3. Layout width and layout height
  4. Other layout attributes, sorted alphabetically
  5. Remaining attributes, sorted alphabetically

5. Extras

5.1 Fetch signing fingerprint

5.1.1 Release

  1. Add the path of keytool from JDK to System Variable PATH
  2. Go to keystore folder
  3. Press Shift + Right Click
  4. Start command prompt / PowerShell
  5. Put command keytool -list -v -alias ALIAS_NAME -keystore KEYSTORE_NAME_WITH_EXTENSION

5.1.2 Debug (1)

  1. Go to right side of Android Studio.
  2. Execute Gradle > root > Tasks > android > signingReport

5.1.3 Debug (2)

  1. Add the path of keytool from JDK to System Variable PATH
  2. Go to keystore folder
  3. Press Shift + Right Click
  4. Start command prompt / PowerShell
  5. Put command keytool -list -v -keystore "debug.keystore" -alias androiddebugkey -storepass android -keypass android

5.2 Fetch android hash key

  1. You can get SHA-1 depending on build variant and put it here to get the key hash. Or follow the below,
  2. Uncomment at BaseApplication.kt
    • import com.boilerplate.utils.helper.DataUtils
    • DataUtils.getAndroidHashKey() at onCreate()
  3. Connect a device via adb and install the application into it
  4. Open the application at device
  5. Open Logcat from Android Studio
  6. Filter the Info logs
  7. Search for the tag Hash
  8. Get the hash key depending on the build variant; i.e: debug, release

5.3 Show application local data

  1. Uncomment at build.gradle
    • debugImplementation 'com.awesomedroidapps:inappstoragereader:1.0.2'
  2. Visit App Data from your device. It will have similar icon as the app launcher.

6. Authors

Mohd. Asfaq-E-Azam Rifat, Executive Software Engineer - Rifat

7. Technical documentation

The technical documentation is located here.

8. Releases

Please visit this link to get the latest build.

9. Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

About


Languages

Language:Kotlin 100.0%