Skip to content

Ider

沉淀我所学习,累积我所见闻,分享我所体验

Primary Navigation Menu

Menu
  • Home
  • Why Ider?
  • How Ider?

English Posts(英文写作)

2021-01-14
January 14 2021
On Thursday
In Design Patterns(设计模式), English Posts(英文写作), Knowledge Base(心得笔库), Mobile Development(移动开发), Software Engineering(软件工程)

ProtoBuf 2.0 method count optimization for android development

The Protocol Buffer library was not initially built for Android, it just turned out to have a Java version to be used in Android, but it’s not optimized for Android apps, for example: it doesn’t consider the method count limit in Android.

So, in this article, I’d like to share some work I found specifically on Protocol Buffer Version 2 that can reduce the method count. If your app is also heavily relying on Protocol Buffer, I hope these approaches are useful for you too.

General Approaches

1. Use Protocol Buffer Java Lite Runtime

Just as the name indicates, the dependency library is much smaller than the regular Protocol Buffer Java runtime, the generated code is also much slimmer. However, the APIs are compatible between those two versions, so the call sites would not be affected when changing the library.

2. Don’t use <Message>OrBuilder interface

For each Protocol Buffer message definition in .proto file, the Protocol Buffer compiler generates an interface named <Message>OrBuilder (<Message> is the name defined in .proto file). This interface would be implemented by the concrete <Message> class and the corresponding Builder.

It might be attractive to use it as a variable type thereby you can depend on abstractions to not concrete classes. But calling methods on Java interface would make Dex take count of those methods.

In reality, every place can directly use either <Message> or Builder, then the optimization tool (like R8, ProGuard) can safely remove the methods declared on the interface.

Special Tricks

Protocol Buffer Java Lite is very good, but if I open the magic box of generated Java classes, I notice it’s still not optimal for Android. There are a couple places I could modify to make it more effective for Android applications.

Instead of copying the Java files and making the change, which is error prone when engineers update the .proto file, I created a script to automate the job. Just add the execution of this script at the end of the Protocol Buffer gradle task, so it works just as a complement of Protocol Buffer code generation process.

  1. protobuf {
  2. protoc {
  3. artifact = 'com.google.protobuf:protoc:3.7.0'
  4. }
  5. plugins {
  6. javalite {
  7. artifact = 'com.google.protobuf:protoc-gen-javalite:3.0.0'
  8. }
  9. }
  10. generateProtoTasks {
  11. all().each { task ->
  12. task.builtins {
  13. remove java
  14. }
  15. task.plugins {
  16. javalite { }
  17. }
  18.  
  19. // `getOutputDir()` can only be read during configuration,
  20. // so it’s out of action block
  21. def outputDir = task.getOutputDir(task.plugins[0])
  22. task.doLast {
  23. exec {
  24. commandLine file('protobuf-optimizer.py')
  25. args outputDir
  26. }
  27. }
  28. }
  29. }
  30. }

In the script, I have made the following modification on generated java code.

1. Change the modifier of Builder constructor from private to package

Simply running the sample addressbook.proto from Protocol Buffer tutorial side, you would get a class like the following snippet:

  1. public static final class Person {
  2. private Person() {}
  3.  
  4. protected final Object dynamicMethod(
  5. com.google.protobuf.GeneratedMessageLite.MethodToInvoke method,
  6. Object arg0, Object arg1) {
  7. switch (method) {
  8. case NEW_BUILDER: {
  9. return new Builder();
  10. }
  11. }
  12. }
  13.  
  14. public static final class Builder {
  15. private Builder() {}
  16. }
  17.  
  18. }

When analyzing the APK file, we could would find some synthetic classes and methods:

This is because the outer class is accessing the private constructor of the inner class. The generated synthetic class would contribute an extra constructor to the Dex file. Therefore, by simply removing the private from Builder(), you can remove such classes and methods.
Read More →

Share on Facebook
Facebook
Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin
Email this to someone
email
2018-12-16
December 16 2018
On Sunday
In English Posts(英文写作), Knowledge Base(心得笔库), Programming Life(程序人生)

Insights from 100 Interviews

As our interview status recorded, I just have conducted 100 interviews at Facebook. I never did batch interviews (multiple interviews a day in consecutive days), so I’m feeling really excited about this accumulative achievement.
On this wonderful milestone, I’d like to share some of my experience and tips about being interviewer at Facebook, I hope you found this helpful.

Benefits as an Interviewer

