Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Android Linting Example #3931

Open
wants to merge 39 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
43ed8c7
add draft androoid linting
c0d33ngr Nov 10, 2024
8bae57e
add android linting example
c0d33ngr Nov 10, 2024
577375f
fix linting error in scala files
c0d33ngr Nov 10, 2024
5cab0a8
Merge branch 'main' into add-android-linting-example
c0d33ngr Nov 10, 2024
92a165e
Update AndroidAppModule.scala
c0d33ngr Nov 10, 2024
4e82c26
update code
c0d33ngr Nov 11, 2024
1d3805a
fix build error
c0d33ngr Nov 11, 2024
bd67ec5
add some changes
c0d33ngr Nov 17, 2024
6588076
Merge branch 'main' into add-android-linting-example
c0d33ngr Nov 17, 2024
c8f33f1
udpate code structure
c0d33ngr Nov 18, 2024
2c78931
udpate code
c0d33ngr Nov 18, 2024
1429e01
udpate code
c0d33ngr Nov 20, 2024
d0c7f11
Merge branch 'main' into add-android-linting-example
c0d33ngr Nov 20, 2024
9e21cee
Update build.mill
c0d33ngr Nov 22, 2024
60b1cbb
Update build.mill
c0d33ngr Nov 22, 2024
f6420eb
Update AndroidLintModule.scala
c0d33ngr Nov 22, 2024
0994b1a
fix format errors
c0d33ngr Nov 22, 2024
67deba8
fix formatting
c0d33ngr Nov 23, 2024
03c0381
restructure code
c0d33ngr Nov 23, 2024
bea5080
fix doc test errors
c0d33ngr Nov 24, 2024
8c32d89
Update build.mill
c0d33ngr Nov 24, 2024
1be883b
update cdoe
c0d33ngr Nov 25, 2024
2cae0ea
update cdoe
c0d33ngr Nov 25, 2024
428e7c0
update cdoe to add some defaults
c0d33ngr Nov 26, 2024
24d91e0
update doc to clean sepcific module
c0d33ngr Nov 26, 2024
3d01603
Update build.mill via GitHub web
c0d33ngr Nov 29, 2024
9585600
Update AndroidLintModule.scala via GitHub web
c0d33ngr Nov 29, 2024
df95eb3
Update AndroidLintModule.scala via GitHub web
c0d33ngr Nov 29, 2024
49d4717
update code
c0d33ngr Dec 9, 2024
54ded22
fix lint and test-doc errors
c0d33ngr Dec 9, 2024
b910ada
add abortOnError and baseline
c0d33ngr Dec 10, 2024
a6e2fc5
update code
c0d33ngr Dec 10, 2024
195a51e
remove old codes
c0d33ngr Dec 10, 2024
e7de9dc
update code
c0d33ngr Dec 10, 2024
20b4107
update code
c0d33ngr Dec 14, 2024
b4801fe
update code
c0d33ngr Dec 14, 2024
7e24224
fix error
c0d33ngr Dec 14, 2024
4133561
fix incrorect java suppress lint test
c0d33ngr Dec 14, 2024
44cbf19
update code
c0d33ngr Dec 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/modules/ROOT/nav.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
** xref:javalib/build-examples.adoc[]
** xref:javalib/web-examples.adoc[]
** xref:javalib/android-examples.adoc[]
** xref:javalib/android-linting.adoc[]
* xref:scalalib/intro.adoc[]
** xref:scalalib/builtin-commands.adoc[]
** xref:scalalib/module-config.adoc[]
Expand Down
14 changes: 14 additions & 0 deletions docs/modules/ROOT/pages/javalib/android-linting.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
= Linting Android Projects
:page-aliases: Linting_Android_Projects.adoc

include::partial$gtag-config.adoc[]

This page covers essential practices for maintaining and enforcing code quality
in Android projects using the Mill build tool. Proper linting helps detect
and resolve potential issues early, promoting better performance, security,
and user experience.


== Linting with Android cmdline lint tool

include::partial$example/javalib/android/2-linting.adoc[]
13 changes: 13 additions & 0 deletions example/javalib/android/2-linting/app/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.helloworld.app">
<uses-sdk android:minSdkVersion="9"/>
<uses-sdk android:targetSdkVersion="35"/>
<application android:label="Hello World" android:debuggable="true">
<activity android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.helloworld.app;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.view.ViewGroup.LayoutParams;
import android.view.Gravity;


public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Create a new TextView
TextView textView = new TextView(this);

// Set the text to "Hello, World!"
textView.setText("Hello, World!");

// Set text size
textView.setTextSize(32);

// Center the text within the view
textView.setGravity(Gravity.CENTER);

// Set layout parameters (width and height)
textView.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));

