AOSP development, static code analyzer on android module. (Part 1)

Wildan Garviandi
3 min readJun 6, 2024

--

Photo by Waldemar on Unsplash

Well this time I will share something that less developer will looking to it but this I feel like my achievement after deep dive on AOSP (Android Open Source Project) not as simple as creating android app and publish to the playstore but AOSP is a android OS it self. Not to mention also AOSP topic like this is niece topic. Let’s get started.

One of the well known static code analyzer being used on android project is detekt. because of that I’ll try to use it on my project, not through gradle as a normal android project, but through cli command that I will bind it on make script of android module .mk .

Know how to get detekt jar binary

From official documentation there is several way how can you get detekt through cli command but for this purposes, getting started on this given link https://detekt.dev/docs/gettingstarted/cli#any-os will help us to get .jar file that soon we will execute it.

gomod YOUR_MODULE_NAME
export VERSION = "1.23.6"
curl -sSLO https://github.com/detekt/detekt/releases/download/v<VERSION>/detekt-cli-<VERSION>-all.jar

Run detekt command and get report of violation

after make sure you saw the .jar file downloaded on where your module is placed, let execute the command to run detekt and generate report. but! before that you need to know what kind of rules that you or your team might already agree with starting from looking to this default rules : detekt default rules. Sure it might not right away suite your style let’s make one rule file that suite your needs to a file called detekt-config.yaml for example,

build:
maxIssues: 0
weights:
# complexity: 2
# LongParameterList: 1
# style: 1
# comments: 1

processors:
active: true
exclude:
# - 'FunctionCountProcessor'
# - 'PropertyCountProcessor'
# - 'ClassCountProcessor'
# - 'PackageCountProcessor'
# - 'KtFileCountProcessor'

console-reports:
active: true
exclude:
# - 'ProjectStatisticsReport'
# - 'ComplexityReport'
# - 'NotificationReport'
# - 'FindingsReport'
# - 'BuildFailureReport'
naming:
active: true
ClassNaming:
active: true
classPattern: '[A-Z][a-zA-Z0-9$]*'
ConstructorParameterNaming:
active: true
parameterPattern: '[a-z][A-Za-z0-9]*'
privateParameterPattern: '[a-z][A-Za-z0-9]*'
excludeClassPattern: '$^'
............

now let’s make sure to continuous adjust the rules we run detekt on our project against that config file

java -jar detekt-cli-1.23.6-all.jar -c default-detekt-config.yml
sample run result : https://detekt.dev/docs/1.21.0/intro

Report on pretty format! I can’t give CLI print to the team to fix it!

Sure, let’s cover a bit more by adding some more params to export the run result into HTML so you can proof to your PO/team that your code is pretty.

java -jar detekt-cli-1.23.6-all.jar -c default-detekt-config.yml -r html:reports/detekt.html

by default if you are not specifying the path to where you want the report to be placed, it will be creating a folder called reports and inside it you should found an html file containing all the result but more readable.

https://detekt.dev/docs/introduction/reporting

after you have confidence that this is the base report that you will maintain let’s configure to make all of this job run when you build the module.

Integrate on android module compile

on your .mk project file on the line before PRODUCT_PACKAGES += YOUR_MODULE_NAME you can definitely run some shell command

$(shell curl -sSLO https://github.com/detekt/detekt/releases/download/v1.23.6/detekt-cli-1.23.6-all.jar)
$(shell java -jar detekt-cli-1.23.6-all.jar -c default-detekt-config.yml -r html:reports/detekt.html)

PRODUCT_PACKAGES += YOUR_MODULE_NAME
..............

Congratulation now when you try to build your module, it will also generate detekt report! 🎉.

This is nice, but how can I make the build failed when there is too much violation?

Good question! I’ll cover it on the next article, stay tune! and see you around!

--

--

Wildan Garviandi

Software developer, Mobile dev wizard, Backend dev for fun, UX/UI Researcher, and new skill Machine Learning engineer FTW!