There is no doubt that interviews are critical for company growth. However, I want to tell you more about the personal growth and benefits of being an Android interviewer.

  • Improving my verbal and written English
    Since English is my second language, interview is a great way for me to practice. I repeat similar conversation during interview, and this exposure increases my confidence in my speech; by providing post-interview feedback, I have increased the speed and quality of my writing.
  • Expanding my social network
    I felt so great when some colleagues suddenly stopped me and asked me “do you know you interviewed me?” then our conversation started very naturally (although, I’m really sorry, I didn’t actually remember the interview).
  • Business travel for recruiting events
    I got a chance to go back to my alma mater to give recruiting talk, which I previously never imagined. However, I also missed a couple trips to interview abroad due to my visa restriction.
  • Clarity on my work and teams
    Candidates often asked about my work and products at Facebook at the end of interviews. By thinking about and answering those questions, I clearly define my own duties, and the work of my team.
  • Polishing my technical skills and general Android knowledge
    The technical skills we use here are very Facebook specific, but I would hear a lot of different terminologies, libraries, architectures that are used by candidates. I can then follow up on these to keep up with the market.
  • Helping my friends apply for software engineer positions
    It’s fine to help friends to prepare for Facebook interviews, as long as we don’t leak the real questions we ask. So I can specifically tell my friends what interviews look like, and what skills they should focus on.
  • Better understand position and level requirements
    After seeing so many interview decisions, I get a better sense on the requirements for Android positions at each level. This also clarifies what gaps I need to fill to advance my own career.
  • Performance review and career growth
    To be honest, the number of interviews doesn’t play a key role on performance review at company. However all the benefits mentioned above helps me a lot on my growth.
Share on Facebook
Facebook
Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin
Email this to someone
email
2016-05-25
May 25 2016
On Wednesday
In Article Collection(聚宝收藏), English Posts(英文写作), Reading Notes(阅而后知)

Universal Principles of Design

universal-principles-of-design

As a front-end engineer, or specifically, an Android UI Engineer, I have a lot of time to work with designers. But there are a lot of concepts in design I am not familiar with. So I asked them how I can learn design, but not being a designer. Then they recommended this book to me.

I read it, and feel it’s really helpful and useful. It’s not only about UI design, many principles can apply to other designs as well: software design, landscape design, product design, option design. After read this book, I learned the explanation on how certain design decisions are made.

I would definitely forgot most principles, so I wrote this post to transcribe all principles and short description from the book, later I can just come here to recall them.
Read More →

Share on Facebook
Facebook
Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin
Email this to someone
email
2015-11-26
November 26 2015
On Thursday
In English Posts(英文写作), Mobile Development(移动开发)

2015 Android Dev Summit Schedule and Videos

ads
Here is the full schedule of Android Dev Summit. If you missed the live stream, you can find the YouTube Play List here. You can find the video link for each session from the title in the following schedule table as well.
The table also contains the speakers of each session, and their Twitter accounts are attached to their names. If you like to know more about Android Development, you can follow them and @AndroidDev.

Day 1

9:05am – 9:45am

Keynote

Dave Burke & Stephanie Saad Cuthbertson

9:45am – 10:30am

Android Application Architecture

Yigit Boyar & Adam Powell

10:30am – 11:00am Coffee Break
11:00am – 11:45am

Respecting User Attention: Notification Best Practices

Chris Wren

Secrets To App & Game Success On Google Play

Brian Quimby & Andreas Preuer

11:45am – 12:30pm

Keep It Secret, Keep It Safe

Chad Brubaker

Android Textual Layout

Raph Levien

12:30pm – 1:30pm Lunch
1:30pm – 2:15pm

RecyclerView Animations And Behind The Scenes

Chet Haase & Yigit Boyar

2:15pm – 3:00pm

Support Library: Guts And Glory

Chris Banes & Adam Powell

3:00pm – 3:30pm Office Hours
3:30pm – 4:15pm

“Mother, May I?” Asking For Permissions

Svet Ganov & Jeff Sharkey

4:15pm – 5:00pm

Framework Fireside Chat

Hosted By Dan Galpin

Read More →

Share on Facebook
Facebook
Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin
Email this to someone
email
2014-03-31
March 31 2014
On Monday
In Data Structures(数据结构), English Posts(英文写作), Language Tips(语言初试), Mobile Development(移动开发)

Introduction to Loc (Lua Objective-C)

Recently we were starting to use Lua in our game. Lua is a very popular scripting language in the game industry. It’s written in pure C and is very lightweight. We can simply add Lua source code to our project, and it is very easy to compile and run.

