Implementation steps - Ruchita7/Build_it_bigger GitHub Wiki

###Implementation Steps###

Step 1: Create a Java library

Create a new Gradle Java project either using the Android Studio wizard, or by hand. Then introduce a project dependency between the app and the new Java Library.

Refer Joke producer lib and add dependency in build.gradle
compile project(':jokeproducerlib') :

Step 2: Create an Android Library

Create an Android Library containing an Activity that will display a joke passed to it as an intent extra. Wire up project dependencies so that the button can now pass the joke from the Java Library to the Android Library.

Refer Joke Consumer lib and add dependency in build.gradle
compile project(':jokeconsumerlib')

** Step 3:Create GCE Module**

Set up a Google Cloud Endpoints development server, and pull our jokes from there. Follow the instructions in the following tutorial to add a Google Could Endpoints module to your project.

Introduce a project dependency between your Java library and your GCE module, and modify the GCE starter code to pull jokes from your Java library. Create an Async task to retrieve jokes. Make the button kick off a task to retrieve a joke, then launch the activity from your Android Library to display it.

Refer Backend and add dependency in build.gradle
compile project(path: ':backend', configuration: 'android-endpoints')

In Endpoints async task add

protected String doInBackground(Void... params) { if (myApiService == null) { MyApi.Builder builder = new MyApi.Builder(AndroidHttp.newCompatibleTransport(), new AndroidJsonFactory(), null).setRootUrl("https://myjokes-1214.appspot.com/_ah/api/"); myApiService = builder.build(); } } try { return myApiService.sayHi().execute().getData(); } catch (IOException e) { return e.getMessage(); }

Step 4: Add Functional Tests

Add code to test that your Async task successfully retrieves a non-empty string

In Instrumentation Test, used Countdown latch for decrementing the count with await() block until the current count reaches zero :

public void testAsyncTask() throws Throwable { runTestOnUiThread(new Runnable() { @Override public void run() { try { endpointsAsyncTask.execute(); countDownLatch.await(30, TimeUnit.SECONDS); } catch (InterruptedException e) { e.printStackTrace(); } } }); }

** Step 5: Add a Paid Flavor**

Add free and paid product flavors to your app. Remove the ad (and any dependencies you can) from the paid flavor.

In build.gradle add freeCompile 'com.google.android.gms:play-services-ads:8.4.0'

Optional Tasks

(i) Add Interstitial Ad : Follow these instructions to add an interstitial ad to the free version. Display the add after the user hits the button, but before the joke is shown. Refer Free version

(ii) Add Loading Indicator : Add a loading indicator that is shown while the joke is being retrieved and disappears when the joke is ready. Override onProgressUpdate() in EndpointsAsyncTask

(iii) Configure Test Task To tie it all together, create a Gradle task that:

  • Launches the GCE local development server

  • Runs all tests

  • Shuts the server down again

In Project build.gradle add tasks

task runAppEngine (dependsOn: ":backend:appengineRun") { doLast { println "Started the server!" } }

task stopAppEngine (dependsOn: ":backend:appengineStop") { doLast { println "Server is stopped" } }

//Stop GCE server if started runAppEngine.finalizedBy stopAppEngine

//Run as gradle --daemon gceServerTask //Start the server, run android tests and stop the server task gceServerTask(dependsOn : ["runAppEngine",":app:connectedAndroidTest","stopAppEngine"]) { evaluationDependsOn ":backend" project(":backend"){ appengine{ daemon true } }

`tasks.getByPath(":app:connectedAndroidTest").dependsOn tasks.getByPath("runAppEngine")`
`tasks.getByPath("stopAppEngine").mustRunAfter  tasks.getByPath(":app:connectedAndroidTest")``