// Set the content view to display the TextView
setContentView(textView);
}
}
29 changes: 29 additions & 0 deletions example/javalib/android/2-linting/build.mill
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package build

import mill._
import mill.javalib.android.{AndroidSdkModule, AndroidLintModule}

// Create and configure an Android SDK module to manage Android SDK paths and tools.
object androidSdkModule0 extends AndroidSdkModule {
def buildToolsVersion = "35.0.0"
}

// Actual android application
object app extends AndroidLintModule {
def androidSdkModule = mill.define.ModuleRef(androidSdkModule0)
c0d33ngr marked this conversation as resolved.
Show resolved Hide resolved
}

/** See Also: app/AndroidManifest.xml */
/** See Also: app/src/main/java/com/helloworld/app/MainActivity.java */

/** Usage

> ./mill show app.androidLint # Display full path to the linting report in HTML
".../out/app/androidLint.dest/report.html"
c0d33ngr marked this conversation as resolved.
Show resolved Hide resolved

*/

// This Mill build configuration includes a linting step, which is essential for ensuring code
// quality and adhering to best practices in Android development projects. Running the `androidLint` task
// produces a detailed HTML report identifying potential issues in the code, such as performance,
// security, and usability warnings. This helps maintain the health and quality of the codebase.
1 change: 1 addition & 0 deletions scalalib/src/mill/javalib/android/AndroidAppModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -254,4 +254,5 @@ trait AndroidAppModule extends JavaModule {

PathRef(keystoreFile)
}

}
82 changes: 82 additions & 0 deletions scalalib/src/mill/javalib/android/AndroidLintModule.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package mill.javalib.android

import mill._
import mill.api.PathRef
import mill.scalalib.JavaModule
import mill.javalib.android.AndroidSdkModule

/**
* Android Lint Module for integrating the Android Lint tool in a Mill build.
*
* This module provides configuration options for executing Android Lint, including setting paths,
* specifying lint rules, managing reports, and more.
*/
@mill.api.experimental
trait AndroidLintModule extends AndroidSdkModule with JavaModule {
c0d33ngr marked this conversation as resolved.
Show resolved Hide resolved

/**
* Path to the project for which lint should run.
*/
def projectPath: T[PathRef] = Task.Source { millSourcePath }
c0d33ngr marked this conversation as resolved.
Show resolved Hide resolved

/**
* Specifies the file format of lint report.
*/
def lintReportFormat: T[String] = Task { "html" }
c0d33ngr marked this conversation as resolved.
Show resolved Hide resolved

/**
* Specifies the lint configuration XML file path. This allows setting custom lint rules or modifying existing ones.
*/
def lintConfigPath: T[Option[PathRef]] = Task { None }

/**
* Enable or disable warnings in the lint report.
*/
def warningsAsErrors: T[Boolean] = Task { false }

/**
* Additional options for lint (e.g., enabling/disabling specific checks).
*/
def lintOptions: T[Seq[String]] = Task { Seq("--check", "NewApi,InlinedApi") }
c0d33ngr marked this conversation as resolved.
Show resolved Hide resolved

/**
* Runs the Android Lint tool to generate a report on code quality issues.
*
* This method utilizes Android Lint, a tool provided by the Android SDK,
* to analyze the source code for potential bugs, performance issues, and
* best practices compliance. It generates an HTML report with the analysis
* results for review.
*
* The lint tool requires the Android SDK's command-line tools to be installed.
* The report is saved in the task's destination directory as "report.html".
*
* For more details on the Android Lint tool, refer to:
* [[https://developer.android.com/studio/write/lint]]
*/

def androidLint: T[PathRef] = Task {

val format = lintReportFormat()
val lintReport: os.Path = T.dest / s"report.$format"

// Map the report format to the corresponding lint command flag
val lintReportFlag = format match {
case "html" => "--html"
case "txt" => "--text"
case "xml" => "--xml"
case _ => throw new Exception(s"Unsupported report format: $format")
}

os.call(
Seq(
cmdlineToolsPath().path.toString + "/lint",
lintReportFlag,
lintReport.toString,
projectPath.toString
)
)

PathRef(lintReport)
}

}
8 changes: 8 additions & 0 deletions scalalib/src/mill/javalib/android/AndroidSdkModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ trait AndroidSdkModule extends Module {
PathRef(sdkPath().path / "build-tools" / buildToolsVersion())
}

/**
* Provides path to the Android cmdline tools for the selected version.
*/
def cmdlineToolsPath: T[PathRef] = Task {
installAndroidSdkComponents()
PathRef(sdkPath().path / "cmdline-tools" / "latest" / "bin")
}

/**
* Provides path to D8 Dex compiler, used for converting Java bytecode into Dalvik bytecode.
*/
Expand Down
Loading