Overview of Android
   Infrastructure
             Alexey Buzdin
@AlexeyBuzdin


alex.buzdin@gmail.com


            LArchaon
Everyone needs an app....
We need an app too!
simple CRUD app
Overview of Android Infrastructure
Requirement:
Support as many android
   devices as you can
    (including tablets)
4.1–4.2
4.0


                        2.1
3.0                    2.2


2.3

 Google data, March 2013
Overview of Android Infrastructure
Ain't that good for a developer
Overview of Android Infrastructure
Overview of Android Infrastructure
Overview of Android Infrastructure
Is there is someone who
      could help us?
nope
Ease our pain
maybe
Overview of Android Infrastructure
Overview of Android Infrastructure
Layers

 Presentation


Application logic


    Domain
Presentation


Application logic


    Domain
But Whers the problem?
Missing in Android 2.2
●
    ActionBar
●
    Support for tablets
●
    Decent ListView support for server communication
Solution?
Compatability libraries
Android Support library
ActionBarSherlock
etc
Support Library
http://developer.android.com/tools/extras/support-
library.html

                   Fragments
                  Async Stuff
               Experimental stuff
Fragments
Example
ActionBar
ActionBarSherlock




 http://actionbarsherlock.com/
Refreshing list
Pull to Refresh




  https://github.com/chrisbanes/Andro
  id-PullToRefresh
Presentation


Application logic


    Domain
Android Runtime
On Android you develop in Java
  ... but Android does not run Java Bytecode !
DalvikVM
Dalvik Virtual Machine
– Custom VM optimized for mobile devices
– Register-based JVM
– More efficient and compact
– Use memory efficiently
– Dalvik Executable Code (.dex)
   ● 30% fewer instructions

   ● 35% fewer code units

   ● 35% more bytes

– Trace JIT compiler (since 2.2)
Android Runtime
Android Java = Java language + Dalvik + Apache Harmony
Android Java API = Java SE – AWT/Swing + Android API
Sun-Java = Java language + JVM + JDK




                                                         36
App lifecycle
Activity
public class MyActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
    }

}
Overview of Android Infrastructure
Activity
public class MyActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
    }

}
Resources
Activity
public class MyActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
    }

}
Context
●
    Context of current state of the
    application/object
Context
●
    Context of current state of the
    application/object
●
    Context is a handle to the system it
    provides services like
    – resolving resources
    – obtaining access to databases and
      preferences
Important
any resource taken from context will
    leave as long as Context does
Context problem
public class MyActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      String appName = getString(R.string.appName);
    }

}
Passing Context
public class MyStringProvider {

    Context context;

    public MyStringProvider(Context context) {
      this.context = context;
    }

    public String getString(){
      return context.getString(R.string.app_name);
    }

}
Passing Context
Context problem
public class MyActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      String appName = getString(R.string.appName);
    }

}
                                                 Presentation


                                               Application logic


                                                    Domain
Injection libraries
  Dependency Injection
●
  RoboGuice
●
  Dagger
RoboGuice

●
    Based on Google Guice
●
    Lightweight
●
    Multifunctional (has resource injection)
RoboGuice
public class MyActivity extends RoboActivity {

    @Inject MyStringProvider stringProvider;

    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);

        ((TextView) findViewById(R.id.textView))
            .setText(stringProvider.getString());
    }

}
RoboGuice
public class MyStringProvider {

    Context context;

    @Inject
    public MyStringProvider(Context context) {
      this.context = context;
    }

    public String getString(){
      return context.getString(R.string.app_name);
    }
}
Notable fact
Optimezed context injection
public class MyActivity extends RoboActivity {

    int i;

    @Inject MyStringProvider stringProvider;

    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);

        i = 1;
        ((TextView) findViewById(R.id.textView))
           .setText(stringProvider.getString());
    }

}
RoboGuice
public class MyStringProvider {

    Context context;

    @Inject
    public MyStringProvider(Context context) {
      this.context = context;
    }

    public String getString(){
      return context.getString(R.string.app_name) + ((MyActivity)context).i;
    }
}
Dagger
public class DaggerActivity extends DaggerBaseActivity {

