dart, add XmlElement

dart, flutter で XmlElementを追加する方法です。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import 'package:flutter/material.dart';
import 'package:xml/xml.dart' as xml;
import 'dart:async';
import 'dart:io';
import 'dart:core';
var bookshelfXml = '''<?xml version="1.0"?>
<bookshelf>
<book>
<title lang="english">Growing a Language</title>
<price>29.99</price>
</book>
<book>
<title lang="english">Learning XML</title>
<price>39.95</price>
</book>
</bookshelf>''';
var document = xml.parse(bookshelfXml);
var element_book = xml.XmlElement(xml.XmlName('book'));
var element_title = xml.xmlElement(xml.XmlName('title'))..attributes.add(xml.XmlAttribute(xml.XmlName('lang'), 'english'))..children.add(xml.XmlText('My book'));
var element_price = xml.XmlElement(xml.XmlName('price'))..children.add(xml.XmlText('19.95'));
element_book..children.add(element_title);
element_book..children.add(element_price);
document.rootElement.children.add(element_book);
import 'package:flutter/material.dart'; import 'package:xml/xml.dart' as xml; import 'dart:async'; import 'dart:io'; import 'dart:core'; var bookshelfXml = '''<?xml version="1.0"?> <bookshelf> <book> <title lang="english">Growing a Language</title> <price>29.99</price> </book> <book> <title lang="english">Learning XML</title> <price>39.95</price> </book> </bookshelf>'''; var document = xml.parse(bookshelfXml); var element_book = xml.XmlElement(xml.XmlName('book')); var element_title = xml.xmlElement(xml.XmlName('title'))..attributes.add(xml.XmlAttribute(xml.XmlName('lang'), 'english'))..children.add(xml.XmlText('My book')); var element_price = xml.XmlElement(xml.XmlName('price'))..children.add(xml.XmlText('19.95')); element_book..children.add(element_title); element_book..children.add(element_price); document.rootElement.children.add(element_book);
import 'package:flutter/material.dart';
import 'package:xml/xml.dart' as xml;
import 'dart:async';
import 'dart:io';
import 'dart:core';

var bookshelfXml = '''<?xml version="1.0"?>
    <bookshelf>
      <book>
        <title lang="english">Growing a Language</title>
        <price>29.99</price>
      </book>
      <book>
        <title lang="english">Learning XML</title>
        <price>39.95</price>
      </book>
    </bookshelf>''';
var document = xml.parse(bookshelfXml);

var element_book = xml.XmlElement(xml.XmlName('book'));
var element_title = xml.xmlElement(xml.XmlName('title'))..attributes.add(xml.XmlAttribute(xml.XmlName('lang'), 'english'))..children.add(xml.XmlText('My book'));
var element_price = xml.XmlElement(xml.XmlName('price'))..children.add(xml.XmlText('19.95'));
element_book..children.add(element_title);
element_book..children.add(element_price);
document.rootElement.children.add(element_book);

この方法でbook要素に title が My book, price が 19.95 の book が追加されます。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?xml version="1.0"?>
<bookshelf>
<book>
<title lang="english">Growing a Language</title>
<price>29.99</price>
</book>
<book>
<title lang="english">Learning XML</title>
<price>39.95</price>
</book>
<book>
<title lang="english">My book</title>
<price>19.95</price>
</book>
</bookshelf>
<?xml version="1.0"?> <bookshelf> <book> <title lang="english">Growing a Language</title> <price>29.99</price> </book> <book> <title lang="english">Learning XML</title> <price>39.95</price> </book> <book> <title lang="english">My book</title> <price>19.95</price> </book> </bookshelf>
<?xml version="1.0"?>
    <bookshelf>
      <book>
        <title lang="english">Growing a Language</title>
        <price>29.99</price>
      </book>
      <book>
        <title lang="english">Learning XML</title>
        <price>39.95</price>
      </book>
      <book>
        <title lang="english">My book</title>
        <price>19.95</price>
      </book>
    </bookshelf>

children.add に別のXmlDocumentのXmlNode, XmlElement などを配置すると例外が発生します。xml.XmlElement(xml.XmlName(“book”)) 等で生成または、xml.XmlElement.copy() を使用する必要があります。

Call Kotlin method from Flutter(FlutterからKotlinのコード内のメソッドを呼び出す)

