提交 a92452ad 编写于 作者: 杜庆泉's avatar 杜庆泉

增加android 混编 kotlin/java 代码示例

上级 8ded75ca
...@@ -149,6 +149,14 @@ ...@@ -149,6 +149,14 @@
} }
}, },
{
"path": "pages/SyntaxCase/MixNativeCode",
"style": {
"navigationBarTitleText": "",
"enablePullDownRefresh": false
}
},
{ {
"path": "pages/SyntaxCase/instanceTest", "path": "pages/SyntaxCase/instanceTest",
"style": { "style": {
......
<template>
<view>
<page-head :title="title"></page-head>
<view class="uni-btn-v uni-common-mt">
// #ifdef APP-ANDROID && UNI-APP-X
<button @tap="callKotlinMethodGetInfoTest">调用kotlin方法</button>
<button @tap="callJavaMethodGetInfoTest">调用java方法(需自定义基座)</button>
// #endif
</view>
</view>
</template>
<script>
// #ifdef APP-ANDROID && UNI-APP-X
import { callKotlinMethodGetInfo, callJavaMethodGetInfo} from "../../uni_modules/uts-syntaxcase";
// #endif
export default {
data() {
return {
title: 'UTS混编示例',
}
},
methods: {
// #ifdef APP-ANDROID && UNI-APP-X
callKotlinMethodGetInfoTest: function () {
let phoneInfo = callKotlinMethodGetInfo();
uni.showToast({
title:phoneInfo
})
},
callJavaMethodGetInfoTest: function () {
let javaInfo = callJavaMethodGetInfo();
uni.showToast({
title:javaInfo
})
},
// #endif
}
}
</script>
<style>
</style>
\ No newline at end of file
...@@ -21,52 +21,48 @@ ...@@ -21,52 +21,48 @@
doLogTest() { doLogTest() {
if(getLog("") != '{"type":"string","value":""}'){ if(getLog("") != '{"type":"string","value":""}'){
this.testRet = "测试失败" this.testRet = "测试失败1"
return; return;
} }
if(getLog("字符串打印测试") != '{"type":"string","value":"字符串打印测试"}'){ if(getLog("字符串打印测试") != '{"type":"string","value":"字符串打印测试"}'){
this.testRet = "测试失败" this.testRet = "测试失败2"
return; return;
} }
if(getLog(2023) != '{"subType":"number","type":"number","value":"2023"}'){ if(getLog(2023) != '{"subType":"number","type":"Int","value":"2023"}'){
this.testRet = "测试失败" this.testRet = "测试失败3"
return; return;
} }
// console.log(getLog(2023.0))
// if(getLog(2023.0) != '{"type":"number","value":"2023"}'){ if(getLog(2023.002 as number) != '{"subType":"number","type":"Double","value":"2023.002"}'){
// this.testRet = "测试失败" this.testRet = "测试失败4"
// return;
// }
if(getLog(2023.002 as number) != '{"subType":"number","type":"number","value":"2023.002"}'){
this.testRet = "测试失败"
return; return;
} }
if(logObjectTest() != '{"className":"uts.sdk.modules.utsApiTest.ParamOptions","subType":"object","__$originalPosition":"","type":"object","value":{"methods":[],"properties":[{"name":"array","subType":"array","className":"io.dcloud.uts.UTSArray","type":"object","value":{"properties":[{"name":0,"type":"string","value":"1"},{"name":1,"type":"string","value":"2"},{"name":2,"type":"string","value":"3"}]}},{"name":"title","type":"string","value":"logObjectTest"}]}}'){ if(logObjectTest() != '{"className":"uts.sdk.modules.utsApiTest.ParamOptions","subType":"object","__$originalPosition":"","type":"object","value":{"methods":[],"properties":[{"name":"array","subType":"array","className":"io.dcloud.uts.UTSArray","type":"object","value":{"properties":[{"name":{"value":{"value":0}},"type":"string","value":"1"},{"name":{"value":{"value":1}},"type":"string","value":"2"},{"name":{"value":{"value":2}},"type":"string","value":"3"}]}},{"name":"title","type":"string","value":"logObjectTest"}]}}'){
this.testRet = "测试失败" this.testRet = "测试失败5"
return; return;
} }
if(logFunctionTest() != '{"parameter":[],"type":"function"}'){ if(logFunctionTest() != '{"parameter":[],"type":"function"}'){
this.testRet = "测试失败" this.testRet = "测试失败6"
return; return;
} }
if(logClassTest() != '{"className":"uts.sdk.modules.utsApiTest.C","subType":"object","__$originalPosition":"","type":"object","value":{"methods":[],"properties":[{"name":"name","type":"string","value":"ccc"},{"parameter":["string"],"name":"sayBye","type":"function"}]}}'){ if(logClassTest() != '{"className":"uts.sdk.modules.utsApiTest.C","subType":"object","__$originalPosition":"","type":"object","value":{"methods":[],"properties":[{"name":"name","type":"string","value":"ccc"},{"parameter":["string"],"name":"sayBye","type":"function"}]}}'){
this.testRet = "测试失败" this.testRet = "测试失败7"
return; return;
} }
/** /**
* uvue 和 vue 因为 java 版本不同,所以这里的打印格式可能存在差异 * uvue 和 vue 因为 java 版本不同,所以这里的打印格式可能存在差异
*/ */
if(logFileTest() != '{"className":"java.io.File","subType":"object","type":"object","value":{"methods":[],"properties":[]}}'){ if(logFileTest() != '{"className":"java.io.File","subType":"object","type":"object","value":{"methods":[],"properties":[]}}'){
this.testRet = "测试失败" this.testRet = "测试失败8"
return; return;
} }
if(logDateTest() != '{"className":"io.dcloud.uts.Date","subType":"date","type":"object","value":"Sat Aug 08 1998 08:00:00 GMT+0800"}'){ if(logDateTest() != '{"className":"io.dcloud.uts.Date","subType":"date","type":"object","value":"Sat Aug 08 1998 08:00:00 GMT+0800"}'){
this.testRet = "测试失败" this.testRet = "测试失败9"
return; return;
} }
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
<script> <script>
import { getLog,logObjectTest,logClassTest,logFunctionTest,logFileTest,logDateTest,frequentlyObjectTest } from '../../uni_modules/uts-api-test' import { getLog,logObjectTest,logClassTest,logFunctionTest,logFileTest,logDateTest,logUTSJSONObjectTest } from '../../uni_modules/uts-api-test'
export default { export default {
...@@ -21,58 +21,51 @@ ...@@ -21,58 +21,51 @@
doLogTest() { doLogTest() {
if(getLog("") != '{"type":"string","value":""}'){ if(getLog("") != '{"type":"string","value":""}'){
this.testRet = "测试失败" this.testRet = "测试失败1"
return; return;
} }
if(getLog("字符串打印测试") != '{"type":"string","value":"字符串打印测试"}'){ if(getLog("字符串打印测试") != '{"type":"string","value":"字符串打印测试"}'){
this.testRet = "测试失败" this.testRet = "测试失败2"
return; return;
} }
if(getLog(2023) != '{"subType":"number","type":"Int","value":"2023"}'){
if(getLog(2023) != '{"type":"number","value":"2023"}'){ this.testRet = "测试失败3"
this.testRet = "测试失败"
return;
}
if(getLog(2023.0) != '{"type":"number","value":"2023"}'){
this.testRet = "测试失败"
return; return;
} }
if(getLog(2023.002) != '{"type":"number","value":"2023.002"}'){ if(getLog(2023.002 as number) != '{"subType":"number","type":"Double","value":"2023.002"}'){
this.testRet = "测试失败" this.testRet = "测试失败4"
return; return;
} }
if(logObjectTest() != '{"className":"uts.sdk.modules.utsApiTest.ParamOptions","subType":"object","__$originalPosition":"","type":"object","value":{"methods":[],"properties":[{"name":"array","subType":"array","className":"io.dcloud.uts.UTSArray","type":"object","value":{"properties":[{"name":0,"type":"string","value":"1"},{"name":1,"type":"string","value":"2"},{"name":2,"type":"string","value":"3"}]}},{"name":"title","type":"string","value":"logObjectTest"}]}}'){ if(logObjectTest() != '{"className":"uts.sdk.modules.utsApiTest.ParamOptions","subType":"object","__$originalPosition":"","type":"object","value":{"methods":[],"properties":[{"name":"array","subType":"array","className":"io.dcloud.uts.UTSArray","type":"object","value":{"properties":[{"name":{"value":{"value":0}},"type":"string","value":"1"},{"name":{"value":{"value":1}},"type":"string","value":"2"},{"name":{"value":{"value":2}},"type":"string","value":"3"}]}},{"name":"title","type":"string","value":"logObjectTest"}]}}'){
this.testRet = "测试失败" this.testRet = "测试失败5"
return; return;
} }
if(logFunctionTest() != '{"parameter":[],"type":"function"}'){ if(logFunctionTest() != '{"parameter":[],"type":"function"}'){
this.testRet = "测试失败" this.testRet = "测试失败6"
return; return;
} }
if(logClassTest() != '{"className":"uts.sdk.modules.utsApiTest.C","subType":"object","__$originalPosition":"","type":"object","value":{"methods":[],"properties":[{"name":"name","type":"string","value":"ccc"},{"parameter":["string"],"name":"sayBye","type":"function"}]}}'){ if(logClassTest() != '{"className":"uts.sdk.modules.utsApiTest.C","subType":"object","__$originalPosition":"","type":"object","value":{"methods":[],"properties":[{"name":"name","type":"string","value":"ccc"},{"parameter":["string"],"name":"sayBye","type":"function"}]}}'){
this.testRet = "测试失败" this.testRet = "测试失败7"
return; return;
} }
if(logFileTest() != '{"className":"java.io.File","subType":"object","type":"object","value":{"methods":[],"properties":[{"name":"filePath","type":"null","value":"null"},{"name":"path","type":"string","value":"/sdcard/temp/1.txt"},{"name":"prefixLength","subType":"number","type":"Int","value":"1"},{"name":"status","type":"null","value":"null"}]}}'){ /**
this.testRet = "测试失败" * uvue 和 vue 因为 java 版本不同,所以这里的打印格式可能存在差异
*/
if(logFileTest() != '{"className":"java.io.File","subType":"object","type":"object","value":{"methods":[],"properties":[]}}'){
this.testRet = "测试失败8"
return; return;
} }
if(logDateTest() != '{"className":"io.dcloud.uts.Date","subType":"date","type":"object","value":"Sat Aug 08 1998 08:00:00 GMT+0800"}'){ if(logDateTest() != '{"className":"io.dcloud.uts.Date","subType":"date","type":"object","value":"Sat Aug 08 1998 08:00:00 GMT+0800"}'){
this.testRet = "测试失败" this.testRet = "测试失败9"
return; return;
} }
if(!frequentlyObjectTest()){
this.testRet = "测试失败"
return;
}
this.testRet = "测试完成" this.testRet = "测试完成"
} }
......
...@@ -78,7 +78,12 @@ ...@@ -78,7 +78,12 @@
}, { }, {
name: "实例测试示例", name: "实例测试示例",
url: "SyntaxCase/instanceTest" url: "SyntaxCase/instanceTest"
}] as Page[] },
{
name: "混编测试示例",
url: "SyntaxCase/MixNativeCode"
},
] as Page[]
}, },
{ {
name: "日志打印", name: "日志打印",
......
package uts.sdk.modules.utsSyntaxcase;
public class JavaUser {
public String name;
public int age;
public JavaUser(String name, int age) {
this.name = name;
this.age = age;
}
public String toString() {
return "Name: " + name + ", Age: " + age;
}
}
package uts.sdk.modules.utsSyntaxcase
import android.os.Build
import io.dcloud.uts.UTSAndroid
object NativeCode {
fun getPhoneInfo():String{
return "${Build.BOARD}-${Build.USER}"
}
fun finishActivity(){
UTSAndroid.getUniActivity()?.finish()
}
fun getJavaUser():JavaUser{
return JavaUser("张三",12)
}
}
\ No newline at end of file
...@@ -145,7 +145,16 @@ export function request(url : string) : RequestTask | null { ...@@ -145,7 +145,16 @@ export function request(url : string) : RequestTask | null {
return new RequestTaskImpl(url) return new RequestTaskImpl(url)
} }
// #ifdef APP-ANDROID // #ifdef APP-ANDROID
// #ifdef UNI-APP-X // #ifdef UNI-APP-X
export function callKotlinMethodGetInfo():String {
return NativeCode.getPhoneInfo()
}
export function callJavaMethodGetInfo():String {
return new JavaUser("jack",12).name
}
import KeyEvent from 'android.view.KeyEvent'; import KeyEvent from 'android.view.KeyEvent';
import Configuration from 'android.content.res.Configuration'; import Configuration from 'android.content.res.Configuration';
import Bundle from 'android.os.Bundle'; import Bundle from 'android.os.Bundle';
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册