| 1 | // Copyright 2015 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package mainscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/cmd/gomobile`/ |
| 6 | |
| 7 | import ( |
| 8 | "encoding/xmlscip-go gomod github.com/golang/go/src go1.24.0 `encoding/xml`/" |
| 9 | "errorsscip-go gomod github.com/golang/go/src go1.24.0 errors/" |
| 10 | "fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/" |
| 11 | "html/templatescip-go gomod github.com/golang/go/src go1.24.0 `html/template`/" |
| 12 | ) |
| 13 | |
| 14 | type manifestXMLscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/cmd/gomobile`/manifestXML# struct { |
| 15 | Activityscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/cmd/gomobile`/manifestXML#Activity. activityXMLscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/cmd/gomobile`/activityXML# `xml:"application>activity"` |
| 16 | } |
| 17 | |
| 18 | type activityXMLscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/cmd/gomobile`/activityXML# struct { |
| 19 | Namescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/cmd/gomobile`/activityXML#Name. string `xml:"name,attr"` |
| 20 | MetaDatascip-go gomod golang.org/x/mobile . `golang.org/x/mobile/cmd/gomobile`/activityXML#MetaData. []metaDataXMLscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/cmd/gomobile`/metaDataXML# `xml:"meta-data"` |
| 21 | } |
| 22 | |
| 23 | type metaDataXMLscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/cmd/gomobile`/metaDataXML# struct { |
| 24 | Namescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/cmd/gomobile`/metaDataXML#Name. string `xml:"name,attr"` |
| 25 | Valuescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/cmd/gomobile`/metaDataXML#Value. string `xml:"value,attr"` |
| 26 | } |
| 27 | |
| 28 | // manifestLibName parses the AndroidManifest.xml and finds the library |
| 29 | // name of the NativeActivity. |
| 30 | func manifestLibNamescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/cmd/gomobile`/manifestLibName().(data []byte) (string, error) { |
| 31 | manifest := new(manifestXMLscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/cmd/gomobile`/manifestXML#) |
| 32 | if err := xmlscip-go gomod github.com/golang/go/src go1.24.0 `encoding/xml`/.Unmarshalscip-go gomod github.com/golang/go/src go1.24.0 `encoding/xml`/Unmarshal().(data, manifest); err != nil { |
| 33 | return "", err |
| 34 | } |
| 35 | if manifest.Activityscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/cmd/gomobile`/manifestXML#Activity..Namescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/cmd/gomobile`/activityXML#Name. != "org.golang.app.GoNativeActivity" { |
| 36 | return "", fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().("can only build an .apk for GoNativeActivity, not %q", manifest.Activityscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/cmd/gomobile`/manifestXML#Activity..Namescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/cmd/gomobile`/activityXML#Name.) |
| 37 | } |
| 38 | libName := "" |
| 39 | for _, md := range manifest.Activityscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/cmd/gomobile`/manifestXML#Activity..MetaDatascip-go gomod golang.org/x/mobile . `golang.org/x/mobile/cmd/gomobile`/activityXML#MetaData. { |
| 40 | if md.Namescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/cmd/gomobile`/metaDataXML#Name. == "android.app.lib_name" { |
| 41 | libName = md.Valuescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/cmd/gomobile`/metaDataXML#Value. |
| 42 | break |
| 43 | } |
| 44 | } |
| 45 | if libName == "" { |
| 46 | return "", errorsscip-go gomod github.com/golang/go/src go1.24.0 errors/.Newscip-go gomod github.com/golang/go/src go1.24.0 errors/New().("AndroidManifest.xml missing meta-data android.app.lib_name") |
| 47 | } |
| 48 | return libName, nil |
| 49 | } |
| 50 | |
| 51 | type manifestTmplDatascip-go gomod golang.org/x/mobile . `golang.org/x/mobile/cmd/gomobile`/manifestTmplData# struct { |
| 52 | JavaPkgPathscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/cmd/gomobile`/manifestTmplData#JavaPkgPath. string |
| 53 | Namescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/cmd/gomobile`/manifestTmplData#Name. string |
| 54 | LibNamescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/cmd/gomobile`/manifestTmplData#LibName. string |
| 55 | } |
| 56 | |
| 57 | var manifestTmplscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/cmd/gomobile`/manifestTmpl. = templatescip-go gomod github.com/golang/go/src go1.24.0 `html/template`/.Mustscip-go gomod github.com/golang/go/src go1.24.0 `html/template`/Must().(templatescip-go gomod github.com/golang/go/src go1.24.0 `html/template`/.Newscip-go gomod github.com/golang/go/src go1.24.0 `html/template`/New().("manifest").Parsescip-go gomod github.com/golang/go/src go1.24.0 `html/template`/Template#Parse().(` |
| 58 | <manifest |
| 59 | xmlns:android="http://schemas.android.com/apk/res/android" |
| 60 | package="{{.JavaPkgPath}}" |
| 61 | android:versionCode="1" |
| 62 | android:versionName="1.0"> |
| 63 | |
| 64 | <application android:label="{{.Name}}" android:debuggable="true"> |
| 65 | <activity android:name="org.golang.app.GoNativeActivity" |
| 66 | android:label="{{.Name}}" |
| 67 | android:configChanges="orientation|keyboardHidden"> |
| 68 | <meta-data android:name="android.app.lib_name" android:value="{{.LibName}}" /> |
| 69 | <intent-filter> |
| 70 | <action android:name="android.intent.action.MAIN" /> |
| 71 | <category android:name="android.intent.category.LAUNCHER" /> |
| 72 | </intent-filter> |
| 73 | </activity> |
| 74 | </application> |
| 75 | </manifest>`)) |