├── go.mod ├── .gitignore ├── testgo ├── .DS_Store └── src │ └── not.expert │ └── test │ └── test.go ├── assets ├── Screenshot 2022-12-12 at 14.10.01.png ├── Screenshot 2022-12-12 at 14.10.21.png ├── Screenshot 2022-12-12 at 14.12.04.png ├── Screenshot 2022-12-12 at 14.13.04.png └── Screenshot 2022-12-12 at 14.14.24.png └── README.md /go.mod: -------------------------------------------------------------------------------- 1 | module test 2 | 3 | go 1.19 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.DS_Store 2 | testgo/bin 3 | testgo/pkg 4 | -------------------------------------------------------------------------------- /testgo/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/burhon97/gomobile-test/HEAD/testgo/.DS_Store -------------------------------------------------------------------------------- /assets/Screenshot 2022-12-12 at 14.10.01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/burhon97/gomobile-test/HEAD/assets/Screenshot 2022-12-12 at 14.10.01.png -------------------------------------------------------------------------------- /assets/Screenshot 2022-12-12 at 14.10.21.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/burhon97/gomobile-test/HEAD/assets/Screenshot 2022-12-12 at 14.10.21.png -------------------------------------------------------------------------------- /assets/Screenshot 2022-12-12 at 14.12.04.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/burhon97/gomobile-test/HEAD/assets/Screenshot 2022-12-12 at 14.12.04.png -------------------------------------------------------------------------------- /assets/Screenshot 2022-12-12 at 14.13.04.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/burhon97/gomobile-test/HEAD/assets/Screenshot 2022-12-12 at 14.13.04.png -------------------------------------------------------------------------------- /assets/Screenshot 2022-12-12 at 14.14.24.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/burhon97/gomobile-test/HEAD/assets/Screenshot 2022-12-12 at 14.14.24.png -------------------------------------------------------------------------------- /testgo/src/not.expert/test/test.go: -------------------------------------------------------------------------------- 1 | package test 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func Greeting(name string) string { 8 | message := fmt.Sprintf("Hello from Gomobile, %s", name) 9 | return message 10 | } 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # Creating gomobile project 3 | 4 | ### - Downloading gomobile and init 5 | ``` 6 | $ go install golang.org/x/mobile/cmd/gomobile@latest 7 | $ export PATH=$PATH:'/Users/code_joker/go/bin' 8 | $ gomobile init 9 | ``` 10 | 11 | ### Creating new go mod: 12 | ``` 13 | $ go mod init test 14 | ``` 15 | 16 | ## Set GOPATH 17 | Lets create our workspace and the same time lets use the workspace directory is our $GOPATH too. 18 | ``` 19 | $ mkdir -p /Users/code_joker/Documents/testgo 20 | $ cd /Users/code_joker/Documents/testgo 21 | $ export GOPATH=`pwd` 22 | ``` 23 | 24 | ## Set Android related envs 25 | Lets export the Android SDK and Android NDK 26 | ``` 27 | $ export ANDROID_NDK_HOME=/Users/code_joker/Library/Android/sdk/ndk/19.2.5345600 28 | $ export ANDROID_HOME=/Users/code_joker/Library/Android/sdk 29 | ``` 30 | 31 | ## Downloading gobind and gomobile 32 | Gobind is a tool that generates language bindings that make it possible to call Go functions from Java and Objective-C. It is called internally by gomobile which can help us build cross-platform applications. We need this two to build our mobile app library. 33 | 34 | Optionally, one can develop an entire mobile application using build command of gomobile. But we wont be doing that. We will, however, develop a library that can be used by mobile applications. And to build this, we will use bind command of gomobile. 35 | 36 | Download gobind and gomobile 37 | ``` 38 | $ go install golang.org/x/mobile/cmd/gomobile@latest 39 | $ go get golang.org/x/mobile/cmd/gobind 40 | ``` 41 | 42 | And export the bin as part of the $PATH and init gomobile 43 | ``` 44 | $ export PATH=$PATH:'/Users/code_joker/go/bin' 45 | $ gomobile init 46 | ``` 47 | 48 | ## The sample App 49 | Lets create the go project structure for our sample app. Let's sc directory which contains our Go package not.expert and finally our package test 50 | ``` 51 | $ mkdir -p $GOPATH/src 52 | $ mkdir -p $GOPATH/src/not.expert 53 | $ mkdir -p $GOPATH/src/not.expert/test 54 | ``` 55 | 56 | Create the test.go file under test package 57 | ``` 58 | $ touch $GOPATH/src/not.expert/test/test.go 59 | ``` 60 | 61 | Put this as contents of the test.go 62 | ``` 63 | package test 64 | 65 | import ( 66 | "fmt" 67 | ) 68 | 69 | func Greet (name string) string { 70 | message := fmt.Sprintf("Hello World, &s", name) 71 | return message 72 | } 73 | ``` 74 | 75 | ## Build the library 76 | Using gomobile, we can then build the libraries respective to the traget platform, in our case ios and android. 77 | 78 | Build framework for ios 79 | ``` 80 | $ gomobile bind -target=ios -tags nowatchdog -o $GOPATH/build/test.xcframework -v $GOPATH/src/not.expert/test 81 | ``` 82 | 83 | And compile your code for ios: 84 | ``` 85 | $ gomobile build -target=ios -tags nowatchdog -v $GOPATH/src/not.expert/test 86 | ``` 87 | ----------------------------------------------------------------------------- 88 | 89 | And build aar for android 90 | ``` 91 | $ gomobile bind -target=android -o $GOPATH/build/test.aar -v $GOPATH/src/not.expert/test 92 | ``` 93 | 94 | And compile your code for android: 95 | ``` 96 | $ gomobile build -v $GOPATH/src/not.expert/test 97 | ``` 98 | ----------------------------------------------------------------------------- 99 | 100 | ## Create app in Xcode(ios) 101 | Lets create native ios app in Xcode 102 | 103 | Open Xcode and create a new Xcode app 104 | 105 | ![Create a new Xcode project](https://miro.medium.com/max/720/1*6H5euen0mZ7MQYQ_QteElw.webp) 106 | 107 | Click **Next** button 108 | 109 | Then: 110 | + Product name: **testgo** 111 | + organization identifier: **not.expert** 112 | + Interface: **Storyboard** 113 | 114 | Click **Next**, your project is created. 115 | 116 | ### Importing a Gomobile framework 117 | Now we are going to import our **test.xcframework** so that we can interact with it in Swift. Open Finder, and navigate to the frameworks folder. Then drag the **test.xcframework** file into Xcode at the bottom of the Project navigator. A dialog will open to confirm the action, be sure to check **Copy items if needed**. And you can follow the [link](https://denbeke.be/blog/programming/go-mobile-example-running-caddy-ios/#:~:text=Building%20an%20iOS%20app%20with%20the%20framework) 118 | 119 | 120 | 121 | And run your Xcode project 122 | 123 | ------------------------------------------------- 124 | 125 | 126 | # Create app in Android Studio(android) 127 | Lets create native android app in Android Studio 128 | 129 | Open Android Studio and create **New project** 130 | And choose **Empty activity**, click **Next** 131 | And set your project name **Name: test**, click **Finish**. 132 | Your native android app is created. 133 | 134 | 135 | ## Import our go project to Android Studio Project 136 | Let's import our go project which compile(.aar & .jar) to Android Studio Project 137 | 138 | + Copy your go project test.aar and test-sources.jar 139 | + Paste to **test/app/libs** your native android app which create 140 | + Add **implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'])** to test/app/build.gradle in dependencies: {} 141 | + And Synchronize and rebuild the project 142 | 143 | ## And inport your go project into MainActivity 144 | ``` 145 | package not.expert.test; 146 | 147 | import androidx.appcompat.app.AppCompatActivity; 148 | import test.Test; 149 | import android.os.Bundle; 150 | 151 | public class MainActivity extends AppCompatActivity { 152 | 153 | @Override 154 | protected void onCreate(Bundle savedInstanceState) { 155 | super.onCreate(savedInstanceState); 156 | setContentView(R.layout.activity_main); 157 | 158 | Test.greeting("android"); 159 | } 160 | } 161 | ``` 162 | 163 | And Run your android project 164 | 165 | 166 | 167 | 168 | --------------------------------------------------------------------------------