Documentation
¶
Overview ¶
Package androidtool builds, installs and signs Android apps for glutter-android (SPEC §24.1): it finds the SDK and the NDK, generates the manifest, runs the Go toolchain once per ABI, links the resources with aapt2, packs the APK with the embedding's classes.dex, aligns and signs it. It runs on the developer's machine, never inside an app, and needs no Gradle, Android Studio, AndroidX or Kotlin.
The Java classes of the embedding are in java/ and their compiled form is the checked-in classes.dex, which `glutter-gen android-dex` makes (D22).
Flutter: no counterpart (flutter build apk)
Index ¶
- Constants
- Variables
- func ABITriple(abi string) string
- func ApplicationIDValid(s string) bool
- func CompileDex(ctx context.Context, run Runner, sdk *SDK, javaDir, out string) error
- func DefaultAppID(pkg string) string
- func DefaultIcon() ([]byte, error)
- func DefaultLabel(pkg string) string
- func FindBuildTools(home string) (string, error)
- func FindHome(getenv func(string) string) (string, error)
- func FindNDK(getenv func(string) string) (string, error)
- func FindPlatform(home string) (string, error)
- func Manifest(c ManifestConfig) []byte
- func ShellQuote(s string) string
- type ABI
- type ADB
- func (a ADB) ABI(ctx context.Context) (ABI, error)
- func (a ADB) Devices(ctx context.Context) ([]Device, error)
- func (a ADB) Install(ctx context.Context, apk string) error
- func (a ADB) Output(ctx context.Context, args ...string) (string, error)
- func (a ADB) Pid(ctx context.Context, appID string) (int, error)
- func (a ADB) PullAppFile(ctx context.Context, appID, path string) ([]byte, error)
- func (a ADB) RemoveAppFile(ctx context.Context, appID, path string) error
- func (a ADB) Screencap(ctx context.Context) ([]byte, error)
- func (a ADB) Start(ctx context.Context, appID string, extras map[string]string) error
- func (a ADB) StreamLog(ctx context.Context, w io.Writer, appID string, startWait time.Duration) error
- type BuildOptions
- type BuildResult
- type Command
- type DebugKey
- type Device
- type ExecRunner
- type ManifestConfig
- type Runner
- type SDK
Constants ¶
const ( // TagAndroidNoDevice is the tag of the error when no device is connected. TagAndroidNoDevice = "AndroidNoDevice" // TagAndroidManyDevices is the tag of the error when several devices are connected and // none was named; the metadata "devices" lists them. TagAndroidManyDevices = "AndroidManyDevices" // TagAndroidDeviceNotFound is the tag of the error for a named device that is not // connected, or not ready. TagAndroidDeviceNotFound = "AndroidDeviceNotFound" // TagAndroidABIUnsupported is the tag of the error for a device whose ABIs glutter-android // does not build. TagAndroidABIUnsupported = "AndroidABIUnsupported" // TagAndroidStartFailed is the tag of the error when the activity does not start. TagAndroidStartFailed = "AndroidStartFailed" // TagAndroidLogcatFailed is the tag of the error when logcat cannot be read. TagAndroidLogcatFailed = "AndroidLogcatFailed" )
The tags of the errors that this file makes (rm4n0s/errors, R5).
const ( // TagAndroidBuildInvalid is the tag of the error for options that cannot make an APK: no // package, an unknown ABI, a release build without a key. TagAndroidBuildInvalid = "AndroidBuildInvalid" // TagAndroidWriteFailed is the tag of the error for a file of the build that cannot be // read or written; the metadata "path" or "dir" says which. TagAndroidWriteFailed = "AndroidWriteFailed" )
The tags of the errors that this file makes (rm4n0s/errors, R5).
const ( // NDKPackage is the sdkmanager package of the NDK that the tool was tested with. NDKPackage = "ndk;30.0.16248370" // BuildToolsPackage is the sdkmanager package of the build tools. BuildToolsPackage = "build-tools;37.0.0" // PlatformPackage is the sdkmanager package of the platform whose android.jar is built against. PlatformPackage = "platforms;android-37.2" // MinSDK is the minimum API level (D24). MinSDK = 26 // TargetSDK is the target API level. TargetSDK = 37 )
The versions the installation instructions name (SPEC §24.1).
const ( // TagAndroidSDKNotFound is the tag of the error for a missing SDK ($ANDROID_HOME); the // metadata "install" says how to get it. SPEC §24.1. TagAndroidSDKNotFound = "AndroidSDKNotFound" // TagAndroidNDKNotFound is the tag of the error for a missing NDK; the metadata "install" // has the sdkmanager command. TagAndroidNDKNotFound = "AndroidNDKNotFound" // TagAndroidBuildToolsNotFound is the tag of the error for missing build tools; the // metadata "install" has the sdkmanager command. TagAndroidBuildToolsNotFound = "AndroidBuildToolsNotFound" // TagAndroidPlatformNotFound is the tag of the error for a missing platform; the metadata // "install" has the sdkmanager command. TagAndroidPlatformNotFound = "AndroidPlatformNotFound" )
The tags of the errors that this file makes (rm4n0s/errors, R5).
const ActivityClass = "io.github.rm4n0s.glutter.GlutterActivity"
ActivityClass is the activity of the embedding.
const ConfigChanges = "" /* 126-byte string literal not displayed */
ConfigChanges are the configuration changes the activity handles itself, so that turning the device or changing the theme does not recreate it: Flutter's list.
Flutter: shell/platform/android/io/flutter/embedding/android/FlutterActivity.java (the manifest meta-data it documents)
const JavaDir = "internal/androidtool/java"
JavaDir is the directory of the embedding's Java sources, relative to the module root.
const ReceiverClass = "io.github.rm4n0s.glutter.NotificationReceiver"
ReceiverClass is the broadcast receiver of the embedding: it hears the swipe of a notification (SPEC §24.11). It is not exported, so only the system's PendingIntent reaches it.
const TagAndroidCommandFailed = "AndroidCommandFailed"
TagAndroidCommandFailed is the tag of the error for a program of the build that cannot be started or exits with a failure; the metadata "command" and "output" say which and why.
const TagAndroidJavaNotFound = "AndroidJavaNotFound"
TagAndroidJavaNotFound is the tag of the error for a missing javac, which is needed only to change the Java of the embedding; the metadata "install" has the command.
const TagAndroidKeyFailed = "AndroidKeyFailed"
TagAndroidKeyFailed is the tag of the error for a debug key that cannot be made or written.
const TagAndroidPullFailed = "AndroidPullFailed"
TagAndroidPullFailed is the tag of the error when a file cannot be read from the device.
const ValidationLayerFile = "libVkLayer_khronos_validation.so"
ValidationLayerFile is the name of the Khronos validation layer's library.
Flutter: no counterpart
Variables ¶
var ABIs = []ABI{
{Name: "arm64-v8a", GOARCH: "arm64", Short: "arm64"},
{Name: "x86_64", GOARCH: "amd64", Short: "amd64"},
}
ABIs are the supported ABIs (SPEC §24.1), in the order they are built.
var DefaultPermissions = []string{"android.permission.POST_NOTIFICATIONS"}
DefaultPermissions are the permissions every generated manifest asks for, because a service of the toolkit needs them: POST_NOTIFICATIONS, the runtime permission of Android 13 and later that desktop.ShowNotification asks the user for (SPEC §24.11). Permissions in ManifestConfig are added to them.
Flutter: no counterpart
var Dex []byte
Dex is the compiled Java of the embedding (D22), checked in as classes.dex. `go run ./cmd/glutter-gen android-dex` makes it from java/; a test checks that it is up to date.
Functions ¶
func ApplicationIDValid ¶
ApplicationIDValid says whether s is an application ID: at least two segments of letters, digits and underscores, each starting with a letter.
Flutter: no counterpart
func CompileDex ¶
CompileDex compiles the .java files below javaDir with javac (--release 11, against the platform's android.jar) and d8 (--min-api 26), and writes the single classes.dex to out. It fails with AndroidJavaNotFound when javac is not installed, and with AndroidCommandFailed when a compiler rejects the code.
Flutter: no counterpart (Gradle compiles FlutterActivity and the rest)
func DefaultAppID ¶
DefaultAppID derives an application ID from a package path: "./examples/dev/android-hello" is "org.glutter.android_hello". Characters that an ID cannot have become underscores, and a segment that does not start with a letter gets a leading "a".
Flutter: no counterpart
func DefaultIcon ¶
DefaultIcon returns Glutter's launcher icon as a PNG: a white ring with a gap and a bar, the shape of a "G", on the blue of the Material seed color.
Flutter: no counterpart
func DefaultLabel ¶
DefaultLabel is the base name of a package path, such as "android-hello".
Flutter: no counterpart
func FindBuildTools ¶
FindBuildTools is the newest build-tools/<version> of the SDK that has aapt2. It fails with AndroidBuildToolsNotFound when there is none.
Flutter: no counterpart
func FindHome ¶
FindHome is $ANDROID_HOME, else $ANDROID_SDK_ROOT. It fails with AndroidSDKNotFound when neither names a directory.
Flutter: no counterpart
func FindNDK ¶
FindNDK is $ANDROID_NDK_HOME, else the newest ndk/<version> of the SDK. It fails with AndroidNDKNotFound when there is none.
Flutter: no counterpart
func FindPlatform ¶
FindPlatform is the newest platforms/android-<level> of the SDK that has android.jar. It fails with AndroidPlatformNotFound when there is none.
Flutter: no counterpart
func Manifest ¶
func Manifest(c ManifestConfig) []byte
Manifest returns the text of AndroidManifest.xml: one activity, the receiver of the notifications' delete intents, the minimum and target API levels of D24, and the permissions asked for (the DefaultPermissions and the config's).
Flutter: no counterpart
func ShellQuote ¶
ShellQuote quotes s for the shell on the device, which `adb shell` hands the arguments to as one string.
Flutter: no counterpart
Types ¶
type ABI ¶
type ABI struct {
// Name is the directory under lib/ in the APK.
Name string
// GOARCH is the architecture Go builds for.
GOARCH string
// Short is the name the -abi flag takes.
Short string
}
ABI describes one Android ABI: the name Android uses and the Go architecture.
Flutter: no counterpart
type ADB ¶
type ADB struct {
// Path is the adb program ("adb" when empty).
Path string
// Serial is the device every command is for; empty for commands of the server.
Serial string
// Env is the environment of adb, for a private server (ANDROID_ADB_SERVER_PORT); the
// current one when nil.
Env []string
// Run runs the commands.
Run Runner
}
ADB runs adb for one device (or for the server, with no serial).
Flutter: no counterpart
func (ADB) ABI ¶
ABI is the ABI of the device that adb is for, chosen among the ones glutter-android builds. It fails with AndroidABIUnsupported for a device that has none of them (32-bit ARM or x86).
func (ADB) PullAppFile ¶
PullAppFile reads a file of the app's private directory (`run-as`, which a debuggable app allows), such as "files/glutter-screenshot.png". It fails with AndroidPullFailed when the file is not there, and with the error of adb when adb fails.
func (ADB) RemoveAppFile ¶
RemoveAppFile removes a file of the app's private directory. It is best effort: run-as fails when the app has not run yet, and then there is no file to remove.
func (ADB) Screencap ¶
Screencap takes a PNG of the device's whole screen (`adb exec-out screencap -p`): the status bar, the keyboard and the notifications are in it. It fails with AndroidPullFailed when the output is not a PNG.
func (ADB) Start ¶
Start stops the app, if it runs, and starts its activity with the given extras (strings, as `am start --es` gives them): the embedding copies the GLUTTER_ ones into the environment (SPEC §24.1).
func (ADB) StreamLog ¶
func (a ADB) StreamLog(ctx context.Context, w io.Writer, appID string, startWait time.Duration) error
StreamLog writes the log lines of the app to w until the app's process ends or ctx is done. It waits up to startWait for the process to appear. Lines are in logcat's "brief" format.
type BuildOptions ¶
type BuildOptions struct {
// Package is the main package to build, as `go build` takes it: "./examples/counter".
Package string
// Dir is the directory to run the go command in, which Package is relative to. The
// default is the current directory.
Dir string
// Out is the APK to write; "<base name of Package>.apk" when empty.
Out string
// ABIs are the ABIs to build, by their short names ("arm64", "amd64"); all when empty.
ABIs []string
// Release builds with -tags glutter_release and -ldflags=-s -w, without the debuggable
// flag, and signs with Key and Cert.
Release bool
// AppID, Label and Permissions go into the manifest; AppID and Label have defaults.
AppID, Label string
Permissions []string
// Icon is a PNG for the launcher; Glutter's icon when empty.
Icon string
// Key and Cert (PKCS#8 and X.509, as apksigner takes them) sign a release build.
Key, Cert string
// ConfigDir holds the debug key; $XDG_CONFIG_HOME/glutter/android-debug when empty.
ConfigDir string
// Env is added to the environment of the go command.
Env []string
// Dex is the embedding's classes.dex; the checked-in Dex when nil.
Dex []byte
// ValidationLayerDir, when set, is a directory with the Khronos validation layer of Android,
// <dir>/<abi>/libVkLayer_khronos_validation.so for each ABI built (the android-binaries zip of a
// Vulkan-ValidationLayers release): it is packed into the APK next to libapp.so, where the
// Vulkan loader finds the layer of a debuggable app (SPEC §24.13).
ValidationLayerDir string
}
BuildOptions are the parameters of Build (the flags of `glutter-android build`, SPEC §24.1).
Flutter: no counterpart
type BuildResult ¶
type BuildResult struct {
// APK is the signed APK, AppID the application ID.
APK, AppID string
// Size is the size of the APK in bytes, LibSizes the size of each ABI's libapp.so.
Size int64
LibSizes map[string]int64
}
BuildResult says what Build wrote.
Flutter: no counterpart
func Build ¶
func Build(ctx context.Context, sdk *SDK, run Runner, o BuildOptions) (*BuildResult, error)
Build makes the APK of a package (SPEC §24.1): it builds libapp.so for each ABI with the NDK's clang and cgo, links the manifest and the icon with aapt2, adds classes.dex and the libraries (stored, not compressed), aligns the result to 16 KB with zipalign and signs it with apksigner. The programs run through run.
Flutter: no counterpart (flutter build apk)
type Command ¶
type Command struct {
// Dir is the working directory; the current one when empty.
Dir string
// Env is the environment; the current one when nil. It is the complete environment.
Env []string
// Name is the program, found on the PATH when it has no directory.
Name string
// Args are the arguments.
Args []string
}
Command is one program the builder runs.
Flutter: no counterpart
type DebugKey ¶
type DebugKey struct {
// Key and Cert are the paths of the files.
Key, Cert string
}
DebugKey is the signing key of debug builds: a PKCS#8 private key and an X.509 certificate, the two files `apksigner sign --key --cert` takes.
Flutter: no counterpart
func EnsureDebugKey ¶
EnsureDebugKey returns the debug key in dir, and makes it first when it is not there, with Go's crypto packages (SPEC §24.1). It is made once and reused, so that an app updated in place keeps its signature. dir is $XDG_CONFIG_HOME/glutter/android-debug by default.
Flutter: no counterpart
type Device ¶
type Device struct {
// Serial is what adb calls it, such as "emulator-5554" or "R58M123ABC".
Serial string
// State is adb's state: "device" when it is usable, else "offline" or "unauthorized".
State string
}
Device is a device or emulator that adb lists.
Flutter: no counterpart
func PickDevice ¶
PickDevice chooses the device a command is for: the one named by want, else the only one that is connected. It fails with AndroidNoDevice, AndroidDeviceNotFound or AndroidManyDevices, which lists the serials.
Flutter: no counterpart
type ExecRunner ¶
type ExecRunner struct{}
ExecRunner is the Runner that runs the programs with os/exec.
Flutter: no counterpart
type ManifestConfig ¶
type ManifestConfig struct {
// AppID is the application ID, such as "org.glutter.counter".
AppID string
// Label is the name under the icon.
Label string
// Permissions are the permissions the app asks for, such as "android.permission.INTERNET".
Permissions []string
// Debuggable sets android:debuggable, which adb's run-as needs; debug builds set it.
Debuggable bool
// HasIcon says that the APK has the resource @mipmap/ic_launcher.
HasIcon bool
}
ManifestConfig is what the generated AndroidManifest.xml says (SPEC §24.1).
Flutter: no counterpart
type Runner ¶
type Runner interface {
// Run runs the command and returns its combined output. A command that fails is an error
// tagged AndroidCommandFailed, with the command line and its output in the metadata.
Run(ctx context.Context, c Command) (string, error)
}
Runner runs the programs of a build: the go command, aapt2, zipalign, apksigner, adb. A test gives the builder a fake one and checks the command lines.
Flutter: no counterpart
type SDK ¶
type SDK struct {
// Home is the SDK ($ANDROID_HOME).
Home string
// NDK is the NDK, $ANDROID_NDK_HOME or the newest ndk/<version> of the SDK.
NDK string
// BuildTools is the newest build-tools/<version> that has aapt2.
BuildTools string
// Platform is the newest platforms/android-<level> that has android.jar.
Platform string
}
SDK is the Android SDK and NDK that a build uses.
Flutter: no counterpart
func FindSDK ¶
FindSDK finds the SDK, the NDK, the build tools and the platform from the environment (getenv is os.Getenv, or a stand-in in tests). The first piece that is missing is a tagged error whose metadata "install" names the sdkmanager package to install.
Flutter: no counterpart
func (*SDK) Adb ¶
Adb is the SDK's adb when it has one, else "adb" (found on the PATH when it runs).
func (*SDK) AndroidJar ¶
AndroidJar is the platform's android.jar.