Top Banner
Overview of Android Infrastructure Alexey Buzdin
95

Overview of Android Infrastructure

Feb 15, 2017

Download

Technology

ctco
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Overview of Android Infrastructure

Overview of Android Infrastructure

Alexey Buzdin

Page 2: Overview of Android Infrastructure

@AlexeyBuzdin

[email protected]

LArchaon

Page 3: Overview of Android Infrastructure

Everyone needs an app....

Page 4: Overview of Android Infrastructure

We need an app too!

Page 5: Overview of Android Infrastructure

simple CRUD app

Page 6: Overview of Android Infrastructure
Page 7: Overview of Android Infrastructure

Requirement:

Support as many android devices as you can

(including tablets)

Page 8: Overview of Android Infrastructure

4.04.1–4.2

3.0

2.3

2.22.1

Google data, March 2013

Page 9: Overview of Android Infrastructure
Page 10: Overview of Android Infrastructure

Ain't that good for a developer

Page 11: Overview of Android Infrastructure
Page 12: Overview of Android Infrastructure
Page 13: Overview of Android Infrastructure
Page 14: Overview of Android Infrastructure

Is there is someone who could help us?

Page 15: Overview of Android Infrastructure

nope

Page 16: Overview of Android Infrastructure

Ease our pain

Page 17: Overview of Android Infrastructure

maybe

Page 18: Overview of Android Infrastructure
Page 19: Overview of Android Infrastructure
Page 20: Overview of Android Infrastructure

Layers

Presentation

Application logic

Domain

Presentation

Application logic

Domain

Page 21: Overview of Android Infrastructure

Presentation

Application logic

Domain

Presentation

Application logic

Domain

Page 22: Overview of Android Infrastructure

But Whers the problem?

Page 23: Overview of Android Infrastructure

Missing in Android 2.2

● ActionBar● Support for tablets● Decent ListView support for server communication

Page 24: Overview of Android Infrastructure

Solution?

Page 25: Overview of Android Infrastructure

Compatability libraries

Android Support library

ActionBarSherlock

etc

Page 26: Overview of Android Infrastructure

Support Libraryhttp://developer.android.com/tools/extras/support-library.html

Fragments

Async Stuff

Experimental stuff

Page 27: Overview of Android Infrastructure

Fragments

Page 28: Overview of Android Infrastructure

Example

Page 29: Overview of Android Infrastructure

ActionBar

Page 30: Overview of Android Infrastructure

ActionBarSherlock

http://actionbarsherlock.com/

Page 31: Overview of Android Infrastructure

Refreshing list

Page 32: Overview of Android Infrastructure

Pull to Refresh

https://github.com/chrisbanes/Android-PullToRefresh

Page 33: Overview of Android Infrastructure

Presentation

Application logic

Domain

Presentation

Application logic

Domain

Page 34: Overview of Android Infrastructure

Android Runtime

On Android you develop in Java

... but Android does not run Java Bytecode !

Page 35: Overview of Android Infrastructure

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)

Page 36: Overview of Android Infrastructure

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

Page 37: Overview of Android Infrastructure

App lifecycle

Page 38: Overview of Android Infrastructure

Activity

public class MyActivity extends Activity {

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

}

Page 39: Overview of Android Infrastructure
Page 40: Overview of Android Infrastructure

Activity

public class MyActivity extends Activity {

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

}

Page 41: Overview of Android Infrastructure

Resources

Page 42: Overview of Android Infrastructure

Activity

public class MyActivity extends Activity {

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

}

Page 43: Overview of Android Infrastructure

Context● Context of current state of the

application/object

Page 44: Overview of Android Infrastructure

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

Page 45: Overview of Android Infrastructure

Important

any resource taken from context will leave as long as Context does

Page 46: Overview of Android Infrastructure

Context problempublic class MyActivity extends Activity {

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

}

Page 47: Overview of Android Infrastructure

Passing Contextpublic class MyStringProvider {

Context context;

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

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

}

Page 48: Overview of Android Infrastructure

Passing Context

Page 49: Overview of Android Infrastructure

Context problempublic 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

Presentation

Application logic

Domain

Page 50: Overview of Android Infrastructure

Injection libraries

