分类 代码匠 下的文章

Java

https://google.github.io/styleguide/javaguide.html#s4.5.1-line-wrapping-where-to-break

  1. When a line is broken at a non-assignment operator the break comes before the symbol. (Note that this is not the same practice used in Google style for other languages, such as C++ and JavaScript.)
  • This also applies to the following "operator-like" symbols:

    • the dot separator (.)
    • the two colons of a method reference (::)
    • an ampersand in a type bound (<T extends Foo & Bar>)
    • a pipe in a catch block (catch (FooException | BarException e)).
  1. When a line is broken at an assignment operator the break typically comes after the symbol, but either way is acceptable.
  • This also applies to the "assignment-operator-like" colon in an enhanced for ("foreach") statement.
  1. A method or constructor name stays attached to the open parenthesis (() that follows it.
  2. A comma (,) stays attached to the token that precedes it.
  3. A line is never broken adjacent to the arrow in a lambda, except that a break may come immediately after the arrow if the body of the lambda consists of a single unbraced expression. Examples:
MyLambda<String, Long, Object> lambda =
    (String label, Long value, Object obj) -> {
        ...
    };

Predicate<String> predicate = str ->
    longExpressionInvolving(str);

Kotlin

https://developer.android.com/kotlin/style-guide#where_to_break

The prime directive of line-wrapping is: prefer to break at a higher syntactic level. Also:

  • When a line is broken at an operator or infix function name, the break comes after the operator or infix function name.
  • When a line is broken at the following "operator-like" symbols, the break comes before the symbol:

    • The dot separator (., ?.).
    • The two colons of a member reference (::).
  • A method or constructor name stays attached to the open parenthesis (() that follows it.
  • A comma (,) stays attached to the token that precedes it.
  • A lambda arrow (->) stays attached to the argument list that precedes it.

C++

https://google.github.io/styleguide/cppguide.html#Boolean_Expressions

When you have a boolean expression that is longer than the standard line length, be consistent in how you break up the lines.
if (this_one_thing > this_other_thing &&
    a_third_thing == a_fourth_thing &&
    yet_another && last_one) {
  ...
}
Note that when the code wraps in this example, both of the && logical AND operators are at the end of the line. This is more common in Google code, though wrapping all operators at the beginning of the line is also allowed. Feel free to insert extra parentheses judiciously because they can be very helpful in increasing readability when used appropriately, but be careful about overuse. Also note that you should always use the punctuation operators, such as && and ~, rather than the word operators, such as and and compl.

旧版本 Android 的模拟器可能出现调用 Canvas.clipPath() 导致 app 崩溃的现象。这是因为模拟器在过低的 API 级别下启用了 Canvas 的硬件加速1,在对应 API 级别下强制关闭硬件加速就好了。

E/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.UnsupportedOperationException
        at android.view.GLES20Canvas.clipPath(GLES20Canvas.java:408)
If your application is affected by any of these missing features or limitations, you can turn off hardware acceleration for just the affected portion of your application by calling setLayerType(View.LAYER_TYPE_SOFTWARE, null). This way, you can still take advantage of hardware acceleration everywhere else. See Control hardware acceleration for more information on how to enable and disable hardware acceleration at different levels in your application.

参考资料

给旧设备写 app 时遇到一个问题,在重写的 Application 类里使用 Java 8 特性会在运行时报错找不到类,而在其他地方使用都没问题。

E/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.NoClassDefFoundError: $r8$backportedMethods$utility$Objects$2$equals
        at MyApplication.onCreate(MyApplication.java:xx)

排查了一圈才发现是 multidex 惹的祸,由于执行 attachBaseContext() 需要先加载 MyApplication 类,此时 multidex 还没来得及安装,所以 MyApplication 里不能调用主 DEX 文件中不存在的类。

public class MyApplication extends SomeOtherApplication {
  @Override
  protected void attachBaseContext(Context base) {
     super.attachBaseContext(base);
     MultiDex.install(this);
  }
}

参考资料

https://developer.android.com/studio/write/java8-support#library-desugaring
https://developer.android.com/studio/build/multidex

先在 Prism 的下载页面选好想要的配置,复制到主题目录下。然后在主题的 header.php 里添加以下两行即可。

<link rel="stylesheet" href="<?php $this->options->themeUrl('prism.css'); ?>">
<script defer src="<?php $this->options->themeUrl('prism.js'); ?>"></script>