Share
Embed
I See Continuous Improvement
How setting up CI for your Android projects leads to continuous improvement of the codebase.
Ahmed El-Helw
April 13, 2018
Transcript
126 slides
-
How do we quickly find bugs before they make their
way into production? -
Why? • Maintain a stylistically consistent code base • Catch
common bugs and design flaws early on • Encourage a test driven culture • Build trust in the code quality -
“Checkstyle is a development tool to help programmers write Java
code that adheres to a coding standard.” -
“It automates the process of checking Java code to spare
humans of this boring (but important) task. This makes it ideal for projects that want to enforce a coding standard.” -
package ae.droidcon.checkstyle; class Example { static int add(int first, int
second) { return first + second; } } -
package ae.droidcon.checkstyle; class Example { static void awesome (int a,
int b) { System.out.println(a + ", " + b); } } -
• Avoid import java.util.*; • Import order • Modifier order
• Unnecessary parentheses Other Stylistic Issues -
class Example { int useless(int value) { value *= 2;
return value; } } -
package ae.droidcon.checkstyle; class Example { void play() { int fantasy
= 7; int offset = 0; System.out.println(fantasy + offset); offset += 3; System.out.println(fantasy + offset); } } -
package ae.droidcon.checkstyle; class Util { static void op() {} static
void secondOp() {} } -
[ant:checkstyle] [WARN] ~/code/experimentation/src/ main/java/ae/droidcon/checkstyle/Util.java:3:1: Utility classes should not have a
public or default constructor. [HideUtilityClassConstructor] -
object Caller { fun whoYouGonnaCall(context: Context) { val intent =
Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "GHOSTBUSTERS")) context.startActivity(intent) } } -
~/code/experiments/app/src/main/kotlin/ae/droidcon/ lint/Caller.kt:12: Error: Missing permissions required by intent Intent.ACTION_CALL: android.permission.CALL_PHONE
[MissingPermission] context.startActivity(intent) -
class BoringActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) setContentView(R.layout.demo) val button: ImageButton = findViewById(R.id.button) button.setOnClickListener { } } } -
~/code/experiments/app/src/main/java/ae/droidcon/lint/ BoringActivity.kt:14: Error: Unexpected implicit cast to ImageButton: layout tag
was Button [WrongViewCast] val button: ImageButton = findViewById(R.id.button) -
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.demo) val button: Button
= findViewById(R.id.button) button.setOnClickListener { } val drawable = ColorDrawable( R.color.accent_material_dark) button.background = drawable } -
~/code/experiments/app/src/main/java/ae/droidcon/ lint/BoringActivity.kt:20: Error: Call requires API level 16 (current min
is 14): android.view.View#setBackground [NewApi] button.background = drawable -
~/code/experiments/app/src/main/java/ae/droidcon/ lint/BoringActivity.kt:19: Error: Should pass resolved color instead of resource
id here: getResources().getColor(R.color.accent_material_dark) [ResourceAsColor] R.color.accent_material_dark) -
• WrongThread • LongLogTag • SQLiteString • … and many
more Additional Checks -
“Using Error Prone to augment the compiler’s type analysis, you
can catch more mistakes before they cost you time, or end up as bugs in production.” -
~/code/experimentation/src/main/java/ae/droidcon/ errorprone/Util.java:9: error: [UnusedCollectionModifiedInPlace] Collection is modified in place, but
the result is not used Collections.sort(new ArrayList<>(foos)); ^ (see http://errorprone.info/bugpattern/ UnusedCollectionModifiedInPlace) -
public class Carbon { public SimpleDateFormat dating() { return new
SimpleDateFormat("YYYY-MM-dd"); } } -
~/code/experimentation/src/main/java/ae/droidcon/ errorprone/Carb.java:7: error: [MisusedWeekYear] Use of "YYYY" (week year) in
a date pattern without "ww" (week in year). You probably meant to use "yyyy" (year) instead. return new SimpleDateFormat("YYYY-MM-dd"); ^ (see http://errorprone.info/bugpattern/ MisusedWeekYear) Did you mean 'return new SimpleDateFormat("yyyy- MM-dd");'? -
public class Finder { public boolean findWaldo(Set<Long> values) { return
(values.contains(1)); } } -
~/code/experimentation/src/main/java/ae/droidcon/ errorprone/Finder.java:7: error: [CollectionIncompatibleType] Argument '1' should not be passed
to this method; its type int is not compatible with its collection's type argument Long return (values.contains(1)); ^ (see http://errorprone.info/bugpattern/ CollectionIncompatibleType) -
public class PostApocolypse { private final Set<Human> theLastOfUs = World.getRemainingHumans();
private volatile int zombies = 9001; public void fightZombies() { zombies--; } } -
~/code/experimentation/src/main/java/ae/droidcon/ errorprone/PostApocolypse.java:9: warning: [NonAtomicVolatileUpdate] This update of a volatile variable
is non-atomic zombies--; ^ (see http://errorprone.info/bugpattern/ NonAtomicVolatileUpdate) 1 warning -
public String getFaction(int version) { String result; switch (version) {
case FALLOUT_2: case FALLOUT_NEW_VEGAS: result = "New California Rangers"; break; case FALLOUT_3: result = "The Enclave"; break; case FALLOUT_4: result = "Institute"; default: result = "War never changes"; } return result; } -
~/code/experimentation/src/main/java/ae/droidcon/ errorprone/Fallout.java:17: warning: [FallThrough] Execution may fall through from the
previous case; add a `// fall through` comment before this line if it was deliberate default: ^ (see http://errorprone.info/bugpattern/ FallThrough) 1 warning -
public class TemplarBase { protected Assassin ezio; public Creed getCreed()
{ return ezio.getCreed(); } } public class TemplarCastle extends TemplarBase { protected Assassin ezio; } -
public class TemplarBase { protected Assassin ezio; public Creed getCreed()
{ return ezio.getCreed(); } } public class TemplarCastle extends TemplarBase { protected Assassin ezio; } -
~/code/experimentation/src/main/java/ae/droidcon/ errorprone/TemplarCastle.java:4: warning: [HidingField] Hiding fields of superclasses may cause
confusion and errors. This field is hiding a field of the same name in superclass: TemplarBase protected Assassin ezio; ^ (see http://errorprone.info/bugpattern/ HidingField) 1 warning -
class Persona { private final Mementos mementos = new Mementos();
Shadow getShadowForPerson(String person) { if (mementos.hasShadowFor(person)) { return new Shadow(person); } return null; } void destroyShadow(String person) { final Shadow shadow = getShadowForPerson(person); shadow.unshadow(); } } -
class Persona { private final Mementos mementos = new Mementos();
Shadow getShadowForPerson(String person) { if (mementos.hasShadowFor(person)) { return new Shadow(person); } return null; } void destroyShadow(String person) { final Shadow shadow = getShadowForPerson(person); shadow.unshadow(); } } -
Found 1 issue Persona.java:13: error: NULL_DEREFERENCE object `shadow` last assigned
on line 12 could be null and is dereferenced at line 13. 11. void destroyShadow(String person) { 12. final Shadow shadow = getShadowForPerson(person); 13. > shadow.unshadow(); 14. } 15. } -
• Context leaks • Resource leaks • Unsafe @GuardedBy access
Additional Checks -
Hitting the real APIs • Unreliable • Can’t test edge
cases • How to test payment related scenarios -
Hitting the real APIs • Unreliable • Can’t test edge
cases • How to test payment related scenarios -
class RecordCallsInterceptor constructor(context: Context, val gson: Gson) : Interceptor {
... override fun intercept(chain: Interceptor.Chain): Response { val request = chain.request() val response = chain.proceed(request) ... val recordModel = ApiRecordModel( request = ApiRecordRequestModel( path = request.url().encodedPath(), method = request.method(), headers = requestHeaders, body = requestBody ), response = ApiRecordResponseModel( code = response.code(), headers = responseHeaders, body = responseBody ) ) ... dumpToServer(recordModel) return response } } -
class RecordCallsInterceptor constructor(context: Context, val gson: Gson) : Interceptor {
... override fun intercept(chain: Interceptor.Chain): Response { val request = chain.request() val response = chain.proceed(request) ... val recordModel = ApiRecordModel( request = ApiRecordRequestModel( path = request.url().encodedPath(), method = request.method(), headers = requestHeaders, body = requestBody ), response = ApiRecordResponseModel( code = response.code(), headers = responseHeaders, body = responseBody ) ) ... dumpToServer(recordModel) return response } } -
class RecordCallsInterceptor constructor(context: Context, val gson: Gson) : Interceptor {
... override fun intercept(chain: Interceptor.Chain): Response { val request = chain.request() val response = chain.proceed(request) ... val recordModel = ApiRecordModel( request = ApiRecordRequestModel( path = request.url().encodedPath(), method = request.method(), headers = requestHeaders, body = requestBody ), response = ApiRecordResponseModel( code = response.code(), headers = responseHeaders, body = responseBody ) ) ... dumpToServer(recordModel) return response } } -
class RecordCallsInterceptor constructor(context: Context, val gson: Gson) : Interceptor {
... override fun intercept(chain: Interceptor.Chain): Response { val request = chain.request() val response = chain.proceed(request) ... val recordModel = ApiRecordModel( request = ApiRecordRequestModel( path = request.url().encodedPath(), method = request.method(), headers = requestHeaders, body = requestBody ), response = ApiRecordResponseModel( code = response.code(), headers = responseHeaders, body = responseBody ) ) ... dumpToServer(recordModel) return response } } -
class RecordCallsInterceptor constructor(context: Context, val gson: Gson) : Interceptor {
... override fun intercept(chain: Interceptor.Chain): Response { val request = chain.request() val response = chain.proceed(request) ... val recordModel = ApiRecordModel( request = ApiRecordRequestModel( path = request.url().encodedPath(), method = request.method(), headers = requestHeaders, body = requestBody ), response = ApiRecordResponseModel( code = response.code(), headers = responseHeaders, body = responseBody ) ) ... dumpToServer(recordModel) return response } } -
We save them as a Postman Collection • Nice GUI
• Easy to edit responses and search requests • Supports placeholders for path, headers or body -
fun mount(collectionName: String, fileName: String) { cachedCollections[collectionName] = file.item.firstOrNull {
collection -> collection.name==collectionName }?: throw IllegalArgumentException("Collection not found: $collectionName") } -
fun mount(collectionName: String, fileName: String) { val file = PostmanLoader.load(fileName)
cachedCollections[collectionName] = file.item.firstOrNull { collection -> collection.name == collectionName }?: throw IllegalArgumentException("Collection not found: $collectionName") } -
fun mount(collectionName: String, fileName: String) { val file = PostmanLoader.load(fileName)
cachedCollections[collectionName] = file.item.firstOrNull { collection -> collection.name == collectionName }?: throw IllegalArgumentException("Collection not found: $collectionName") } -
MockWebServer().apply { setDispatcher(object : Dispatcher() { override fun dispatch(request: RecordedRequest):
MockResponse { return mountedCollections .firstOrNull { requestsMatch(request, it.request) }?.let { createResponseFromPostman(it.response[0]) } ?: MockResponse().apply { setResponseCode(404) } } }) -
MockWebServer().apply { setDispatcher(object : Dispatcher() { override fun dispatch(request: RecordedRequest):
MockResponse { return mountedCollections .firstOrNull { requestsMatch(request, it.request) }?.let { createResponseFromPostman(it.response[0]) } ?: MockResponse().apply { setResponseCode(404) } } }) -
MockWebServer().apply { setDispatcher(object : Dispatcher() { override fun dispatch(request: RecordedRequest):
MockResponse { return mountedCollections .firstOrNull { requestsMatch(request, it.request) }?.let { createResponseFromPostman(it.response[0]) } ?: MockResponse().apply { setResponseCode(404) } } }) -
MockWebServer().apply { setDispatcher(object : Dispatcher() { override fun dispatch(request: RecordedRequest):
MockResponse { return mountedCollections .firstOrNull { requestsMatch(request, it.request) }?.let { createResponseFromPostman(it.response[0]) } ?: MockResponse().apply { setResponseCode(404) } } }) -
@Test public void loginTest_success() { mount("defaultCollection"); screenshot("loginTest", "success", "emptyFields", lang);
onView(withId(R.id.email)).perform(typeText("[email protected]")); onView(withId(R.id.edt_password)).perform(typeText("Password"), closeSoftKeyboard()); screenshot("loginTest", "success", "filledFields", lang); onView(withId(R.id.btn_login_with_fb)).perform(click()); screenshot("loginTest", "success", "loading", lang); } -
@Test public void loginTest_success() { mount("defaultCollection"); screenshot("loginTest", "success", "emptyFields", lang);
onView(withId(R.id.email)).perform(typeText("[email protected]")); onView(withId(R.id.edt_password)).perform(typeText("Password"), closeSoftKeyboard()); screenshot("loginTest", "success", "filledFields", lang); onView(withId(R.id.btn_login_with_fb)).perform(click()); screenshot("loginTest", "success", "loading", lang); } -
What to do with screenshot? • Upload them to your
server • Tag them with configuration, test, scenario, date • Run image diffing (ie: http://www.imagemagick.org/ Usage/compare/) • Flag different/new screenshots for manual verification -
Making it cross platform • Share API data • Run
a python mock server instead of OkHttp • Share screenshot diff-ing logic/UI -
aapt d permissions ./app/build/outputs/apk/debug/app-debug.apk uses-permission: name='android.permission.INTERNET' uses-permission: name='android.permission.WRITE_EXTERNAL_STORAGE' uses-permission: name='android.permission.ACCESS_NETWORK_STATE'
uses-permission: name='android.permission.WAKE_LOCK' uses-permission: name='android.permission.READ_EXTERNAL_STORAGE' -
› diff before after 6a7,9 > uses-permission: name='android.permission.ACCESS_WIFI_STATE' > uses-permission:
name='android.permission.RECORD_AUDIO' > uses-permission: name='android.permission.MODIFY_AUDIO_SETTINGS' Picture from principedifino -
> Task :app:countDebugDexMethods Total methods in app-debug.apk: 52673 (80.37% used)
Total fields in app-debug.apk: 30053 (45.86% used) Total classes in app-debug.apk: 6962 (10.62% used) Methods remaining in app-debug.apk: 12862 Fields remaining in app-debug.apk: 35482 Classes remaining in app-debug.apk: 58573 -
> Task :app:countDebugDexMethods Total methods in app-debug.apk: 69802 (106.51% used)
Total fields in app-debug.apk: 42438 (64.76% used) Total classes in app-debug.apk: 9291 (14.18% used) Methods remaining in app-debug.apk: 0 Fields remaining in app-debug.apk: 23097 Classes remaining in app-debug.apk: 56244 -
What else can we do? •Upload apk to S3 bucket
for internal distribution •Automatically upload builds to Google Play •Post on slack about new builds -
lane :alpha do gradle(task: "clean assembleDebug test android") aws_s3 slack({
message: "new version uploaded to S3", channel: “#qa" }) end -
lane :alpha do gradle(task: "clean assembleDebug test android") aws_s3 slack({
message: "new version uploaded to S3", channel: “#qa" }) end -
lane :alpha do gradle(task: "clean assembleDebug test android") aws_s3 slack({
message: "new version uploaded to S3", channel: “#qa" }) end -
lane :alpha do gradle(task: "clean assembleDebug test android") aws_s3 slack({
message: "new version uploaded to S3", channel: “#qa" }) end -
lane :alpha do gradle(task: "clean assembleDebug test android") aws_s3 slack({
message: "new version uploaded to S3", channel: “#qa" }) end -
lane :release do gradle(task: "clean assembleRelease test android") supply slack({
message: "new version released", channel: “#announcements" }) end -
lane :release do gradle(task: "clean assembleRelease test android") supply slack({
message: "new version released", channel: “#announcements" }) end -
Compilation time Pull request time Commit time Post-commit time Photo
by Ales Krivec on Unsplash -
Photo by Evan Kirby on Unsplash Gotchas • Flaky Tests
• Tool version updates • Unstable environment • Crashing tests -
• Invest in improving build times • Set up static
analysis tools • Optimize for code quality and stability Summary -
References • https://speakerdeck.com/kaskasi/i-am-on-a-fastlane-to-hell • https://medium.com/square-corner-blog/surfacing-hidden- change-to-pull-requests-6a371266e479 • https://github.com/KeepSafe/dexcount-gradle-plugin • https://github.com/JakeWharton/dex-method-list
• http://www.imagemagick.org/Usage/compare/ • https://android.github.io/kotlin-guides/interop.html