Dependency Injection● RoboGuice● Dagger

Page 51: Overview of Android Infrastructure

RoboGuice

● Based on Google Guice● Lightweight● Multifunctional (has resource injection)

Page 52: Overview of Android Infrastructure

RoboGuicepublic 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()); }

}

Page 53: Overview of Android Infrastructure

RoboGuicepublic class MyStringProvider {

Context context;

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

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

Page 54: Overview of Android Infrastructure

Notable fact

Page 55: Overview of Android Infrastructure

Optimezed context injectionpublic 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()); }

}

Page 56: Overview of Android Infrastructure

RoboGuicepublic 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; }}

Page 57: Overview of Android Infrastructure

Daggerpublic 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());

}

}

Page 58: Overview of Android Infrastructure

Daggerpublic 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); }}

Page 59: Overview of Android Infrastructure

Daggerpublic 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); }}

Page 60: Overview of Android Infrastructure

@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; }

}

Page 61: Overview of Android Infrastructure

SummaryDagger

● More customizable● Easy communicates with other libraries● Requires more code

RoboGuice● Simpler● Out of Box functionality

Page 62: Overview of Android Infrastructure

Problem with views

Page 63: Overview of Android Infrastructure
Page 64: Overview of Android Infrastructure
Page 65: Overview of Android Infrastructure

Other “Injection” libraries

Resource and View “Injection”● RoboGuice● ButterKnife● AndroidAnnotations

Page 66: Overview of Android Infrastructure

RoboGuice Views Injectionpublic 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()); }

}

Page 67: Overview of Android Infrastructure

Butterknifepublic 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()); }

}

Page 68: Overview of Android Infrastructure

AndroidAnnotations@EActivity(R.layout.translate) // Sets content view to R.layout.translatepublic 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); } }

Page 69: Overview of Android Infrastructure

Presentation

Application logic

Domain

Presentation

Application logic

Domain

Page 70: Overview of Android Infrastructure

Data Source

Android has built in SQLite

Page 71: Overview of Android Infrastructure

Data Source

... but lacks ORM

Alternatives:– GreenDAO– ORMLite

Page 72: Overview of Android Infrastructure

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

}

}

Page 73: Overview of Android Infrastructure

// 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();

ORMLite

Page 74: Overview of Android Infrastructure

Testing

Page 75: Overview of Android Infrastructure

Testing

● DVM or JVM● Automation or Unit Tests

Page 76: Overview of Android Infrastructure

DVM

● Requires a separate Test Project● android.test or Robotium

Page 77: Overview of Android Infrastructure

Android.testpublic 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()); }}

Page 78: Overview of Android Infrastructure

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();

);               

Page 79: Overview of Android Infrastructure

JVMpublic class RoboGuiceActivityTest {

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

Page 80: Overview of Android Infrastructure

JVMpublic class RoboGuiceActivityTest {

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

Page 81: Overview of Android Infrastructure
Page 82: Overview of Android Infrastructure

We have Mockito! lets mock something

in our Activity ...

Page 83: Overview of Android Infrastructure

Activity source code

Page 84: Overview of Android Infrastructure

Robolectric to the rescue

Allows you to run

you android code

on JVM

Page 85: Overview of Android Infrastructure

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)); }}

Page 86: Overview of Android Infrastructure

Robolectric + Mockito

Page 87: Overview of Android Infrastructure

Testing● DVM or JVM● Automation or Unit Tests● Robotium or Robolectric● Or both

Page 88: Overview of Android Infrastructure

Conclusion

Page 89: Overview of Android Infrastructure

More libraries?

Page 90: Overview of Android Infrastructure

More libraries

Page 91: Overview of Android Infrastructure

Solution?

Page 92: Overview of Android Infrastructure

Android Bootstrap● Fragments,Fragment Pager● android-maven-plugin, Dagger● ActionBarSherlock ● ViewPagerIndicator● http-request, GSON ● Robotium

http://www.androidbootstrap.com/

Page 93: Overview of Android Infrastructure

or customs scripts

Page 94: Overview of Android Infrastructure

Build Systems● Gradle● Maven

https://github.com/LArchaon/android_mvn_template/

Page 95: Overview of Android Infrastructure

Questions?