However, in practice we encountered issues with type conversion between Lua and Objective-C. In the beginning, we followed instructions to create C functions, and inside the functions we called Objective-C methods, then registered those functions into Lua State as library. When requirements went more complicated, we have to create a bunch of C functions, which are redundancy in a lot of places. This is really not an ideal to bridge between Lua and Objective-C.

After reading the Lua instructions for two days, and with knowledge about iOS7 JavaScript Framework, I decided to create a similar framework to handle type conversion between these two languages. The good thing is Objective-C is a superclass of C, and Lua is naturally compiled from C, this makes it very easy to construct a bridge and make them communicating with each other. With reflection from Objective-C, it’s also very convenient to call Objective-C methods from Lua.

Loc (Lua Objective-C) is the framework I created to convert type between Lua and Objective-C for our project. It’s as simple as Lua, with only 6 packages. It reduces a lot of time to create functions mapping Lua call, and it works pretty well so far.

With the permission from my company, I shared the slides that I presented to my colleagues about the design and syntax about Loc. I am trying to persuade my managers to make it open source, so more developers could use it to prove the usability and make the framework more mature.

Share on Facebook
Facebook
Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin
Email this to someone
email
2014-01-17
January 17 2014
On Friday
In Article Collection(聚宝收藏), English Posts(英文写作), Language Tips(语言初试)

Redis Command Reference

Redis is really awesome key-value storage. Not only because it is very fast on data get/set as all data are stored in memory, but also it is very simple to setup even from source code, it has multiple data structures that can be used in practice, and it is well documented, many useful resources can be found in Redis.io.

The most valuable thing of Redis should be the commands that communicate with Redis server to operate on data. On Redis.io, there is page fully explained the functionality of each commands, it also provides some interactive page to try each command and know them well.

Here I restyle the commands page, so that they could be print out in two papers, and also highlighted them with different color according to their type. You can download the PDF file here as well.

Share on Facebook
Facebook
Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin
Email this to someone
email
2012-04-13
April 13 2012
On Friday
In Algorithm Analysis(算法分析), English Posts(英文写作), Knowledge Base(心得笔库), Mathematical Theory(数学理论)

String Redution问题的分析

这是一道关于字符串的操作的问题,一开始思考的时候,感觉需要取出各种不同的子序列,如果是这样时间复杂度就会变成指数级别。不过在朋友的指示下,发现其实它是有规律可寻的,最后的算法时间负责度只要O(n),而且代码极其简单。

虽然朋友是直接告诉了我其中的规律,但是我还是花了点时间思考和证明了一下,才放心的写下了代码。现在把它总结在这里。

整个问题的证明基本是使用数学归纳法,以及奇数和偶数的一些性质。感叹数学的世界真奇妙。也庆幸自己一直以来我十分喜欢数学。其实计算机的世界本来就是建立在各种数学理论之上,比如离散数学,模糊数学,逻辑数学。但是计算机又让数学的应该变得更加的广阔。

(你可以在看完题之后直接跳到红色的部分看这个问题的结论。)
Read More →

Share on Facebook
Facebook
Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin
Email this to someone
email
Facebook
Twitter
LinkedIn
RSS
ZhiHu

Recent Posts

  • ProtoBuf 2.0 method count optimization for android development
  • 面过100场行为面试后
  • The Coaching Habit
  • 基础数据类型的包装类的内存大小
  • 下载Google Photos上的所有图片
  • 婚礼誓词
  • 科技公司的技术博客清单

Categories

  • Algorithm Analysis(算法分析)
  • Article Collection(聚宝收藏)
  • Data Structures(数据结构)
  • Design Patterns(设计模式)
  • English Posts(英文写作)
  • Front Interface(界面构想)
  • IT Products(数码产品)
  • Knowledge Base(心得笔库)
  • Language Tips(语言初试)
  • Mathematical Theory(数学理论)
  • Mobile Development(移动开发)
  • Programming Life(程序人生)
  • Reading Notes(阅而后知)
  • Software Engineering(软件工程)
  • Special Tricks(奇技妙招)
  • Tangential Speech(漫话杂谈)

Tags

Aero Android API Bash Binary Search Bitwise Operation Book C/C++ Career Chrome Conference CSS Debug Device DOM Extension Framework Gradle Hearthstone HTML Initialization Interview iOS Java JavaScript jQuery Keyword Language Issues Mac Microsoft Mobile Modifier Objective-C PHP Preference Principle Reference Regular Expression Static String SVG Tools Tutorial UI XML

Blogroll

  • Ahmed's Blog
  • Gert Lombard's Blog
  • Gordon Luk
  • Jack & Allison
  • 开发部落

Archives

Copyright © 2021 Ider. Designed using Chromatic WordPress Theme. Powered by WordPress.