2
0
mirror of synced 2025-02-23 06:48:15 +00:00

mobile/bind: fix gomobile bind with custom Java package

The change from using strings to objects for passing errors across
the language barrier broke the custom java package mode of gombile
bind. Fix it and add a runtime test to make sure it won't happen
again.

Fixes golang/go#16262

Change-Id: Ia7f8afb79556798056f0755758052190081a2dbb
Reviewed-on: https://go-review.googlesource.com/24800
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
This commit is contained in:
Elias Naur 2016-07-07 17:27:58 +02:00
parent 8d3035464e
commit 44ce26ee94
3 changed files with 38 additions and 10 deletions

View File

@ -538,12 +538,12 @@ var javaNameReplacer = strings.NewReplacer(
)
func (g *javaGen) javaPkgName(pkg *types.Package) string {
if g.javaPkg != "" {
return g.javaPkg
}
if pkg == nil {
return "go"
}
if g.javaPkg != "" {
return g.javaPkg
}
s := javaNameReplacer.Replace(pkg.Name())
// Look for Java keywords that are not Go keywords, and avoid using
// them as a package name.

View File

@ -0,0 +1,15 @@
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package go;
import android.test.InstrumentationTestCase;
import org.golang.custompkg.Testpkg;
public class CustomPkgTest extends InstrumentationTestCase {
public void testHi() {
Testpkg.Hi();
}
}

View File

@ -16,15 +16,18 @@ import (
"time"
)
// TestJavaSeqTest runs java test SeqTest.java.
// This requires the gradle command in PATH and
// the Android SDK whose path is available through ANDROID_HOME environment variable.
func TestCustomPkg(t *testing.T) {
runTest(t, []string{
"golang.org/x/mobile/bind/testpkg",
}, "org.golang.custompkg", "CustomPkgTest")
}
func TestJavaSeqTest(t *testing.T) {
runTest(t, []string{
"golang.org/x/mobile/bind/testpkg",
"golang.org/x/mobile/bind/testpkg/secondpkg",
"golang.org/x/mobile/bind/testpkg/simplepkg",
}, "SeqTest")
}, "", "SeqTest")
}
// TestJavaSeqBench runs java test SeqBench.java, with the same
@ -38,10 +41,15 @@ func TestJavaSeqTest(t *testing.T) {
//
// while running the benchmark to see the results.
func TestJavaSeqBench(t *testing.T) {
runTest(t, []string{"golang.org/x/mobile/bind/benchmark"}, "SeqBench")
runTest(t, []string{"golang.org/x/mobile/bind/benchmark"}, "", "SeqBench")
}
func runTest(t *testing.T, pkgNames []string, javaCls string) {
// runTest runs the Android java test class specified with javaCls. If javaPkg is
// set, it is passed with the -javapkg flag to gomobile. The pkgNames lists the Go
// packages to bind for the test.
// This requires the gradle command in PATH and
// the Android SDK whose path is available through ANDROID_HOME environment variable.
func runTest(t *testing.T, pkgNames []string, javaPkg, javaCls string) {
if _, err := run("which gradle"); err != nil {
t.Skip("command gradle not found, skipping")
}
@ -84,7 +92,12 @@ func runTest(t *testing.T, pkgNames []string, javaCls string) {
}
}
buf, err := run("gomobile bind -o pkg.aar " + strings.Join(pkgNames, " "))
cmd := []string{"gomobile", "bind", "-o", "pkg.aar"}
if javaPkg != "" {
cmd = append(cmd, "-javapkg", javaPkg)
}
cmd = append(cmd, pkgNames...)
buf, err := run(strings.Join(cmd, " "))
if err != nil {
t.Logf("%s", buf)
t.Fatalf("failed to run gomobile bind: %v", err)