Android Tips/Tricks @DavidCorrado About.me/DavidCorrado Mobile Meetup Organizer
Android Tips/Tricks@DavidCorrado
About.me/DavidCorradoMobile Meetup Organizer
IDE
● Don’t Use Eclipse● Use either Android Studio/IntelliJ
○ They are basically the same thing. They are both built off of IntelliJ
○ Still in beta but everyone uses it● I recommend Android Studio. It takes out
the stuff that is not android related and dumbs things down =)
Attach Debugger Button
● If you have built the application on the device/emulator you can press this button at any time and it will attach the debugger○ After a crash you can put a breakpoint and attach to
the debugger and dig into what is happening.
Crashlytics
● Bug Reporting○ Tons of information on crashes
● QA Distributionsoutside of app store
● Analytics
Emulator Slow● When creating an emulator
○ Use Intel X86 version.○ Check Hardware acceleration
● Use Genymotion to run Android in a virtual machine○ Its kind of buggy. I had to run android 4.2.2○ http://www.genymotion.com/
● Run on a device○ Before going to production you have to run on a device anyway○ But keep in mind different screen sizes
● I use Genymotion and devices
Clean Code on Save● Every Time you save it will clean up your code to match google’s standard● Create Macro
○ Menu->Edit->Macro->Start Recording● Choose Menu->Code
○ Reformat Code,Auto Indent, Optimize Import, Rearrange Code● Stop Recording
○ Menu->Edit->Macro->Stop Recording● Override Save Button
○ Menu->Android Studio->Preferences> Search Keymap->Find Macros and the macro you saved and set the key to be save all command
● I then recommend you reformat the entire app so after that all your checkins are clean
Distribute Default settings
● You can save your settings and distribute them to your team.○ Menu->Export Settings
■ Be careful what you choose here. You will want to test this before distributing to the team. I think we ran into a problem with the SDK tables
Git
● Try to use Android studio for everything with Git.○ History,Merge,Commit,Push/Pull, Stash
● Example if you made a few changes to a file. You want to revert just a single function. You can compare with previous revision. Then the buttons to choose previous for that function
Auto Update Android SDK● When building your application can automatically
download Android SDK/Play Services/Build Tools/Platform Tools etc○ https://github.com/JakeWharton/sdk-manager-plugin
● Example. The lead developer updates any of the above. When you pull down the code it will give you errors that you need to update any of the above
● With the Tool above it automagically works
Browse Android SDK Source
● You can do this easily by downloading the Android SDK Source from the SDK Manager. This will make it so you can browse the android source just as easily as your own.○ Example you can browse how the Button works
Communication Between Fragments/Activities
● It is not very easy to communicate between activities and fragments etc.
● EventBus sends an event that we can listen to anywhere.○ https://github.com/greenrobot/EventBus
● If you use otto or something else and prefer it over greenrobots Event bus contact me after so you can do a talk on it at the mobile meetup.
Automagically Handle Network
● You can theoretically never touch JSON. You send it a model. It returns a model
● Automatically does request on separate thread and returns data via callback
● REST,Multipart form● Retrofit
○ http://square.github.io/retrofit/
Database● I prefer to never touch the database except in the DAO. So I only work
with models.● You can manually do this or there are frameworks that do this
○ Green DAO is popular http://greendao-orm.com/○ Thing I do not like about it is there is a build process to generate DAO
objects● Interesting framework that you just do annotations and the basic methods
DAO methods are created without a build process. I haven’t used this but its interesting○ https://github.com/emilsjolander/sprinkles
Global Context?● You have context being sent all around the application.
It seems some places you can just use the application context.
● For Example if you needed ○ context.getString(R.string.hello);
● If you were doing the above inside of a method you would have to pass the context to the method. Or if you use the example on the next slide you can do MyApplication.getContext().getString(R.string.hello); Or create a helper that does essentially the above
Global Context Example//In Manifest
<application android:name="com.xyz.MyApplication">
</application>
//then write the class
public class MyApplication extends Application{
private static Context context;
public void onCreate(){
super.onCreate();
MyApplication.context = getApplicationContext();
}
public static Context getAppContext() {
return MyApplication.context;
}
}
Dependency Injection
● Dagger is popular○ http://square.github.io/dagger/
● I am not really the best person to speak on this. Its definately something to look into.
● You could use this instead of a global context like I demoed before
Automagically Handle Images
● Load Images in one line of code. Something like○ Picasso.with(context).load(urlString).into
(imageView);● It can handle resizing. Making images into
circles etc● Picasso
○ http://square.github.io/picasso/
Multiple Image Resources● mdpi,hdpi,xhdpi etc● Create an image at your highest supported resolution
and then use one of the below tools● http://romannurik.github.io/AndroidAssetStudio/
○ There are a lot of cool tools in here look through it● Write or google for a script that automatically creates all
of the images you need and puts them in the appropriate folder
Cleaner Code with Butterknife● Someone should do a whole talk on this. Essentially its
helps you reference your views easier● Butterknife
○ http://jakewharton.github.io/butterknife/● There are a lot of different cases where you can use this
Cleaner Code With Butterknife Example
//Old wayButton button= (Button) findViewById(R.id.submit);button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //Do Onclick logic }});//With Butterknife@OnClick(R.id.submit)public void submit() { //Do Onclick logic}
Code Completion
● You can auto create models getter and setters○ Right click in code ->Generate->Getters and Setters○ You can do this for a lot of different methods
● You can also type in the class and have it automatically complete override methods
● Android Intentions on next slide
Android Intentions● This is the automagic creation of code that they think you want to.
○ On mac you can do this with hotkey alt+enter○ You can also put your cursor on the area that you want the intention to
run on. Then you should see a lightbulb to have it tell you the intentions
● Android team recommends you look through these so you use them. Menu->Android Studio->Preferences->Search Intentions
● This does things like create string resource, implement unimplemented methods, etc
Handling Configuration Changes● If an android device is rotated everything is destroyed and recreated again. There are alot of
ways to handle this○ Put all data into model every time the activity is destroyed and save it in the parcelable
data.○ Each built in view if it has an id. It will restore some state. If you have a custom view you
will have to write your own restore state. Also you can not have multiple of the same view id used on the activity. So if you are dynamically adding the same view you will have to generate a dynamic id. Google how to do this.
○ You can also make it so your application never does configuration changes by adding configuration flags to all activities but that is pretty hacky
○ Some use this framework as well but it does not solve all your problemshttps://github.com/square/mortar
● Let me know after the talk if you know a good way to do this because. I think its annoying =)
Android Standard New Instance
● When creating things that need an instance created. Android generally creates a static method in the class that creates the new instance.
● Android does this when it you use the Activity generator
● This is so you do not have new instance code everywhere
Example New Instancepublic static UserFragment newInstance(PublicUser user) { UserFragment fragment = new UserFragment(); Bundle args = new Bundle(); args.putParcelable(PUBLIC_USER_TAG, user); fragment.setArguments(args); return fragment;}
Too Many abstract methods
● You can create a class that implements all the abstract methods so your code does not have empty methods
● A great example of this is the Text Watcher● Next slide shows the method that you would
implement instead of TextWatcher
Too Many abstract methods Exampleabstract public class BaseTextWatcher implements TextWatcher {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
}
}
Quick Tips
● You can click in logcat to keep your location○ If you dont do this it will scroll you to the bottom
● You can setup a proxy from your device to your computer with a tool such as fiddler or Charles
● You can right click on a png to create a 9 patch
Library Recommendations
● https://github.com/square● https://github.com/JakeWharton●