Skip to content

Commit

Permalink
Merge pull request #2 from nao1215/sample-code
Browse files Browse the repository at this point in the history
Rename project name
  • Loading branch information
nao1215 authored Oct 7, 2022
2 parents 9730589 + ac96c3a commit 2abc3b1
Show file tree
Hide file tree
Showing 19 changed files with 544 additions and 95 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
on: [pull_request]
name: Coverage
jobs:
test:
strategy:
matrix:
go-version: [1.19.x]
os: [ubuntu-latest]

runs-on: ${{ matrix.os }}
steps:
- name: Install Go
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go-version }}

- name: Checkout code
uses: actions/checkout@v3

- name: Test
run: go test -race -coverprofile="coverage.txt" -covermode=atomic ./...

- name: upload coverage
uses: codecov/codecov-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
# Dependency directories (remove the comment below to include it)
# vendor/
cover.*
apkparser
deapk
6 changes: 3 additions & 3 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
project_name: apkparser
project_name: deapk
env:
- GO111MODULE=on
before:
Expand All @@ -8,7 +8,7 @@ before:
builds:
- main: .
ldflags:
- -s -w -X apkparser/cmd.Version=v{{ .Version }}
- -s -w -X deapk/cmd.Version=v{{ .Version }}
env:
- CGO_ENABLED=0
goos:
Expand All @@ -35,4 +35,4 @@ changelog:
filters:
exclude:
- "^docs:"
- "^test:"
- "^test:"
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.PHONY: build test clean vet fmt chkfmt

APP = apkparser
APP = deapk
VERSION = $(shell git describe --tags --abbrev=0)
GO = go
GO_BUILD = $(GO) build
Expand All @@ -15,7 +15,7 @@ GOOS = ""
GOARCH = ""
GO_PKGROOT = ./...
GO_PACKAGES = $(shell $(GO_LIST) $(GO_PKGROOT))
GO_LDFLAGS = -ldflags '-X apkparser/cmd.Version=${VERSION}'
GO_LDFLAGS = -ldflags '-X deapk/cmd.Version=${VERSION}'