    @Inject DaggerStringProvider stringProvider;

    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);

        ((TextView) findViewById(R.id.textView))
           .setText(stringProvider.getString());
    }

}
Dagger
public class DaggerBaseActivity extends Activity {

    @Inject
    Bus bus;

    @Override protected void onCreate(Bundle state) {
     super.onCreate(state);

        DaggerApplication.inject(this);
    }

    @Override protected void onResume() {
      super.onResume();
      bus.register(this);
    }

    @Override protected void onPause() {
      super.onPause();
      bus.unregister(this);
    }
}
Dagger
public class DaggerApplication extends Application {

    private static ObjectGraph objectGraph;

    @Override public void onCreate() {
     super.onCreate();

        objectGraph = ObjectGraph.create(new DaggerModule(this));
    }

    public static <T> void inject(T instance) {
      if(objectGraph != null) objectGraph.inject(instance);
    }
}
@Module(
    entryPoints = { DaggerApplication.class, DaggerActivity.class}
)
public class DaggerModule {
  private final Context appContext;

    public DaggerModule(Context appContext) {
      this.appContext = appContext.getApplicationContext();
    }

    @Provides @Singleton Bus provideBus() {
      return new Bus();
    }

    @Provides Context provideContext() {
      return appContext;
    }

}
Summary
    Dagger
●
    More customizable
●
    Easy communicates with other libraries
●
    Requires more code


    RoboGuice
●
    Simpler
●
    Out of Box functionality
Problem with views
Overview of Android Infrastructure
Overview of Android Infrastructure
Other “Injection” libraries
  Resource and View “Injection”
●
  RoboGuice
●
  ButterKnife
●
  AndroidAnnotations
RoboGuice Views Injection
public class RoboGuiceActivity extends RoboActivity {


    @Inject RoboGuiceStringProvider stringProvider;
    @InjectView(R.id.textView) TextView textView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);

        textView.setText(stringProvider.getString());
    }

}
Butterknife
public class DaggerActivity extends DaggerBaseActivity {

    @Inject DaggerStringProvider stringProvider;
    @InjectView(R.id.textView) TextView textView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Views.inject(this);

        textView.setText(stringProvider.getString());
    }

}
AndroidAnnotations
@EActivity(R.layout.translate) // Sets content view to R.layout.translate
public class TranslateActivity extends Activity {
    @ViewById // Injects R.id.textInput
    EditText textInput;
    @ViewById(R.id.myTextView) // Injects R.id.myTextView
    TextView result;
    @AnimationRes // Injects android.R.anim.fade_in
    Animation fadeIn;
    @Click // When R.id.doTranslate button is clicked
    void doTranslate() {
         translateInBackground(textInput.getText().toString());
    }
    @Background // Executed in a background thread
    void translateInBackground(String textToTranslate) {
         String translatedText = callGoogleTranslate(textToTranslate);
         showResult(translatedText);
    }

    @UiThread // Executed in the ui thread
    void showResult(String translatedText) {
         result.setText(translatedText);
         result.startAnimation(fadeIn);
    }
}
Presentation


Application logic


    Domain
Data Source
Android has built in SQLite
Data Source
... but lacks ORM


Alternatives:
–   GreenDAO
–   ORMLite
ORMLite
@DatabaseTable(tableName = "accounts")
public class Account {
    @DatabaseField(id = true) private String name;


    @DatabaseField(canBeNull = false) private String password;


    Account() {
     // all persisted classes must define a no-arg constructor with at least package
visibility
    }


}
ORMLite
// you get the SQLiteOpenHelper from your Android Activity
ConnectionSource connectionSource = new AndroidConnectionSource(sqliteOpenHelper);


// instantiate the DAO to handle Account with String id
Dao<Account,String> accountDao = BaseDaoImpl.createDao(connectionSource, Account.class);


TableUtils.createTable(connectionSource, Account.class);


                         String name = "Jim Smith";
                         Account account = new Account(name, "_secret");
                         accountDao.create(account)


                         Account account2 = accountDao.queryForId(name);
                         connectionSource.close();
