|
1 | | -## File Structures |
2 | | -* `/base` module: |
3 | | - * Responsibility: define the base, common and utilities classes. |
4 | | - * It can be updated by merging base repository, and will not affect the project modules. |
5 | | -* `/app` module: |
6 | | - * Responsibility: define all the features in your app. |
7 | | - * Define `applicationId` as app package name. |
8 | | - * Include all resources that app used. (including strings, colors, dimensions, drawables.) |
9 | | - |
10 | | -## Dependencies |
11 | | -* `/app` include `/base` serves as common library that the whole project used. |
12 | | - |
13 | | -## Don't do |
14 | | -Don't add anything to the module other than `/base`, since other modules will change the package to feature, and it will break the merge. |
| 1 | +  |
| 2 | + |
| 3 | +The `Base` project provides a Android app project template that includes the base modules/classes, setups for **Gradle Kotlin DSL** and eliminates boilerplate code. |
| 4 | + |
| 5 | +It helps you to quickly create a well configured Android starter application with the most popular libraries (Ex: Android Architecuture Component, Retrofit/OkHttp, RxJava, Logging...etc.). It creates and configures your project for you. Just focus on code development! |
| 6 | + |
| 7 | +## Setup |
| 8 | +1. Just click on [](https://github.com/enginebai/Base/generate) button to create a new repo starting from this template. Or you can clone this project by `git clone git@github.com:enginebai/Base.git` . |
| 9 | +2. Set your application ID in `Versions.kt` |
| 10 | +3. Set the package name in `AndroidManifest.xml` file of `:app` module . |
| 11 | +4. Select `com.enginebai.project` directory in "Project" tool window and rename package for your app. |
| 12 | +5. Create your application class which extends `BaseApplication` in `:app` module, implement abstract methods and add to `AndroidManifest.xml` file. |
| 13 | +6. Specify your retrofit base URL in `NetworkModule.kt` file. |
| 14 | +7. Start to design your main layout xml file `fragment_main.xml` and fragment class. |
| 15 | +8. That's all. Start your app development journey now 🎉. |
| 16 | + |
| 17 | +## Good Practices |
| 18 | +* Add all dependencies version in `Versions.kt` |
| 19 | + |
| 20 | +```kotlin |
| 21 | +object Versions { |
| 22 | + const val kotlin = "1.3.50" |
| 23 | + const val awesomeLibrary = "x.y.z" |
| 24 | + ... |
| 25 | +} |
| 26 | +``` |
| 27 | +* Add all 3rd-party dependencies in `Dependencies.kt` |
| 28 | + |
| 29 | +```kotlin |
| 30 | +object Dependencies { |
| 31 | + const val rxJava = "io.reactivex.rxjava2:rxjava:${Versions.rxJava}" |
| 32 | + // TODO: add standalone dependency here! |
| 33 | + ... |
| 34 | + |
| 35 | + object Kotlin { |
| 36 | + const val gradlePlugin = "org.jetbrains.kotlin:kotlin-gradle-plugin:${Versions.kotlin}" |
| 37 | + const val stdLib = "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${Versions.kotlin}" |
| 38 | + } |
| 39 | + |
| 40 | + // TODO: add inner object for sub-modules of library |
| 41 | + object AndroidX { |
| 42 | + ... |
| 43 | + } |
| 44 | + ... |
| 45 | +} |
| 46 | +``` |
| 47 | +* Always import dependency from `Dependencies.kt` in `build.gradle.kts` file. |
| 48 | + |
| 49 | +```kotlin |
| 50 | +dependencies { |
| 51 | + implementation(Dependencies.Glide.core) |
| 52 | + "kapt"(Dependencies.Glide.compiler) |
| 53 | + implementation(project(":base")) |
| 54 | + // TODO: add by using dependency imported from `Dependencies.kt` file |
| 55 | + ... |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +* Configure android build script in `Config.kt`. |
| 60 | + |
| 61 | +```kotlin |
| 62 | +fun Project.configAndroid() = this.extensions.getByType<BaseExtension>().run { |
| 63 | + compileSdkVersion(Versions.Android.sdk) |
| 64 | + defaultConfig { |
| 65 | + minSdkVersion(Versions.Android.minSdk) |
| 66 | + targetSdkVersion(Versions.Android.sdk) |
| 67 | + versionCode = Versions.App.versionCode |
| 68 | + versionName = Versions.App.versionName |
| 69 | + // TODO: add your configurations |
| 70 | + ... |
| 71 | + } |
| 72 | + ... |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +It's equalivent to the old way `android { ... }` block in `build.gradle` file |
| 77 | +```groovy |
| 78 | +android { |
| 79 | + compileSdkVersion 21 |
| 80 | + buildToolsVersion "21.1.2" |
| 81 | + defaultConfig { |
| 82 | + applicationId "com.enginebai.moviehunt" |
| 83 | + // TODO: add your configurations |
| 84 | + ... |
| 85 | + } |
| 86 | + ... |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | + |
| 91 | +* Add all configuration variables inside `Config` object in `Config.kt`, and add `buildConfigField(...)` to include. |
| 92 | + |
| 93 | +```kotlin |
| 94 | +object Config { |
| 95 | + const val API_ROOT = "\"https://api.themoviedb.org/3/\"" |
| 96 | + const val IMAGE_API_ROOT = "\"https://image.tmdb.org/t/p/\"" |
| 97 | + // TODO: add your constants here, make sure to add extra double quotes for string value. |
| 98 | +} |
| 99 | + |
| 100 | +fun Project.configAndroid() = this.extensions.getByType<BaseExtension>().run { |
| 101 | + compileSdkVersion(Versions.Android.sdk) |
| 102 | + defaultConfig { |
| 103 | + ... |
| 104 | + |
| 105 | + buildConfigField("String", "API_ROOT", Config.API_ROOT) |
| 106 | + buildConfigField("String", "IMAGE_API_KEY", Config.IMAGE_API_ROOT) |
| 107 | + // TODO: add your varialbes here imported from `Config` object |
| 108 | + ... |
| 109 | + } |
| 110 | + ... |
| 111 | +} |
| 112 | +``` |
| 113 | + |
| 114 | +* Add the common dependencies that share between modules to `Dependencies.kt` |
| 115 | + |
| 116 | +```kotlin |
| 117 | +fun Project.importCommonDependencies() { |
| 118 | + dependencies { |
| 119 | + ... |
| 120 | + implementation(Dependencies.material) |
| 121 | + // TODO: add your dependencies |
| 122 | + .. |
| 123 | + } |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +> **Note:** Remember to perform Gradle Sync to apply your changes when updating any files in `buildSrc`. |
| 128 | +
|
| 129 | +## Modules Structure |
| 130 | +* `:base` module: It defines the base, common and utilities classes. |
| 131 | +* `:app` module: That's your app module, just like a normal Android app project. You puts all resources that app used, including strings, colors, dimensions, drawables. Or you can create `:common` modules for that if you use multi-modules project. |
| 132 | + |
| 133 | +> **Note**: Don't put the resources inside `:base` module since it can be updated from remote repo, please treat `:base` module as library. |
| 134 | +
|
| 135 | +## Included Libraries |
| 136 | +* [Android Architecture Components](https://developer.android.com/topic/libraries/architecture), part of Android Jetpack for give to project a robust design, testable and maintainable. |
| 137 | +* [Retrofit](https://github.com/square/retrofit) / [OkHttp](https://github.com/square/okhttp), Square open-source RESTful API and http client. |
| 138 | +* [RxJava](https://github.com/ReactiveX/RxJava/) / [RxAndroid](https://github.com/ReactiveX/RxAndroid), reactive programming for JVM. |
| 139 | +* [Koin](https://github.com/InsertKoinIO/koin), kotlin light-weight dependency injection. |
| 140 | +* [Timber](https://github.com/JakeWharton/timber), for logging. |
| 141 | +* [Epoxy](https://github.com/airbnb/epoxy), for RecyclerView complex view layout. |
| 142 | + |
| 143 | +## LICENSE |
| 144 | + |
| 145 | +``` |
| 146 | +Copyright (c) 2020 Engine Bai |
| 147 | +
|
| 148 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 149 | +of this software and associated documentation files (the "Software"), to deal |
| 150 | +in the Software without restriction, including without limitation the rights |
| 151 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 152 | +copies of the Software, and to permit persons to whom the Software is |
| 153 | +furnished to do so, subject to the following conditions: |
| 154 | +
|
| 155 | +The above copyright notice and this permission notice shall be included in all |
| 156 | +copies or substantial portions of the Software. |
| 157 | +
|
| 158 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 159 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 160 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 161 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 162 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 163 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 164 | +SOFTWARE. |
| 165 | +``` |
| 166 | + |
15 | 167 |
|
0 commit comments