golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.97k stars 17.67k forks source link

x/mobile: build doesn't include libc++_shared.so to produced .aar file and app crashes #50157

Open tangxuesong6 opened 2 years ago

tangxuesong6 commented 2 years ago

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

go version go1.17.5 linux/amd64

Does this issue reproduce with the latest release?

yes

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

GO111MODULE="on"
GOARCH="amd64"
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOVERSION="go1.17.4"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
...

What did you do?

I use the open source project github.com/anacrolix, and want to build an Android aar for my project. After gomobile bind -v -target=android ./android/, successfully exported an .aar file for me. Then i import the aar to my project use implementation files('libs/bt.aar') . The apk build successfully too. But when i launch the apk and cal the method in aar,like SDK.Start, my app crashed. Here is the error log

    java.lang.UnsatisfiedLinkError: dlopen failed: library "libc++_shared.so" not found
        at java.lang.Runtime.loadLibrary0(Runtime.java:1071)
        at java.lang.Runtime.loadLibrary0(Runtime.java:1007)
        at java.lang.System.loadLibrary(System.java:1667)
        at go.Seq.<clinit>(Seq.java:37)
        at go.Seq.touch(Seq.java:50)

What did you expect to see?

apk run successful

What did you see instead?

crashed

cherrymui commented 2 years ago

What version of Android you are running on and what SDK version you're using? Thanks.

cc @hyangah @eliasnaur @changkun

tangxuesong6 commented 2 years ago

What version of Android you are running on and what SDK version you're using? Thanks.

cc @hyangah @eliasnaur @changkun

Running on Android 10 (API 29). But i think the bug maybe because of my wrong use of gomobile bind cmd, or it's gomobile's bug.

changkun commented 2 years ago

@cherrymui The builders on Corellium are running Android 9 and the installed NDK version is 23b.

tangxuesong6 commented 2 years ago

@cherrymui The builders on Corellium are running Android 9 and the installed NDK version is 23b.

请问我是要换NDK版本吗? 当前是21.4.7075529

changkun commented 2 years ago

@cherrymui The builders on Corellium are running Android 9 and the installed NDK version is 23b.

请问我是要换NDK版本吗? 当前是21.4.7075529

Can you maybe provide a minimal example to show your build process? The previous description is not clear as the project that you claim to build is not clear to us too.

tangxuesong6 commented 2 years ago

@cherrymui The builders on Corellium are running Android 9 and the installed NDK version is 23b.

请问我是要换NDK版本吗? 当前是21.4.7075529

Can you maybe provide a minimal example to show your build process? The previous description is not clear as the project that you claim to build is not clear to us too.

OK

1

download some repositories(GO111MODULE="on")

go get -u github.com/anacrolix/torrent
go get -u github.com/dustin/go-humanize

2

here is my code(only 17 lines)

import (
    "fmt"
    "github.com/anacrolix/torrent"
    "github.com/dustin/go-humanize"
)

func GetBtInfo() {
    var t *torrent.Torrent
    cfg := torrent.NewDefaultClientConfig()
    cfg.DataDir = "./movie"
    c, err := torrent.NewClient(cfg)
    if err != nil {
        fmt.Printf("err: %v", err)
    }
    url := "magnet:?xt=urn:btih:38F07BE8BBBBE4F2C6CBEC909F5C7BFAB8114185&dn=Resident%20Evil%20Welcome%20to%20Raccoon%20City%202021%20x264%20AAC%20800MB%20SD%20HDC&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A6969%2Fannounce&tr=udp%3A%2F%2F9.rarbg.to%3A2710%2Fannounce&tr=udp%3A%2F%2F9.rarbg.me%3A2780%2Fannounce&tr=udp%3A%2F%2F9.rarbg.to%3A2730%2Fannounce&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337&tr=http%3A%2F%2Fp4p.arenabg.com%3A1337%2Fannounce&tr=udp%3A%2F%2Ftracker.torrent.eu.org%3A451%2Fannounce&tr=udp%3A%2F%2Ftracker.tiny-vps.com%3A6969%2Fannounce&tr=udp%3A%2F%2Fopen.stealth.si%3A80%2Fannounce"
    t, err = c.AddMagnet(url)
    if err != nil {
        fmt.Printf("add err: %v", err)
    }
    <-t.GotInfo()
    size := humanize.Bytes(uint64(t.Info().TotalLength()))
    fmt.Printf("totalSize: %s, name: %s", size, t.Info().Name)
}

3

use gomobile

 gomobile init   

 gomobile bind -v  -target=android ./android/ 

this step will output an .aar file(named android.aar)

4

copy the .aar file to Android studio, and implementation

    implementation files('libs/android.aar')

5

create a buttun in my Android app, when click, call the android.aar method GetBtInfo

Android.getBtInfo();

6

build and run the Android project. the app run successfully. But when i click the buttun which call a method Android.getBtInfo();, crashed. Here is the error log.

 java.lang.UnsatisfiedLinkError: dlopen failed: library "libc++_shared.so" not found
        at java.lang.Runtime.loadLibrary0(Runtime.java:1071)
        at java.lang.Runtime.loadLibrary0(Runtime.java:1007)
        at java.lang.System.loadLibrary(System.java:1667)
        at go.Seq.<clinit>(Seq.java:37)
        at go.Seq.touch(Seq.java:50)
        at android.Android.<clinit>(Android.java:12)
        at android.Android.getBtInfo(Native Method)
anacrolix commented 1 year ago

I've also run into this issue. Same reproduction. One of the dependencies for anacrolix/torrent (probably anacrolix/go-libutp) uses C++.