Testing
Testing
●
  DVM or JVM
●
  Automation or Unit Tests
DVM
●
  Requires a separate Test Project
●
  android.test or Robotium
Android.test
public class SimpleActivityTestStandard extends
    ActivityUnitTestCase<SimpleActivity> {
  public SimpleActivityTestStandard() {
    super(SimpleActivity.class);
  }

  public void setUp() throws Exception {
    startActivity(new
Intent(getInstrumentation().getTargetContext(),
        SimpleActivity.class), null, null);
  }

     public void testLayout() {
      SimpleActivity activity = getActivity();
      assertNotNull(activity.findViewById(R.id.button1));
      Button view = (Button) activity
          .findViewById(com.example.test.target.R.id.button1);
      assertEquals("My Button 1", view.getText());
    }
}
 
                       Robotium
    public void testPreferenceIsSaved() throws Exception {
         Solo solo = new Solo(getInstrumentation(), getActivity());
         solo.sendKey(Solo.MENU);
        solo.clickOnText("More");
         solo.clickOnText("Preferences");
         solo.clickOnText("Edit File Extensions");
         assertTrue(solo.searchText("rtf"));
                 
         solo.clickOnText("txt");
         solo.clearEditText(2);
         solo.enterText(2, "robotium");
         solo.clickOnButton("Save");
         solo.goBack();
         solo.clickOnText("Edit File Extensions");

       assertTrue(solo.searchText("application/robotium")
       solo.finishOpenedActivities();
);
               
JVM
public class RoboGuiceActivityTest {

    @Test
    public void shouldHaveISet() throws Exception {
      RoboGuiceActivity activity = new RoboGuiceActivity();
      assertThat(activity.getI(), equalTo(1));
    }
}
JVM
public class RoboGuiceActivityTest {

    @Test
    public void shouldHaveISet() throws Exception {
      RoboGuiceActivity activity = new RoboGuiceActivity();
      assertThat(activity.getI(), equalTo(1));
    }
}
Overview of Android Infrastructure
We have Mockito!
lets mock something
   in our Activity ...
Activity source code
Robolectric to the rescue




    Allows you to run
    you android code
         on JVM
Robolectric
@RunWith(RobolectricRoboTestRunner.class)
public class RoboGuiceActivityTest {

    @Test
    public void shouldHaveISet() throws Exception {
      RoboGuiceActivity activity = new RoboGuiceActivity();
      activity.onCreate(null);
      assertThat(activity.getI(), equalTo(1));
    }
}
Robolectric + Mockito
Testing
●
  DVM or JVM
●
  Automation or Unit Tests
●
  Robotium or Robolectric
●
  Or both
Conclusion
More libraries?
More libraries
Solution?
Android Bootstrap
●
    Fragments,Fragment Pager
●
    android-maven-plugin, Dagger
●
    ActionBarSherlock
●
    ViewPagerIndicator
●
    http-request, GSON
●
    Robotium



                 http://www.androidbootstrap.com/
or customs scripts
Build Systems
● Gradle
● Maven




    https://github.com/LArchaon/android
    _mvn_template/
Questions?

More Related Content

PPT
Python Evolution
PPTX
Jdk 7 4-forkjoin
PDF
The Ring programming language version 1.4 book - Part 3 of 30
PPTX
Modern Java Workshop
PDF
The Ring programming language version 1.5.4 book - Part 14 of 185
PDF
A Playful Introduction to Rx
PPTX
Jersey framework
PDF
Advanced Debugging Using Java Bytecodes
Python Evolution
Jdk 7 4-forkjoin
The Ring programming language version 1.4 book - Part 3 of 30
Modern Java Workshop
The Ring programming language version 1.5.4 book - Part 14 of 185
A Playful Introduction to Rx
Jersey framework
Advanced Debugging Using Java Bytecodes

What's hot (20)

PDF
Google Guava & EMF @ GTUG Nantes
DOC
Ad java prac sol set
PDF
Leveraging Completable Futures to handle your query results Asynchrhonously
PDF
Java9 Beyond Modularity - Java 9 más allá de la modularidad
PPTX
Jdk(java) 7 - 6 기타기능
PDF
Let's go Developer 2011 sendai Let's go Java Developer (Programming Language ...
PPTX
Demystifying dependency Injection: Dagger and Toothpick
PDF
Easy Going Groovy 2nd season on DevLOVE
PDF
Lambda and Stream Master class - part 1
ODP
Java Concurrency
PPT
Swiss army knife Spring
PDF
Java Concurrency by Example
PPT
What's New in Groovy 1.6?
PDF
Google Guava for cleaner code
PDF
Google guava - almost everything you need to know
PPTX
Looping the Loop with SPL Iterators
PDF
Dependency Injection with CDI in 15 minutes
PPTX
Android & Kotlin - The code awakens #01
Google Guava & EMF @ GTUG Nantes
Ad java prac sol set
Leveraging Completable Futures to handle your query results Asynchrhonously
Java9 Beyond Modularity - Java 9 más allá de la modularidad
Jdk(java) 7 - 6 기타기능
Let's go Developer 2011 sendai Let's go Java Developer (Programming Language ...
Demystifying dependency Injection: Dagger and Toothpick
Easy Going Groovy 2nd season on DevLOVE
Lambda and Stream Master class - part 1
Java Concurrency
Swiss army knife Spring
Java Concurrency by Example
What's New in Groovy 1.6?
Google Guava for cleaner code
Google guava - almost everything you need to know
Looping the Loop with SPL Iterators
Dependency Injection with CDI in 15 minutes
Android & Kotlin - The code awakens #01
Ad

Viewers also liked (11)

DOC
Quantum professional academy
PDF
scan0006
PPTX
Crowdsourcing
PDF
ApresMOTOFRETE WEB2015VCliente
PDF
PMV Middle East Article and Cover May 2015
DOCX
Universidad Técnica Particular de Loja
PDF
Investor Development & Retention Presentation
PPTX
Biblioteca Digital Hispánica. Preservar. Difundir. Re-crear
ODP
Presentacion patatas
PPTX
Calabazas
Quantum professional academy
scan0006
Crowdsourcing
ApresMOTOFRETE WEB2015VCliente
PMV Middle East Article and Cover May 2015
Universidad Técnica Particular de Loja
Investor Development & Retention Presentation
Biblioteca Digital Hispánica. Preservar. Difundir. Re-crear
Presentacion patatas
Calabazas
Ad

Similar to Overview of Android Infrastructure (20)

PPT
Android Tutorial
PPT
android-tutorial-for-beginner
PPT
Beginning Native Android Apps
PDF
Marakana android-java developers
PDF
597724227-mad-unit2.pdf-Mobile Application Development
PDF
Androidoscon20080721 1216843094441821-9
PDF
Androidoscon20080721 1216843094441821-9
PPTX
Android - Application Framework
PDF
Android Introduction by Kajal
PDF
Modern Android app library stack
PPTX
Android beginners David
PDF
Hello android
PPT
Migrating JavaME Apps to Android
PPTX
Android apps development
PDF
Android Workshop Part 1
PPTX
Introduction to Android (before 2015)
PDF
Getting Started with Android - OSSPAC 2009
PDF
Matteo Gazzurelli - Andorid introduction - Google Dev Fest 2013
PDF
Lecture3
PDF
Android development first steps
Android Tutorial
android-tutorial-for-beginner
Beginning Native Android Apps
Marakana android-java developers
597724227-mad-unit2.pdf-Mobile Application Development
Androidoscon20080721 1216843094441821-9
Androidoscon20080721 1216843094441821-9
Android - Application Framework
Android Introduction by Kajal
Modern Android app library stack
Android beginners David
Hello android
Migrating JavaME Apps to Android
Android apps development
Android Workshop Part 1
Introduction to Android (before 2015)
Getting Started with Android - OSSPAC 2009
Matteo Gazzurelli - Andorid introduction - Google Dev Fest 2013
Lecture3
Android development first steps

More from C.T.Co (10)

PDF
Things They Didn't Teach Me In Academy
PPTX
Project Management in Information Technologies
PDF
Mobile vs Desktop
PPTX
Business Analysis in IT by Ilze Buksha, Latvian
PPT
Stratoplan 2013 Brief by C.T.Co
PPTX
Scrum methodology
PPT
Introduction to Performance Testing Part 1
PPT
Project management in Agile Way
PPTX
Data Access using Entity Framework
PPTX
Windows phone 7
Things They Didn't Teach Me In Academy
Project Management in Information Technologies
Mobile vs Desktop
Business Analysis in IT by Ilze Buksha, Latvian
Stratoplan 2013 Brief by C.T.Co
Scrum methodology
Introduction to Performance Testing Part 1
Project management in Agile Way
Data Access using Entity Framework
Windows phone 7

Recently uploaded (20)

PDF
WOOl fibre morphology and structure.pdf for textiles
PDF
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
PPT
Geologic Time for studying geology for geologist
PDF
A contest of sentiment analysis: k-nearest neighbor versus neural network
PDF
Hindi spoken digit analysis for native and non-native speakers
PDF
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
PDF
Developing a website for English-speaking practice to English as a foreign la...
PDF
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
PPTX
Modernising the Digital Integration Hub
PDF
Enhancing emotion recognition model for a student engagement use case through...
PDF
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
PDF
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game
PDF
Zenith AI: Advanced Artificial Intelligence
PPTX
O2C Customer Invoices to Receipt V15A.pptx
PPTX
Chapter 5: Probability Theory and Statistics
PPTX
observCloud-Native Containerability and monitoring.pptx
PPT
Module 1.ppt Iot fundamentals and Architecture
PDF
sustainability-14-14877-v2.pddhzftheheeeee
PPTX
Benefits of Physical activity for teenagers.pptx
PDF
A comparative study of natural language inference in Swahili using monolingua...
WOOl fibre morphology and structure.pdf for textiles
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
Geologic Time for studying geology for geologist
A contest of sentiment analysis: k-nearest neighbor versus neural network
Hindi spoken digit analysis for native and non-native speakers
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
Developing a website for English-speaking practice to English as a foreign la...
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
Modernising the Digital Integration Hub
Enhancing emotion recognition model for a student engagement use case through...
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game
Zenith AI: Advanced Artificial Intelligence
O2C Customer Invoices to Receipt V15A.pptx
Chapter 5: Probability Theory and Statistics
observCloud-Native Containerability and monitoring.pptx
Module 1.ppt Iot fundamentals and Architecture
sustainability-14-14877-v2.pddhzftheheeeee
Benefits of Physical activity for teenagers.pptx
A comparative study of natural language inference in Swahili using monolingua...

Overview of Android Infrastructure

  • 1. Overview of Android Infrastructure Alexey Buzdin
  • 4. We need an app too!
  • 7. Requirement: Support as many android devices as you can (including tablets)
  • 8. 4.1–4.2 4.0 2.1 3.0 2.2 2.3 Google data, March 2013
  • 10. Ain't that good for a developer
  • 14. Is there is someone who could help us?
  • 15. nope
  • 17. maybe
  • 22. But Whers the problem?
  • 23. Missing in Android 2.2 ● ActionBar ● Support for tablets ● Decent ListView support for server communication
  • 25. Compatability libraries Android Support library ActionBarSherlock etc
  • 32. Pull to Refresh https://github.com/chrisbanes/Andro id-PullToRefresh
  • 34. Android Runtime On Android you develop in Java ... but Android does not run Java Bytecode !
  • 35. DalvikVM Dalvik Virtual Machine – Custom VM optimized for mobile devices – Register-based JVM – More efficient and compact – Use memory efficiently – Dalvik Executable Code (.dex) ● 30% fewer instructions ● 35% fewer code units ● 35% more bytes – Trace JIT compiler (since 2.2)
  • 36. Android Runtime Android Java = Java language + Dalvik + Apache Harmony Android Java API = Java SE – AWT/Swing + Android API Sun-Java = Java language + JVM + JDK 36
  • 38. Activity public class MyActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } }
  • 40. Activity public class MyActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } }
  • 42. Activity public class MyActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } }
  • 43. Context ● Context of current state of the application/object
  • 44. Context ● Context of current state of the application/object ● Context is a handle to the system it provides services like – resolving resources – obtaining access to databases and preferences
  • 45. Important any resource taken from context will leave as long as Context does
  • 46. Context problem public class MyActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); String appName = getString(R.string.appName); } }
  • 47. Passing Context public class MyStringProvider { Context context; public MyStringProvider(Context context) { this.context = context; } public String getString(){ return context.getString(R.string.app_name); } }
  • 49. Context problem public class MyActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); String appName = getString(R.string.appName); } } Presentation Application logic Domain
  • 50. Injection libraries Dependency Injection ● RoboGuice ● Dagger
  • 51. RoboGuice ● Based on Google Guice ● Lightweight ● Multifunctional (has resource injection)
  • 52. RoboGuice public class MyActivity extends RoboActivity { @Inject MyStringProvider stringProvider; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ((TextView) findViewById(R.id.textView)) .setText(stringProvider.getString()); } }
  • 53. RoboGuice public class MyStringProvider { Context context; @Inject public MyStringProvider(Context context) { this.context = context; } public String getString(){ return context.getString(R.string.app_name); } }
  • 55. Optimezed context injection public class MyActivity extends RoboActivity { int i; @Inject MyStringProvider stringProvider; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); i = 1; ((TextView) findViewById(R.id.textView)) .setText(stringProvider.getString()); } }
  • 56. RoboGuice public class MyStringProvider { Context context; @Inject public MyStringProvider(Context context) { this.context = context; } public String getString(){ return context.getString(R.string.app_name) + ((MyActivity)context).i; } }
  • 57. Dagger public class DaggerActivity extends DaggerBaseActivity { @Inject DaggerStringProvider stringProvider; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ((TextView) findViewById(R.id.textView)) .setText(stringProvider.getString()); } }
  • 58. Dagger public class DaggerBaseActivity extends Activity { @Inject Bus bus; @Override protected void onCreate(Bundle state) { super.onCreate(state); DaggerApplication.inject(this); } @Override protected void onResume() { super.onResume(); bus.register(this); } @Override protected void onPause() { super.onPause(); bus.unregister(this); } }
  • 59. Dagger public class DaggerApplication extends Application { private static ObjectGraph objectGraph; @Override public void onCreate() { super.onCreate(); objectGraph = ObjectGraph.create(new DaggerModule(this)); } public static <T> void inject(T instance) { if(objectGraph != null) objectGraph.inject(instance); } }
  • 60. @Module( entryPoints = { DaggerApplication.class, DaggerActivity.class} ) public class DaggerModule { private final Context appContext; public DaggerModule(Context appContext) { this.appContext = appContext.getApplicationContext(); } @Provides @Singleton Bus provideBus() { return new Bus(); } @Provides Context provideContext() { return appContext; } }
  • 61. Summary Dagger ● More customizable ● Easy communicates with other libraries ● Requires more code RoboGuice ● Simpler ● Out of Box functionality
  • 65. Other “Injection” libraries Resource and View “Injection” ● RoboGuice ● ButterKnife ● AndroidAnnotations
  • 66. RoboGuice Views Injection public class RoboGuiceActivity extends RoboActivity { @Inject RoboGuiceStringProvider stringProvider; @InjectView(R.id.textView) TextView textView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); textView.setText(stringProvider.getString()); } }
  • 67. Butterknife public class DaggerActivity extends DaggerBaseActivity { @Inject DaggerStringProvider stringProvider; @InjectView(R.id.textView) TextView textView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Views.inject(this); textView.setText(stringProvider.getString()); } }
  • 68. AndroidAnnotations @EActivity(R.layout.translate) // Sets content view to R.layout.translate public class TranslateActivity extends Activity { @ViewById // Injects R.id.textInput EditText textInput; @ViewById(R.id.myTextView) // Injects R.id.myTextView TextView result; @AnimationRes // Injects android.R.anim.fade_in Animation fadeIn; @Click // When R.id.doTranslate button is clicked void doTranslate() { translateInBackground(textInput.getText().toString()); } @Background // Executed in a background thread void translateInBackground(String textToTranslate) { String translatedText = callGoogleTranslate(textToTranslate); showResult(translatedText); } @UiThread // Executed in the ui thread void showResult(String translatedText) { result.setText(translatedText); result.startAnimation(fadeIn); } }
  • 70. Data Source Android has built in SQLite
  • 71. Data Source ... but lacks ORM Alternatives: – GreenDAO – ORMLite
  • 72. ORMLite @DatabaseTable(tableName = "accounts") public class Account { @DatabaseField(id = true) private String name; @DatabaseField(canBeNull = false) private String password; Account() { // all persisted classes must define a no-arg constructor with at least package visibility } }
  • 73. ORMLite // you get the SQLiteOpenHelper from your Android Activity ConnectionSource connectionSource = new AndroidConnectionSource(sqliteOpenHelper); // instantiate the DAO to handle Account with String id Dao<Account,String> accountDao = BaseDaoImpl.createDao(connectionSource, Account.class); TableUtils.createTable(connectionSource, Account.class); String name = "Jim Smith"; Account account = new Account(name, "_secret"); accountDao.create(account) Account account2 = accountDao.queryForId(name); connectionSource.close();
  • 75. Testing ● DVM or JVM ● Automation or Unit Tests
  • 76. DVM ● Requires a separate Test Project ● android.test or Robotium
  • 77. Android.test public class SimpleActivityTestStandard extends ActivityUnitTestCase<SimpleActivity> { public SimpleActivityTestStandard() { super(SimpleActivity.class); } public void setUp() throws Exception { startActivity(new Intent(getInstrumentation().getTargetContext(), SimpleActivity.class), null, null); } public void testLayout() { SimpleActivity activity = getActivity(); assertNotNull(activity.findViewById(R.id.button1)); Button view = (Button) activity .findViewById(com.example.test.target.R.id.button1); assertEquals("My Button 1", view.getText()); } }
  • 78.   Robotium   public void testPreferenceIsSaved() throws Exception {   Solo solo = new Solo(getInstrumentation(), getActivity());        solo.sendKey(Solo.MENU);       solo.clickOnText("More");        solo.clickOnText("Preferences");        solo.clickOnText("Edit File Extensions");        assertTrue(solo.searchText("rtf"));                        solo.clickOnText("txt");        solo.clearEditText(2);        solo.enterText(2, "robotium");        solo.clickOnButton("Save");        solo.goBack();        solo.clickOnText("Edit File Extensions");        assertTrue(solo.searchText("application/robotium") solo.finishOpenedActivities(); );                
  • 79. JVM public class RoboGuiceActivityTest { @Test public void shouldHaveISet() throws Exception { RoboGuiceActivity activity = new RoboGuiceActivity(); assertThat(activity.getI(), equalTo(1)); } }
  • 80. JVM public class RoboGuiceActivityTest { @Test public void shouldHaveISet() throws Exception { RoboGuiceActivity activity = new RoboGuiceActivity(); assertThat(activity.getI(), equalTo(1)); } }
  • 82. We have Mockito! lets mock something in our Activity ...
  • 84. Robolectric to the rescue Allows you to run you android code on JVM
  • 85. Robolectric @RunWith(RobolectricRoboTestRunner.class) public class RoboGuiceActivityTest { @Test public void shouldHaveISet() throws Exception { RoboGuiceActivity activity = new RoboGuiceActivity(); activity.onCreate(null); assertThat(activity.getI(), equalTo(1)); } }
  • 87. Testing ● DVM or JVM ● Automation or Unit Tests ● Robotium or Robolectric ● Or both
  • 92. Android Bootstrap ● Fragments,Fragment Pager ● android-maven-plugin, Dagger ● ActionBarSherlock ● ViewPagerIndicator ● http-request, GSON ● Robotium http://www.androidbootstrap.com/
  • 94. Build Systems ● Gradle ● Maven https://github.com/LArchaon/android _mvn_template/