Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Android development

Modern way of android development

  • Login to see the comments

Android development

  1. 1. Android development Modern way of android development
  2. 2. Who I am Gregoire BARRET Web: www.smartsoftasia.com Email: greg@smartsoftasia.com
  3. 3. Summary 1. Application pattern 2. Dependency injection 3. Reactive programming 4. Glide and OkHttp 5. Annotation 6. Testing 7. Kotlin 8. Future of android development
  4. 4. Application pattern
  5. 5. ● Gold activity class ● Spaghetti code ● No architecture Activity Model Business logic Database Network No patternBefore
  6. 6. Controller Model View ● Layer separation ● Readable ● Reusable Now MVC
  7. 7. ● Model not update the view ● Flexibility ● More android way Presenter Model View MVPNow
  8. 8. Presenter Model View ● Interface ● Fix inclusion ● Life cycle MVP for androidNow
  9. 9. ViewModel Model View ● Propagation of change ● Many to one relationship ● Require binder MVVMNow
  10. 10. Activity XML private void setViewModel(ViewModel viewModel){ binding.setViewModel(viewModel); } <TextView <!-- --> android:text="@{viewModel.name}"/> MVVM + DataBindingNow
  11. 11. Dependency injection
  12. 12. What is dependency injection ● Every application have a kind of dependency injection. ● Model need an other model Post message Message API HTTP Client Before
  13. 13. What is dependency injection public class HttpClient { public HttpClient(){ } public void execute(Request request) { //TODO: send request } } Before
  14. 14. What is dependency injection public class MessageApi { private HttpClient httpClient; public MessageApi(HttpClient httpClient){ this.httpClient = httpClient; } public void sendMessage(String user, String message){ httpClient.execute(new Request(user, message)); } } Before
  15. 15. What is dependency injection public class SendMessage { private MessageApi messageApi; private String user; public SendMessage(MessageApi messageApi, String user) { this.messageApi = messageApi; this.user = user; } public void send(String message){ messageApi.sendMessage(user, message); } } Before
  16. 16. What is dependency injection HttpClient httpClient = new HttpClient(); MessageApi messageApi = new MessageApi(httpClient); SendMessage sendMessage = new SendMessage(messageApi, "user"); sendMessage.send("Hello"); Before
  17. 17. What is dependency injection Post message Message API HTTP Client Post message
  18. 18. What is dependency injection Post message Message API HTTP Client Post message
  19. 19. Dagger 2 ● Developped by Google ● Work with proguard ● Fast ● Low configurations ● Multiple injection type ○ Constructor ○ Field ○ Method Now
  20. 20. Module ● Class hold all dependencies ● Work with a Component ● Bridge between Module and Injection @Module public class MessageModule { @Provide @Singleton HttpClient provideHttpClient(){ return new HttpClient(); } @Provide @Singleton MessageApi provideMessageApi(HttpClient httpClient){ return new MessageApi(httpClient); } @Provide SendMessage provideSendMessage(MessageApi messageApi){ return new SendMessage(messageApi, "User"); } } Now
  21. 21. Application public class Application { @Inject SendMessage sendMessage; public void main(){ sendMessage.send("Hello"); } } Now
  22. 22. Reactive X
  23. 23. Threading HTTP Client Activity AsyncTask Before
  24. 24. Threading HTTP Client Activity Thread Before
  25. 25. Included Listener getApi(new ResultListener() { @Override public void onSuccess() { getApi(new ResultListener() { @Override public void onSuccess() { //TODO } }); } }); Before
  26. 26. ReactiveX (RxJava) An API for asynchronous programming with observable streams
  27. 27. ReactiveX (RxJava) Functional Operation Observable Observer
  28. 28. Observer onNext onComplete onError
  29. 29. Observable create{ onNext( ); onNext( ); onComplete(); }
  30. 30. Functional Operation map() take(2)
  31. 31. Combining Combine()
  32. 32. Implementation Observable.just(1, 2, 3, 4) .map((Func1) (i) → { return String.valueOf(i); }) .subscribeOn(Schedulers.newThread()) .observeOn(AndroidSchedulers.mainThread()) .subscribe((Action1) (String) →{ Log.d("", s); }; Now
  33. 33. Rx Java Presentation Layer Domain Layer Data Layer MVP Interactor POJO API Database Interface Interface Now
  34. 34. Http library
  35. 35. Application Web API JSON
  36. 36. Old way, manually public static boolean postUserName(final Context context, final String id, final String userName){ String serverUrl = BaseAppConfig.BASE_URL + ApiPreferences.USER_API; serverUrl += "?id=" + id; long backoff = BACKOFF_MILLI_SECONDS + random.nextInt(1000); for (int i = 1; i <= MAX_ATTEMPTS; i++) { try { post(serverUrl, null, userName); return true; } catch (IOException e) { if (i == MAX_ATTEMPTS) { break; } try { Thread.sleep(backoff); } catch (InterruptedException e1) { Thread.currentThread().interrupt(); return false; } backoff *= 2; } } return false; } private static void post(String endpoint, Map<String, String> params, String token, String apiKey) throws IOException { URL url = null; try { url = new URL(endpoint); } catch (MalformedURLException e) { } if (url==null)return; HttpURLConnection conn = null; try { conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setUseCaches(false); conn.setRequestMethod("POST"); conn.setRequestProperty("X-Locale", "en"); int status = conn.getResponseCode(); //TODO: Code for read input stream } finally { if (conn != null) { conn.disconnect(); } } } Before
  37. 37. Retrofit public interface UserService { @GET("user/{user_id}") User getUser(@Path("user_id") int userId); } NowNow
  38. 38. Retrofit Retrofit Gson OkHttp Now
  39. 39. Retrofit OkHttpClient client = new OkHttpClient.Builder().build(); retrofit = new Retrofit.Builder().baseUrl(AppConfig.BASE_URL) .client(client) .addConverterFactory(GsonConverterFactory.create(new GsonBuilder().create())) .build(); userService = retrofit.create(UserService.class); Now
  40. 40. Retrofit OkHttpClient client = new OkHttpClient.Builder().build(); retrofit = new Retrofit.Builder().baseUrl(AppConfig.BASE_URL) .client(client) .addConverterFactory(GsonConverterFactory.create(new GsonBuilder().create())) .build(); userService = retrofit.create(UserService.class); Now
  41. 41. Retrofit OkHttpClient client = new OkHttpClient.Builder().build(); retrofit = new Retrofit.Builder().baseUrl(AppConfig.BASE_URL) .client(client) .addConverterFactory(GsonConverterFactory.create(new GsonBuilder().create())) .build(); userService = retrofit.create(UserService.class); Now
  42. 42. Retrofit OkHttpClient client = new OkHttpClient.Builder().build(); retrofit = new Retrofit.Builder().baseUrl(AppConfig.BASE_URL) .client(client) .addConverterFactory(GsonConverterFactory.create(new GsonBuilder().create())) .build(); userService = retrofit.create(UserService.class); Now
  43. 43. Retrofit OkHttpClient client = new OkHttpClient.Builder().build(); retrofit = new Retrofit.Builder().baseUrl(AppConfig.BASE_URL) .client(client) .addConverterFactory(GsonConverterFactory.create(new GsonBuilder().create())) .build(); userService = retrofit.create(UserService.class); Now
  44. 44. Retrofit User user = restclient.getUserService.getUser(5); Now
  45. 45. Annotation
  46. 46. What An annotation can do? Provide informations for the compiler Allow runtime processing Allow compile time processing
  47. 47. Bind view example @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_user); TextView nameTextView = (TextView) findViewById(R.id.textView_user_name); TextView phoneTextView = (TextView) findViewById(R.id.textView_user_phone); ImageView profileImageview = (ImageView) findViewById(R.id.imageView_user_profile); Button callButton = (Button) findViewById(R.id.button_call); } Before
  48. 48. Butterknife @BindView(R.id.button_friendprofile_block) protected TextView nameTextView; @BindView(R.id.button_friendprofile_block) protected TextView phoneTextView; @BindView(R.id.button_friendprofile_block) protected ImageView profileImageview; @BindView(R.id.button_friendprofile_block) protected Button callButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_user); ButterKnife.bind(this); } Now
  49. 49. Skip enum public static final String TYPE_ONE = "one"; public static final String TYPE_TWO= "two"; @StringDef({ TYPE_ONE, TYPE_TWO}) @Retention(RetentionPolicy.SOURCE) public @interface MyType {} @MyType private String type; public enum MyType { TYPE_ONE, TYPE_TWO } private MyType type; NowBefore
  50. 50. Example of library that use annotations ● ButterKnife ● Dagger ● Retrofit ● Real ● Gson ● ...
  51. 51. Testing
  52. 52. Testing framework
  53. 53. Testing framework
  54. 54. Testing Local unit test Instrument test UI test
  55. 55. CI PipelineCode CI Pipeline Prod Beta
  56. 56. What is kotlin ● Programing language ● Interoperable ● Concise ● Safe ● Versatile
  57. 57. What does it look like class Greeter(val name: String) { fun greet() { println("Hello, $name") } } fun main(args: Array<String>) { Greeter(args[0]).greet() }
  58. 58. Kotlin over java Null safety: b?.length
  59. 59. Kotlin over java Null safety: b?.length Data class: data class User(val name: String, val age: Int)
  60. 60. Kotlin over java Null safety: b?.length Data class: data class User(val name: String, val age: Int) Smart cast: fun demo(x: Any) { if (x is String) { print(x.length) } }
  61. 61. Kotlin over java Lambda expression: max(strings, { a, b -> a.length < b.length })
  62. 62. Kotlin over java Lambda expression: max(strings, { a, b -> a.length < b.length }) Range: for (i in 1..4) print(i)
  63. 63. Kotlin over java Lambda expression: max(strings, { a, b -> a.length < b.length }) Range: for (i in 1..4) print(i) And much more...
  64. 64. The ugly ● Slow compile time ● Larger app ● Issue with annotation processor ● Testing ● No static analyzer
  65. 65. Future of android
  66. 66. Next billion user 77M 223M 538M 1B 1.4B 2011 2012 2013 2014 2015
  67. 67. Android everywhere
  68. 68. Android closer to the web ● Instant app ● Progressive app ● Js development on android
  69. 69. Android Potential Maturity
  70. 70. Questions? greg@smartsoftasia.com

×