github.com/F4RD1N/gomobile@v1.0.1/cmd/gomobile/manifest.go (about)

     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 main
     6  
     7  import (
     8  	"encoding/xml"
     9  	"errors"
    10  	"fmt"
    11  	"html/template"
    12  )
    13  
    14  type manifestXML struct {
    15  	Activity activityXML `xml:"application>activity"`
    16  }
    17  
    18  type activityXML struct {
    19  	Name     string        `xml:"name,attr"`
    20  	MetaData []metaDataXML `xml:"meta-data"`
    21  }
    22  
    23  type metaDataXML struct {
    24  	Name  string `xml:"name,attr"`
    25  	Value string `xml:"value,attr"`
    26  }
    27  
    28  // manifestLibName parses the AndroidManifest.xml and finds the library
    29  // name of the NativeActivity.
    30  func manifestLibName(data []byte) (string, error) {
    31  	manifest := new(manifestXML)
    32  	if err := xml.Unmarshal(data, manifest); err != nil {
    33  		return "", err
    34  	}
    35  	if manifest.Activity.Name != "org.golang.app.GoNativeActivity" {
    36  		return "", fmt.Errorf("can only build an .apk for GoNativeActivity, not %q", manifest.Activity.Name)
    37  	}
    38  	libName := ""
    39  	for _, md := range manifest.Activity.MetaData {
    40  		if md.Name == "android.app.lib_name" {
    41  			libName = md.Value
    42  			break
    43  		}
    44  	}
    45  	if libName == "" {
    46  		return "", errors.New("AndroidManifest.xml missing meta-data android.app.lib_name")
    47  	}
    48  	return libName, nil
    49  }
    50  
    51  type manifestTmplData struct {
    52  	JavaPkgPath string
    53  	Name        string
    54  	LibName     string
    55  }
    56  
    57  var manifestTmpl = template.Must(template.New("manifest").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>`))