golang / go

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

x/mobile/bind/java: fatal error: jni.h: No such file or directory #58426

Open dpanic opened 1 year ago

dpanic commented 1 year ago

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

$ go version
go version go1.20 linux/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

GOHOSTARCH="amd64" 
GOHOSTOS="linux"   

ANDROID_NDK_HOME=/home/dpanic/development/Android/Sdk/ndk/25.2.9519653
ANDROID_HOME=/home/dpanic/development/Android/Sdk

What did you do?

Binding Go Mobile project with following command:

CGO_ENABLED=1 \
        gomobile bind \
        -androidapi 23 \
        -target=android/$TARGET \
        -ldflags="-w -s" \
        -o export_$TARGET.aar \
        -v

What did you expect to see?

On Go version 1.19.5 and lower this works.

What did you see instead?

unable to import bind/java: [-: # golang.org/x/mobile/bind/java                         
/home/dpanic/go/pkg/mod/golang.org/x/mobile@v0.0.0-20221110043201-43a038452099/bind/java
/context_android.go:9:10: fatal error: jni.h: No such file or directory                 
    9 | //#include <jni.h>                                                              
      |          ^~~~~~~                                                                
compilation terminated.]                                                                
gomobile: /home/dpanic/go/bin/gobind -lang=go,java -outdir=/tmp/gomobile-work-1953076754
 example.com/export failed: exit status 1                                            
dr2chase commented 1 year ago

@hyangah can you give this a look? It has a real "this could be a lot of things" look to it.

hyangah commented 1 year ago

Is it possible to post the log when running gomobile bind with -x -v flags?

dpanic commented 1 year ago
GOMOBILE=/home/dpanic/go/pkg/gomobile
WORK=/tmp/gomobile-work-791525417
GOOS=android CGO_ENABLED=1 $GOPATH/bin/gobind -lang=go,java -outdir=$WORK REDACTED.com/export
unable to import bind/java: [-: # golang.org/x/mobile/bind/java
/home/dpanic/go/pkg/mod/golang.org/x/mobile@v0.0.0-20221110043201-43a038452099/bind/java/context_android.go:9:10: fatal error: jni.h:
No such file or directory
    9 | //#include <jni.h>
      |          ^~~~~~~
compilation terminated.]
rm -r -f "$WORK"
gomobile: /home/dpanic/go/bin/gobind -lang=go,java -outdir=/tmp/gomobile-work-791525417 REDACTED.com/export failed: exit status 1
leo-lox commented 1 year ago

had the same issue for me the environment variable in PATH => C:\Program Files\OpenJDK\jdk-19.0.2\bin was missing.

yiitz commented 1 year ago

had the same issue with go 1.20.1, while it succeeded with go 1.19.6.

dpanic commented 1 year ago

gomobile init solves the problem!

Thank you @leo-lox

ChenSee commented 1 year ago

When encountering the same problem, gomobile init does not solve the problem.

yiitz commented 1 year ago

finally solved by upgrade golang.org/x/mobile to latest version. go get -u golang.org/x/mobile

chenxiaolong commented 1 year ago

After doing a git bisect, this seems to happen when using go 1.20 with a version of gomobile that's missing https://github.com/golang/mobile/commit/406ed3a7b8e44dc32844953647b49696d8847d51. Relevant bug report: https://github.com/golang/go/issues/56292.

(For folks who are using the gomobile version distributed in the Fedora repositories, I created a bug report to get that updated at: https://bugzilla.redhat.com/show_bug.cgi?id=2209147)

ignoramous commented 1 year ago

icymi: gomobile works on go1.21 just fine.

vikulin commented 2 months ago

Any updates? It is still reproducing for golang v1.21.11 and v1.22.4

yiitz commented 2 months ago

Any updates? It is still reproducing for golang v1.21.11 and v1.22.4

I solved it after update my project's go mobile dependencies to the latest version.

vikulin commented 2 months ago

@yiitz could you please give exact golang, gomobile versions where you have solved this issue for?

ignoramous commented 2 months ago

could you please give exact golang, gomobile versions where you have solved this issue for?

Works with latest.

You can see this Action (recent run) which builds AAR (Android Archive) with go1.22 just fine (gomobile install/init in Makefile via make-aar).

vikulin commented 2 months ago

could you please give exact golang, gomobile versions where you have solved this issue for?

Works with latest.

You can see this Action (recent run) which builds AAR (Android Archive) with go1.22 just fine (gomobile install/init in Makefile via make-aar).

I like your 🐼 symbols)

vikulin commented 2 months ago

could you please give exact golang, gomobile versions where you have solved this issue for?

Works with latest.

You can see this Action (recent run) which builds AAR (Android Archive) with go1.22 just fine (gomobile install/init in Makefile via make-aar).

Aar builds successfully has been done in GitHub Action but still failing in local environment:

Executing make

gomobile: /usr/bin/gobind -lang=go,java -outdir=/tmp/gomobile-work-1766811808 -tags=mobile github.com/RiV-chain/RiV-mesh/contrib/mobile github.com/RiV-chain/RiV-mesh/src/config failed: exit status 1
unable to import bind/java: [-: # golang.org/x/mobile/bind/java
/home/vadym/go/pkg/mod/golang.org/x/mobile@v0.0.0-20240604190613-2782386b8afd/bind/java/context_android.go:9:10: fatal error: jni.h: No such file or directory
    9 | //#include <jni.h>
      |          ^~~~~~~
compilation terminated.]

make: *** [Makefile:2: all] Error 1
vikulin commented 2 months ago

could you please give exact golang, gomobile versions where you have solved this issue for?

Works with latest.

You can see this Action (recent run) which builds AAR (Android Archive) with go1.22 just fine (gomobile install/init in Makefile via make-aar).

Can you please confirm the build works locally from command line without GitHub Actions run?

yiitz commented 2 months ago

could you please give exact golang, gomobile versions where you have solved this issue for?

Works with latest. You can see this Action (recent run) which builds AAR (Android Archive) with go1.22 just fine (gomobile install/init in Makefile via make-aar).

Can you please confirm the build works locally from command line without GitHub Actions run?

golang.org/x/mobile v0.0.0-20240320162201-c76e57eead38 go version go1.22.2 darwin/amd64 I build with the above version locally a few days ago, and it works well.

vikulin commented 2 months ago

could you please give exact golang, gomobile versions where you have solved this issue for?

Works with latest. You can see this Action (recent run) which builds AAR (Android Archive) with go1.22 just fine (gomobile install/init in Makefile via make-aar).

Can you please confirm the build works locally from command line without GitHub Actions run?

golang.org/x/mobile v0.0.0-20240320162201-c76e57eead38 go version go1.22.2 darwin/amd64 I build with the above version locally a few days ago, and it works well.

Originally, the issue is reported for linux/amd64 not for MacOS.

vikulin commented 2 months ago

Installed golang 1.22.4 on clean Ubuntu 24.04, issue is not reproducing.