build: ## Build binary
env GO111MODULE=on GOOS=$(GOOS) GOARCH=$(GOARCH) $(GO_BUILD) $(GO_LDFLAGS) -o $(APP) main.go
Expand Down
80 changes: 78 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,81 @@
[![Go Reference](https://pkg.go.dev/badge/github.com/nao1215/apk-parser.svg)](https://pkg.go.dev/github.com/nao1215/apk-parser)
[![Go Report Card](https://goreportcard.com/badge/github.com/nao1215/apk-parser)](https://goreportcard.com/report/github.com/nao1215/apk-parser)
![GitHub](https://img.shields.io/github/license/nao1215/apk-parser)
# apk-parser - parse android package (.apk), getting meta data.
[WIP]
# deapk - parse android package (.apk), getting meta data.
The deapk (decompile android package) command parses the apk file and outputs metadata information. It is still in the development stage and output information is few. In the future, deapk will provide the ability to decompile dex files and convert them to source code.

# How to install
### Step1. Install golang
deapk command only supports installation with `$ go install`. If you does not have the golang development environment installed on your system, please install golang from the [golang official website](https://go.dev/doc/install).

### Step2. Install deapk
```
$ go install github.com/nao1215/deapk@latest
```

# How to use
## Output *.apk metadata
```
$ deapk info testdata/app-debug.apk
pacakage name : jp.debimate.deapk_test
application name : deapk-test
application version: 1.0
sdk target version : 31
sdk max version : -1 (deprecated attribute)
sdk min version : 31
main activity : jp.debimate.deapk_test.MainActivity
```

## Output *.apk metadata in json format
```
$ deapk info --json testdata/app-debug.apk
{
"Basic": {
"package_name": "jp.debimate.deapk_test",
"application_name": "deapk-test",
"version": "1.0",
"main_activity": "jp.debimate.deapk_test.MainActivity",
"sdk": {
"minimum": 31,
"target": 31,
"maximum": -1
}
}
}
```

## Output *.apk metadata to file
### Use redirect
```
$ deapk info --json testdata/app-debug.apk > apk.json
```
### Use --output option
```
$ deapk info --json --output=apk.json testdata/app-debug.apk
$ cat apk.json
{
"Basic": {
"package_name": "jp.debimate.deapk_test",
"application_name": "deapk-test",
"version": "1.0",
"main_activity": "jp.debimate.deapk_test.MainActivity",
"sdk": {
"minimum": 31,
"target": 31,
"maximum": -1
}
}
}
```
# Contributing
First off, thanks for taking the time to contribute! ❤️ See [CONTRIBUTING.md](./CONTRIBUTING.md) for more information.
Contributions are not only related to development. For example, GitHub Star motivates me to develop!

# Contact
If you would like to send comments such as "find a bug" or "request for additional features" to the developer, please use one of the following contacts.

- [GitHub Issue](https://github.com/nao1215/deapk/issues)

# LICENSE
The deapk project is licensed under the terms of [MIT License](./LICENSE).

142 changes: 142 additions & 0 deletions apk/apk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// Package apk manage APK file information.
package apk

import (
"encoding/json"
"errors"
"fmt"
"io"

"github.com/shogo82148/androidbinary/apk"
)

var (
// ErrNotOpenAPK : failed to open apk file
ErrNotOpenAPK = errors.New("failed to open apk file")
)

// APK is android package (*.apk) file information.
type APK struct {
// Path is path to apk file.
Path string
// Package is android package (*.apk) itself.
Package *Package
}

// Package is Android Package (*.apk) information.
type Package struct {
// Basic is basic information for apk file.
Basic *Basic
}

// Basic is basic information for apk file.
type Basic struct {
// PackageName is package name (bundle id).
PackageName string `json:"package_name,omitempty"`
// ApplicationName is android application name.
ApplicationName string `json:"application_name,omitempty"`
// Version is application version.
Version string `json:"version,omitempty"`
// MainActivity is the activity that loads first and the rest of your application.
MainActivity string `json:"main_activity,omitempty"`
// SDK is android sdk information.
SDK *SDK `json:"sdk,omitempty"`
}

// SDK is android sdk information
type SDK struct {
// Minimun is supported minimum SDK versions
Minimum int32 `json:"minimum,omitempty"`
// Target is target SDK version.
Target int32 `json:"target,omitempty"`
// Maximum is supported maximum SDK versions. It's deprecated attribute from android 2.0.1.
Maximum int32 `json:"maximum,omitempty"`
}

// NewAPK return APK struct.
func NewAPK(path string) *APK {
return &APK{
Path: path,
Package: &Package{
Basic: &Basic{
SDK: &SDK{},
},
},
}
}

// Parse parses the files contained in the *.apk file
// and sets the metadata into an APK struct.
func (a *APK) Parse() error {
apk, err := apk.OpenFile(a.Path)
if err != nil {
return fmt.Errorf("%w: %s", ErrNotOpenAPK, a.Path)
}
defer apk.Close()

a.setBasicInfo(*apk)

return nil
}

// setBasicInfo extract basic information from the apk file and
// set its contents into an APK struct.
func (a *APK) setBasicInfo(apk apk.Apk) {
a.setSDK(apk)
a.Package.Basic.PackageName = apk.PackageName()

var err error
a.Package.Basic.ApplicationName, err = apk.Label(nil)
if err != nil {
a.Package.Basic.ApplicationName = "(unknown)"
}

a.Package.Basic.Version, err = apk.Manifest().VersionName.String()
if err != nil {
a.Package.Basic.Version = "(unknown)"
}

a.Package.Basic.MainActivity, err = apk.MainActivity()
if err != nil {
a.Package.Basic.MainActivity = "(unknown)"
}
}

func (a *APK) setSDK(apk apk.Apk) {
var err error
a.Package.Basic.SDK.Minimum, err = apk.Manifest().SDK.Min.Int32()
if err != nil {
a.Package.Basic.SDK.Minimum = -1
}

a.Package.Basic.SDK.Target, err = apk.Manifest().SDK.Target.Int32()
if err != nil {
a.Package.Basic.SDK.Target = -1
}

a.Package.Basic.SDK.Maximum, err = apk.Manifest().SDK.Max.Int32()
if err != nil || a.Package.Basic.SDK.Maximum == 0 {
a.Package.Basic.SDK.Maximum = -1
}
}

// Print write apk information at io.Writer (e.g. STDOUT)
func (a *APK) Print(w io.Writer) {
fmt.Fprintf(w, "pacakage name : %s\n", a.Package.Basic.PackageName)
fmt.Fprintf(w, "application name : %s\n", a.Package.Basic.ApplicationName)
fmt.Fprintf(w, "application version: %s\n", a.Package.Basic.Version)
fmt.Fprintf(w, "sdk target version : %d\n", a.Package.Basic.SDK.Target)
fmt.Fprintf(w, "sdk max version : %d (deprecated attribute)\n", a.Package.Basic.SDK.Maximum)
fmt.Fprintf(w, "sdk min version : %d\n", a.Package.Basic.SDK.Minimum)
fmt.Fprintf(w, "main activity : %s\n", a.Package.Basic.MainActivity)
}

// PrintJSON write apk information in json format
func (a *APK) PrintJSON(w io.Writer) error {
j, err := json.MarshalIndent(a.Package, "", "\t")
if err != nil {
return err
}
fmt.Fprintln(w, string(j))
return nil
}
Loading

0 comments on commit 2abc3b1

Please sign in to comment.