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

x/mobile: proguard strips generated proxy classes from AAR #30251

Closed
halseth opened this issue Feb 15, 2019 · 4 comments

Comments

Projects
None yet
4 participants
@halseth
Copy link

commented Feb 15, 2019

What version of Go are you using (go version)?

$ go version
go version go1.11.5 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/johan/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/johan/golang"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/Cellar/go/1.11.5/libexec"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.11.5/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/johan/golang/src/github.com/lightningnetwork/lnd/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/kq/3436m_v11sg0l7zqtmv2r1gw0000gn/T/go-build914982300=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Build android library using gomobile (commit a42111704963f4f0d1266674e1e97489aa8dcca0):

GO111MODULE=off ${GOPATH}/bin/gomobile bind -target=android -tags="android" -v -o ./build/Lndmobile.aar github.com/lightningnetwork/lnd/mobile

where the package lndmobile is defined as here: https://github.com/halseth/lnd/tree/mobile-support/mobile

This creates a library Lndmobile.aar which I import into an Android Studio project: https://github.com/lightninglabs/lightning-app/blob/expokit-32/mobile/android/app/src/main/java/host/exp/exponent/LndNativeModule.java

What did you expect to see?

App builds and runs, both in debug and release modes.

What did you see instead?

App works in debug mode, but in release mode (Build->Generate Signed APK) it crashes with error

02-14 03:51:26.714: A/ng.LightningAp(11522): thread.cc:2148] No pending exception expected: java.lang.ClassNotFoundException: Didn't find class "lndmobile.Lndmobile$proxyCallback" on path: DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/system/framework/android.test.mock.jar", zip file "/system/framework/org.apache.http.legacy.boot.jar", zip file "/data/app/com.google.android.apps.mtaas.crawler-8_-i2i64DrNb9s4Hpvcoqg==/base.apk", zip file "/data/app/engineering.lightning.LightningApp-YDmR4u0ypHHFvfyOS8qZGw==/base.apk"],nativeLibraryDirectories=[/data/app/com.google.android.apps.mtaas.crawler-8_-i2i64DrNb9s4Hpvcoqg==/lib/arm64, /data/app/engineering.lightning.LightningApp-YDmR4u0ypHHFvfyOS8qZGw==/lib/arm, /data/app/com.google.android.apps.mtaas.crawler-8_-i2i64DrNb9s4Hpvcoqg==/base.apk!/lib/armeabi-v7a, /data/app/engineering.lightning.LightningApp-YDmR4u0ypHHFvfyOS8qZGw==/base.apk!/lib/armeabi-v7a, /system/lib]]
02-14 03:51:26.714: A/ng.LightningAp(11522): thread.cc:2148]   at java.lang.Class dalvik.system.BaseDexClassLoader.findClass(java.lang.String) (BaseDexClassLoader.java:134)
02-14 03:51:26.714: A/ng.LightningAp(11522): thread.cc:2148]   at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String, boolean) (ClassLoader.java:379)
02-14 03:51:26.714: A/ng.LightningAp(11522): thread.cc:2148]   at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String) (ClassLoader.java:312)
02-14 03:51:26.714: A/ng.LightningAp(11522): thread.cc:2148]   at void lndmobile.Lndmobile._init() (Lndmobile.java:-2)
02-14 03:51:26.714: A/ng.LightningAp(11522): thread.cc:2148]   at void lndmobile.Lndmobile.<clinit>() (Lndmobile.java:12)
02-14 03:51:26.714: A/ng.LightningAp(11522): thread.cc:2148]   at void lndmobile.Lndmobile.start(java.lang.String, lndmobile.Callback) (Lndmobile.java:-2)
02-14 03:51:26.714: A/ng.LightningAp(11522): thread.cc:2148]   at void host.exp.exponent.LndNativeModule$1.run() (LndNativeModule.java:155)
02-14 03:51:26.714: A/ng.LightningAp(11522): thread.cc:2148]   at void java.lang.Thread.run() (Thread.java:764)
02-14 03:51:26.714: A/ng.LightningAp(11522): thread.cc:2148] 

Inspecting the generated APKs shows that lndmobile.Lndmobile$proxyCallback gets stripped from the release APK. The following patch seems to resolve it, by adding the package name to the proguard rules:

diff --git a/cmd/gomobile/bind_androidapp.go b/cmd/gomobile/bind_androidapp.go
index 9897fbd..6d2ebfa 100644
--- a/cmd/gomobile/bind_androidapp.go
+++ b/cmd/gomobile/bind_androidapp.go
@@ -160,6 +160,7 @@ func buildAAR(srcDir, androidDir string, pkgs []*build.Package, androidArchs []s
                return err
        }
        fmt.Fprintln(w, `-keep class go.** { *; }`)
+       fmt.Fprintln(w, `-keep class lndmobile.** { *; }`)

        w, err = aarwcreate("classes.jar")
        if err != nil {

Let me know if this is a desired addition, or if there is a better way to resolve this issue.

@gopherbot gopherbot added this to the Unreleased milestone Feb 15, 2019

@gopherbot gopherbot added the mobile label Feb 15, 2019

@bcmills

This comment has been minimized.

Copy link
Member

commented Feb 28, 2019

@gopherbot

This comment has been minimized.

Copy link

commented Mar 1, 2019

Change https://golang.org/cl/164798 mentions this issue: cmd/gomobile: add generated package names to proguard.txt

@eliasnaur

This comment has been minimized.

Copy link
Contributor

commented Mar 1, 2019

@halseth please test https://golang.org/cl/164798 as I couldn't figure out how to enable class stripping for our gradle tests.

@halseth

This comment has been minimized.

Copy link
Author

commented Mar 5, 2019

Confirming that https://golang.org/cl/164798 fixes my issue! Thanks 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.