MainActivity.kt (Kotlin)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package (your.app)
import android.content.res.Configuration
import android.os.Bundle
import android.os.PersistableBundle
import androidx.annotation.NonNull
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugins.GeneratedPluginRegistrant
import io.flutter.embedding.android.FlutterFragment
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import io.flutter.plugin.platform.PlatformViewsController
class MainActivity: FlutterActivity() {
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine)
MethodChannel(flutterEngine.dartExecutor, CHANNEL).setMethodCallHandler { methodCall, result ->
val args = methodCall.arguments
when (methodCall.method) {
"method1" -> {
val number = 10
result.success(number)
}
}
}
}
companion object {
private val CHANNEL = "com.your.app.flutter/method1"
}
}
package (your.app) import android.content.res.Configuration import android.os.Bundle import android.os.PersistableBundle import androidx.annotation.NonNull import io.flutter.embedding.android.FlutterActivity import io.flutter.embedding.engine.FlutterEngine import io.flutter.plugins.GeneratedPluginRegistrant import io.flutter.embedding.android.FlutterFragment import io.flutter.plugin.common.MethodCall import io.flutter.plugin.common.MethodChannel import io.flutter.plugin.common.MethodChannel.MethodCallHandler import io.flutter.plugin.common.MethodChannel.Result import io.flutter.plugin.platform.PlatformViewsController class MainActivity: FlutterActivity() { override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) { GeneratedPluginRegistrant.registerWith(flutterEngine) MethodChannel(flutterEngine.dartExecutor, CHANNEL).setMethodCallHandler { methodCall, result -> val args = methodCall.arguments when (methodCall.method) { "method1" -> { val number = 10 result.success(number) } } } } companion object { private val CHANNEL = "com.your.app.flutter/method1" } }
package (your.app)

import android.content.res.Configuration
import android.os.Bundle
import android.os.PersistableBundle
import androidx.annotation.NonNull
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugins.GeneratedPluginRegistrant

import io.flutter.embedding.android.FlutterFragment
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel

import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result

import io.flutter.plugin.platform.PlatformViewsController


class MainActivity: FlutterActivity() {
    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine)

        MethodChannel(flutterEngine.dartExecutor, CHANNEL).setMethodCallHandler { methodCall, result ->
            val args = methodCall.arguments
            when (methodCall.method) {
                "method1" -> {
                    val number = 10
                    result.success(number)
                }
            }
        }
    }

    companion object {
        private val CHANNEL = "com.your.app.flutter/method1"
    }

}

Dart

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
static const platform = const MethodChannel('com.your.app.flutter/method1');
Future<void> _Method1() async{
int _number;
try {
final int result = await platform.invokeMethod('method1');
_number = result;
setState(() {
number = _number;
});
} on PlatformException catch (e)
{
setState(() {
number = -1;
});
}
}
int number;
Widget ShowNumber()
{
Text(number.toString());
}
static const platform = const MethodChannel('com.your.app.flutter/method1'); Future<void> _Method1() async{ int _number; try { final int result = await platform.invokeMethod('method1'); _number = result; setState(() { number = _number; }); } on PlatformException catch (e) { setState(() { number = -1; }); } } int number; Widget ShowNumber() { Text(number.toString()); }
static const platform = const MethodChannel('com.your.app.flutter/method1');

Future<void> _Method1() async{
    int _number;
    try {
      final int result = await platform.invokeMethod('method1');
      _number = result;

      setState(() {
        number = _number;
      });

    } on PlatformException catch (e)
    {
      setState(() {
        number = -1;
      });
    }
}

int number;

Widget ShowNumber()
{
  Text(number.toString());
}

_Method1() を呼び出すと Kotlin側のmethod1 -> {} が実行され、結果が Flutter側の result に戻ります。

String testString = “abc”;
platform.invokeMethod(‘method1’, testString); でtestStringを引数にできます。
Kotlin側の methodCall.arguments で引数を取得できます。

io.flutter.app.FlutterActivity の FlutterActivity には FlutterEngine が存在しないのでGeneratedPluginRegistrant.registerWith(this) ではregistできません。またMethodCall の flutterView の代わり(java では getFlutterView() )に flutterEngine.dartExecutor を使用します。

2019-12-05の時点で app/build.gradle 内で Cannot resolve symbol ‘GradleException’ が、MainActivity.kt で Unresolved reference: NonNull がそれぞれ赤字で表示されます。2019-12-07のAndroid Studio アップデートとFlutter Pluginのアップデートで表示されなくなったようです。

こちらのサイトの情報が参考になりました。

https://github.com/flutter/flutter/issues/41102
https://qiita.com/tasogarei/items/bf3761139d46c43d5896
https://qiita.com/unsoluble_sugar/items/ae42b5faf52a491f6470
https://flutter.keicode.com/basics/method-channel-java.php

Google Play Licensing Verification Library (LVL)

Android Studio 3.5 でGoogle Play Licensing Verification Library (LVL) をインポートする方法です。

  1. File – New – Import Module .. を選択して Source directory: に [ANDROID_SDK]/extras/google/market_licensing/library を指定します。Finishをクリックします。プロジェクト内にlibrary ディレクトリーが作成され、インポートされます。
  2. library/src/main/java/com を android/app/src/main/java/com にコピーします。
  3. AndroidManifest.xmlに次の要素を追加します。
<!-- Devices >= 3 have version of Google Play that supports licensing. -->
<uses-sdk android:minSdkVersion="3" />
<!-- Required permission to check licensing. -->
<uses-permission android:name="com.android.vending.CHECK_LICENSE" />