anacrolix / torrent

Full-featured BitTorrent client package and utilities
Mozilla Public License 2.0
5.41k stars 617 forks source link

Packaging an android torrent app that uses the gui toolkit "https://github.com/fyne-io/fyne" #809

Closed vboehr closed 1 year ago

vboehr commented 1 year ago

Describe the bug

I am trying to package an android torrent app that uses the following gui toolkit: https://github.com/fyne-io/fyne

Yet after installing the app on android and running it I get a black screen and app stops. How to reproduce

create a new go mod go mod init fynetorrentguiapp

go get fyne.io/fyne/v2

go get github.com/anacrolix/torrent

touch main.go

add an appicon.png file

add the example code include below

package the app as follow: ./fyne-cmd package -os android -appID com.fynetorrentguiapp.myapp -icon ./Icons/appicon.png

install the app on android and run it. Screenshots

No response Example code

package main

import (
    "fmt"
    "fyne.io/fyne/v2"
    "fyne.io/fyne/v2/app"
    "fyne.io/fyne/v2/canvas"
    "fyne.io/fyne/v2/container"
    "fyne.io/fyne/v2/data/binding"
    "fyne.io/fyne/v2/widget"
    "github.com/anacrolix/torrent"
    "image/color"
    "log"
    "time"
)

var mainapp fyne.App
var AppLocation string
var MainTorrent string //magnet
var MainFile string    //filepath
var AppIsClosing bool

func main() {
    mainapp = app.New()
    AppIsClosing = false
    AppLocation = mainapp.Storage().RootURI().String()
    fmt.Println("AppLocation :", AppLocation)
    mainwin := mainapp.NewWindow("wetorrent")
    mainwin.Resize(fyne.NewSize(400, 710))
    go initmainclient()
    tabs := container.NewAppTabs(
        container.NewTabItem("Home", homeScreen(mainwin)),
    )
    tabs.SetTabLocation(container.TabLocationTop)
    mainwin.SetContent(tabs)
    mainwin.ShowAndRun()
    AppIsClosing = true
}
func homeScreen(win fyne.Window) fyne.CanvasObject {
    data := binding.BindStringList(
        // &[]string{"Item 1", "Item 2", "Item 3"},
        &[]string{},
    )
    list := widget.NewListWithData(data,
        func() fyne.CanvasObject {
            return widget.NewLabel("template")
        },
        func(i binding.DataItem, o fyne.CanvasObject) {
            o.(*widget.Label).Bind(i.(binding.String))
        })
    text := canvas.NewText("Text Object"+AppLocation, color.Black)
    add := widget.NewButton("Open New Webapp Tab", func() {
        // val := fmt.Sprintf("Item %d", data.Length()+1)
        // data.Append(val)
        fmt.Println("coool")
    })
    return container.NewBorder(text, add, nil, nil, list)
}

var mainclient *torrent.Client

func initmainclient() {
    cfg := torrent.NewDefaultClientConfig()
    // cfg.Seed = true
    cfg.DataDir = AppLocation //
    // cfg.NoDHT = true
    // cfg.DisableTCP = true
    // cfg.DisableUTP = true
    // cfg.DisableAggressiveUpload = false
    // cfg.DisableWebtorrent = false
    // cfg.DisableWebseeds = false
    var err error
    mainclient, err = torrent.NewClient(cfg)
    if err != nil {
        log.Print("new torrent client: %w", err)
        return //fmt.Errorf("new torrent client: %w", err)
    }
    log.Print("new torrent client INITIATED")
    defer mainclient.Close()
    for {
        if AppIsClosing {
            log.Print("closing mainclient")
            break
        }
        time.Sleep(1 * time.Second)
    }
}

Fyne version

v2.3.0 Go compiler version

go1.20 linux/amd64 Operating system

Linux Operating system version

Ubuntu 18.04.6 LTS (Bionic Beaver) Additional Information

./fyne-cmd env Go module info

cli_version="(devel)" go_mod="true" go_path="false" imported="true" path="***" version="v2.3.0" Go version info

version="go1.20 linux/amd64" Go environment info

CGO_ENABLED="1" GO111MODULE="" GOARCH="amd64" GOOS="linux" OS info

architecture="x86_64" home_url="https://www.ubuntu.com/" kernel="Linux 5.4.0-137-generic #154~18.04.1-Ubuntu SMP Tue Jan 10 16:58:20 UTC 2023" name="Ubuntu" version="18.04.6 LTS (Bionic Beaver)"

anacrolix commented 1 year ago

Sorry I got stuck trying to install the NDK. There are downstream projects that have done this successfully I believe.

vboehr commented 1 year ago

Download ndk from: https://dl.google.com/android/repository/android-ndk-r25b-linux.zip

gedit ~/.bashrc

In bashrc add the following line: export ANDROID_NDK_HOME=~/Documents/android-ndk-r25b-linux/android-ndk-r25b

after that: source ~/.bashrc

If you want to test if ANDROID_NDK_HOME is working: cd $ANDROID_NDK_HOME

vboehr commented 1 year ago

I used "adb logcat" to get the runtime error and I got :+1: java.lang.UnsatisfiedLinkError: dlopen failed: library "libc++_shared.so" not found Screenshot from 2023-02-17 21-48-45

vboehr commented 1 year ago

I asked chatgpt to explain this runtime error and here its answer:

CHATGPT answer: Based on the error message you provided, it seems that the libc++_shared.so library is missing. This library is required by the libwetorrent_rebirth.so library, which is used by your app.

To fix this issue, you should make sure that you have included the libc++_shared.so library in your project and that it is being properly loaded by your app.

Here are some steps you can try to resolve this issue:

Check that you have included the libc++_shared.so library in your project. You can do this by looking for the library in your project's libs directory. If the library is missing, you may need to download it from the Android NDK and include it in your project.

If the libc++_shared.so library is present in your project, you should check that it is being properly loaded by your app. You can do this by checking your app's Android.mk or CMakeLists.txt file to ensure that the library is included in the LOCAL_LDLIBS variable.

If you are still having issues, you may want to try building your app with a different version of the Android NDK, as the version you are using may not be compatible with your project.

I hope this helps! Let me know if you have any further questions or concerns. what do you think?

anacrolix commented 1 year ago

I was able to build the apk now. The libc++_shared.so library is probably pulled in for the C++ part of go-libutp. You should include an appropriate build of libc++_shared.so file, or you could try building without libutp by passing --tags disable_libutp to fyne, or disabling CGO entirely.

EmmettKim93 commented 1 year ago

I would like to point out that if you add "disable_libutp" you will not be able to add UDP trackers and that would significantly limit the usefulness of this libray as most torrents use UDP trackers.

anacrolix commented 1 year ago

@EmmettKim93 if that's the case, it's a bug. uTP for peer protocol, and the use of UDP for UDP trackers are distinct.

EmmettKim93 commented 1 year ago

I am sorry I thought that uTP for peer protocol was used for UDP trackers. I apologize.

anacrolix commented 1 year ago

All good, I appreciate people trying to help out with issues and discussion.

vboehr commented 1 year ago

Thanks for the help provided. As you suggested adding "disable_libutp" solved the issue. Now I can use "anacrolix/torrent" in a fyne app !

Thanks again !

anacrolix commented 1 year ago

Note that I still think a better fix is out of scope for this repo, but I am glad you have found a sufficient workaround for now. I will close this for now. Thanks for following up with your resolution.