|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "Jumpstarting Android Projects" |
| 4 | +date: 2017-08-30 08:00:00 |
| 5 | +author: anuj |
| 6 | +categories: Technical Android |
| 7 | +--- |
| 8 | + |
| 9 | +Starting a new Android project means writing a lot of boilerplate code, setting up your dependencies, setting up networking, dependency injection and a myriad of other small things before you can even get started on the actual app. In the spirit of DRY, we have created an automated process at moldedbits to jumpstart Android projects and get started quickly. And in the spirit of open source, we are putting this out in the public domain. |
| 10 | + |
| 11 | +In this post, we will demo setting up an application with the dependencies, networking and dependency injection setup and the login implemented with [Argus](https://github.com/moldedbits/argus-android). |
| 12 | + |
| 13 | +#### Setting up |
| 14 | + |
| 15 | +We have a python script to help with the initial setup. You can clone this [repo](https://github.com/moldedbits/JumpstartScript) on your machine and navigate to the folder on a terminal. |
| 16 | + |
| 17 | +Next, to setup the project, all you need to do is run |
| 18 | + |
| 19 | +``` |
| 20 | +$ python android-jumpstart.py |
| 21 | +``` |
| 22 | +This will clone the repository [android-jumpstart](https://github.com/moldedbits/android-jumpstart) locally and then ask you a few questions to customize it. |
| 23 | + |
| 24 | +``` |
| 25 | +... |
| 26 | +Enter new app name: JumpDemo |
| 27 | +... |
| 28 | +Enter new app package: com.moldedbits.jump |
| 29 | +... |
| 30 | +``` |
| 31 | +This will create a project for you in the folder _JumpDemo_ that is all set to be imported into Android Studio. |
| 32 | + |
| 33 | +``` |
| 34 | +$ cd JumpDemo |
| 35 | +$ ls |
| 36 | +README.md buildSrc gradle.properties jumpstart.keystore |
| 37 | +app config gradlew settings.gradle |
| 38 | +build.gradle gradle gradlew.bat version.properties |
| 39 | +``` |
| 40 | +With this we can import the project into Android Studio |
| 41 | + |
| 42 | +![Android Studio][studio-screenshot] |
| 43 | + |
| 44 | +This is our base project all setup. Some of the features already implemented by this are, |
| 45 | + |
| 46 | +* Dependency injection with Dagger 2 |
| 47 | +* Networking with Retrofit |
| 48 | +* Gradle build types, signing keystore, dependencies, version code auto increment |
| 49 | +* Mocking framework for unit tests |
| 50 | + |
| 51 | +A detailed and updated list of the features is available on the [project page](https://github.com/moldedbits/android-jumpstart). |
| 52 | + |
| 53 | +#### Adding login |
| 54 | + |
| 55 | +Most apps require a login flow with email, phone and social logins. To further automate this, we have created another library to make our lives easier, [Argus](https://github.com/moldedbits/argus-android). Including this is really simple. The first thing we have to do is to include Argus as a dependency in our app level build.gradle. |
| 56 | + |
| 57 | +``` |
| 58 | +compile 'com.moldedbits.argus:argus:0.2.0' |
| 59 | +``` |
| 60 | +Next, we modify the class BaseApplication to configure Argus. |
| 61 | + |
| 62 | +```java |
| 63 | +// initialize Argus |
| 64 | +ArrayList<BaseProvider> loginProviders = new ArrayList<>(); |
| 65 | + |
| 66 | +loginProviders.add(new EmailLoginProvider() { |
| 67 | + @Override |
| 68 | + protected void doServerLogin(String username, String password) { |
| 69 | + |
| 70 | + } |
| 71 | +}); |
| 72 | + |
| 73 | +ArgusTheme.Builder themeBuilder = new ArgusTheme.Builder(); |
| 74 | +themeBuilder.logo(R.mipmap.ic_launcher); |
| 75 | + |
| 76 | +new Argus.Builder() |
| 77 | + .argusStorage(new DefaultArgusStorage(getApplicationContext())) |
| 78 | + .nextScreenProvider(new SimpleNextScreenProvider(ExampleAppActivity.class)) |
| 79 | + .loginProviders(loginProviders) |
| 80 | + .theme(themeBuilder.build()) |
| 81 | + .build(); |
| 82 | +``` |
| 83 | +And finally, we add the Argus activity to AndroidManifest.xml |
| 84 | + |
| 85 | +```xml |
| 86 | +<activity |
| 87 | + android:name="com.moldedbits.argus.ArgusActivity" |
| 88 | + android:label="@string/app_name" |
| 89 | + android:screenOrientation="portrait" |
| 90 | + android:theme="@style/AppTheme"> |
| 91 | + <intent-filter> |
| 92 | + <action android:name="android.intent.action.MAIN"/> |
| 93 | + <category android:name="android.intent.category.LAUNCHER"/> |
| 94 | + </intent-filter> |
| 95 | +</activity> |
| 96 | +``` |
| 97 | +And that is all. With this, we have setup an app with all the dependencies and scaffolding setup to get the developer started straight on the business logic of the app, and a basic login framework. |
| 98 | + |
| 99 | +![Demo Login][login-screenshot] |
| 100 | + |
| 101 | +Happy coding! |
| 102 | + |
| 103 | +The moldedbits Team |
| 104 | + |
| 105 | +[studio-screenshot]: {{site.url}}/assets/images/android-studio-import.png "Android Studio Import" |
| 106 | +[login-screenshot]: {{site.url}}/assets/images/jump-demo-login.png "Demo Login" |
0 commit comments