# nogar 0.1.0

## What nogar is

nogar is a toolkit for Android apps: it reads, changes, builds, signs and decompiles apk and
dex files.
It comes as ngr, a command-line tool for macOS, Linux and Windows.

## Install

Unpack the package for your system. It holds one folder with ngr (ngr.exe on Windows), this
file, LICENSE.txt, NOTICE.txt and a licenses folder.

macOS (Apple silicon, arm64)

    unzip nogar-0.1.0-osx-arm64.zip

To use ngr from any folder in this terminal session, put its folder on your PATH:

    export PATH="$PWD/nogar-0.1.0-osx-arm64:$PATH"

Linux (x64)

    tar xzf nogar-0.1.0-linux-x64.tar.gz
    export PATH="$PWD/nogar-0.1.0-linux-x64:$PATH"

ngr needs glibc 2.35 or later (Ubuntu 22.04 or later, or another distribution with the same
glibc) and the ICU and OpenSSL libraries. On Ubuntu 22.04:

    sudo apt install libicu70 libssl3

On Ubuntu versions later than 22.04 the names of these packages change; install the equivalent
packages of ICU and of OpenSSL 3 that your version offers.

Windows (x64)

Unblock the zip before you unpack it (see First run), then unpack nogar-0.1.0-win-x64.zip with
Explorer or with:

    Expand-Archive nogar-0.1.0-win-x64.zip -DestinationPath .

With Explorer's Extract All, change the destination to the folder that holds the zip, or you
end up with a folder inside another folder of the same name. Then, to use ngr from any folder
in this PowerShell session, put its folder on your Path:

    $env:Path = "$PWD\nogar-0.1.0-win-x64;$env:Path"

## First run

nogar is not notarized, so each system asks you to confirm it once.

macOS. ngr carries an ad hoc signature, not an Apple notarization, so Gatekeeper refuses to
run it the first time. Remove the quarantine mark from the unpacked folder:

    xattr -dr com.apple.quarantine nogar-0.1.0-osx-arm64

Windows. ngr.exe is not signed, so SmartScreen may warn when you run it. Before you unpack the
zip, unblock it in PowerShell:

    Unblock-File nogar-0.1.0-win-x64.zip

If SmartScreen still stops it, choose More info and then Run anyway.

When its output goes to a file or a pipe, ngr.exe writes UTF-8. In PowerShell, set
[Console]::OutputEncoding = [Text.Encoding]::UTF8 first, or non-ASCII characters in piped output
will look wrong.

Linux needs nothing beyond the libraries above.

On every system, check that ngr runs:

    ngr --version

## Three walkthroughs

The walkthroughs assume that ngr is on your PATH (see Install). Each one runs in a folder that
holds the input file. The files they write sit next to it.
Warnings, when there are any, come first on stderr; the last line on stderr says what was
written.

1. Look at an apk, then decompile it to Java.

    ngr identify app.apk
    ngr decompile app.apk

identify says what the file is and which verbs accept it; the output here is shortened:

    file: app.apk
    structure: apk with dex in the root (114,280 B)
    verbs: info, resource, xml, decode, ...
    ...

decompile writes the Java sources to app_java, with a summary of what it did, and ends with:

    wrote 17 file(s) to app_java

2. Decode an apk, change it, build it again and sign it.

    ngr decode app.apk
    ngr build app
    ngr sign --debug-key app_built.apk

decode unpacks the apk into the folder app, with the resources as text, the code as smali and
the rest as it is. The number of files depends on the apk, and WARN: lines can come before the
wrote line:

    wrote 134 file(s) to app

Edit what you want in that folder. build packs it into app_built.apk:

    wrote app_built.apk

sign writes app_built_signed.apk and the file app_built_signed.apk.idsig with the v4
signature. The first time, it creates the debug key (see The debug key) and says so first:

    WARN: created a new debug key in /Users/you/Library/Application Support/NOGAR/debug.p12; keep it to sign updates of the same apps
    wrote app_built_signed.apk and app_built_signed.apk.idsig

3. Disassemble a dex to smali and assemble it again. If you have an apk and not a dex, take
   the dex out of it first (these commands are not part of the walkthrough). On macOS and
   Linux:

    unzip app.apk classes.dex

   On Windows 10 or later, in PowerShell:

    tar -xf app.apk classes.dex

   The walkthrough:

    ngr disassemble classes.dex
    ngr assemble classes_smali

disassemble writes one smali file per class:

    wrote 25 file(s) to classes_smali

assemble builds a dex from that folder:

    wrote classes_smali.dex

A verb that refuses to overwrite says so and names the option that allows it, usually -f.

## The debug key

ngr sign --debug-key signs with a key that nogar creates for you the first time you ask for it.
Later runs of ngr sign --debug-key sign with that same key, which is stored here:

    macOS    ~/Library/Application Support/NOGAR/debug.p12
    Linux    $XDG_CONFIG_HOME/NOGAR/debug.p12, or ~/.config/NOGAR/debug.p12 when
             XDG_CONFIG_HOME is not set
    Windows  %APPDATA%\NOGAR\debug.p12

Keep that file. An Android device only installs an update of an app when the update carries
the same signature as the installed app, so if you lose the key, apps you signed with it can no
longer be updated in place. nogar does not back it up for you.

The debug key is for testing. To sign for release, use your own key store: ngr sign --ks
<keystore>, with --ks-pass and --ks-key-alias as needed.

## The 0.x versions are beta

The 0.x.y versions are beta versions. We will try not to, but from one to the next the verbs,
the options, the exit codes and the output formats may change.

## Exit codes

    0  Success.
    1  Failure: an input can't be read or used, or an output can't be written.
    2  Usage error: the options or inputs on the command line are wrong.
    3  Negative verdict, only from identify, align, verify and decompile:
         identify    the file was read and is none of the structures it knows;
         align       a stored entry is not aligned;
         verify      the apk does not verify;
         decompile   some code was written as smali: Java cannot express it, --unstructured
                     smali asked for it, or its translation is not supported yet or failed.
       The help of each verb gives the exact meaning.

ngr help lists the verbs, and ngr help <verb> shows the inputs and options of one.

## License and third-party notices

nogar is distributed in binary form under the agreement in LICENSE.txt. It includes components
from other projects, under their own licenses: MIT (the .NET runtime and libraries, and
System.CommandLine), BSD-3-Clause, zlib and the Apache License 2.0, among others. NOTICE.txt
lists them with their notices, and the licenses folder holds the full text of the Apache
License 2.0 and of the MIT license, and the third-party notices of the .NET runtime.

## Help, bugs and support

Write to nogar@stau.me. For a bug, say which verb you ran, the exact command, what you
expected and what happened, and the output of ngr --version.
