Skip to content

Commit e35fbaf

Browse files
committed
封装,实现多实例调用JNI
1 parent 53a2de3 commit e35fbaf

File tree

18 files changed

+1437
-637
lines changed

18 files changed

+1437
-637
lines changed

.idea/jarRepositories.xml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@ plugins {
22
id 'com.android.application'
33
id 'kotlin-android'
44
id 'kotlin-android-extensions'
5+
id 'com.github.dcendents.android-maven'
56
}
67

8+
group ='com.github.sandyz987'
9+
710
android {
811
compileSdkVersion 30
912
buildToolsVersion "30.0.3"
1013

1114
defaultConfig {
12-
applicationId "com.example.CalculatorJNI"
15+
applicationId "com.example.calculatorjni"
1316
minSdkVersion 21
1417
targetSdkVersion 30
1518
versionCode 1

app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3-
package="com.example.CalculatorJNI">
3+
package="com.example.calculatorjni">
44

55
<application
66
android:allowBackup="true"
@@ -9,7 +9,7 @@
99
android:roundIcon="@mipmap/ic_launcher_round"
1010
android:supportsRtl="true"
1111
android:theme="@style/Theme.CalculatorJNI">
12-
<activity android:name=".MainActivity">
12+
<activity android:name="com.example.calculatorjni.MainActivity">
1313
<intent-filter>
1414
<action android:name="android.intent.action.MAIN" />
1515

app/src/main/java/com/example/CalculatorJNI/jni/CalculatorJNI.java

Lines changed: 0 additions & 28 deletions
This file was deleted.

app/src/main/java/com/example/CalculatorJNI/MainActivity.kt renamed to app/src/main/java/com/example/calculatorjni/MainActivity.kt

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
package com.example.CalculatorJNI
1+
package com.example.calculatorjni
22

33
import android.annotation.SuppressLint
44
import android.os.Bundle
55
import androidx.appcompat.app.AppCompatActivity
6-
import com.example.CalculatorJNI.jni.CalculatorJNI
6+
import com.example.calculatorjni.jni.Calculator
77
import kotlinx.android.synthetic.main.activity_main.*
88

99
class MainActivity : AppCompatActivity() {
@@ -14,33 +14,39 @@ class MainActivity : AppCompatActivity() {
1414

1515

1616
button.setOnClickListener {
17+
val c = Calculator()
1718
Thread {
1819
et_exp.text.toString().let {
1920
it.removeSuffix(" ")
2021
if (it.isBlank()) {
21-
CalculatorJNI.setExpression("0")
22+
c.setExpression("0")
2223
} else {
23-
CalculatorJNI.setExpression(it)
24+
c.setExpression(it)
2425
}
2526
}
2627

27-
CalculatorJNI.formatExpression()
28-
CalculatorJNI.eval()
2928
var ans = 0.0
29+
3030
try {
31-
CalculatorJNI.setVariable('x', et_x.text.toString().toDouble())
32-
CalculatorJNI.setVariable('y', et_y.text.toString().toDouble())
33-
CalculatorJNI.clearVariable()
34-
ans = CalculatorJNI.getAns()
31+
32+
for (i in et_x.text.toString().toInt()..et_y.text.toString().toInt()) {
33+
c.clearVariable()
34+
c.setVariable('x', i.toDouble())
35+
ans += c.getAns()
36+
}
37+
38+
3539
} catch (e: Exception) {
3640
e.printStackTrace()
41+
} finally {
42+
c.destroy()
3743
}
3844

39-
4045
runOnUiThread {
4146
done(ans)
4247
}
4348
}.start()
49+
4450
}
4551

4652
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.example.calculatorjni.jni
2+
3+
/**
4+
*@author zhangzhe
5+
*@date 2021/3/24
6+
*@description
7+
*/
8+
9+
class Calculator {
10+
private var instanceId = 0
11+
private var _isDestroy = false
12+
val isDestroy get() = _isDestroy
13+
14+
init {
15+
instanceId = CalculatorJNI.newInstance()
16+
}
17+
18+
19+
private fun checkDestroy() {
20+
if (isDestroy) {
21+
throw IllegalStateException("the Calculator instance has destroyed!")
22+
}
23+
}
24+
25+
26+
private fun formatExpression() {
27+
checkDestroy()
28+
CalculatorJNI.formatExpression(instanceId)
29+
}
30+
31+
private fun eval() {
32+
checkDestroy()
33+
CalculatorJNI.eval(instanceId)
34+
}
35+
36+
/**
37+
* you should call @{destroy()} after used this instance.
38+
*/
39+
fun destroy() {
40+
CalculatorJNI.destroyInstance(instanceId)
41+
_isDestroy = true
42+
}
43+
44+
45+
fun setExpression(expression: String) {
46+
checkDestroy()
47+
expression.removeSuffix(" ")
48+
if (expression.length >= 1000)
49+
throw IllegalStateException("expression's length is too long! length: ${expression.length}")
50+
CalculatorJNI.setExpression(instanceId, expression)
51+
formatExpression()
52+
eval()
53+
}
54+
55+
fun clearVariable() {
56+
checkDestroy()
57+
CalculatorJNI.clearVariable(instanceId)
58+
}
59+
60+
fun getAns(): Double {
61+
checkDestroy()
62+
return CalculatorJNI.getAns(instanceId)
63+
}
64+
65+
fun setVariable(variable: Char, value: Double) {
66+
checkDestroy()
67+
CalculatorJNI.setVariable(instanceId, variable, value)
68+
}
69+
70+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.example.calculatorjni.jni;
2+
3+
import kotlin.jvm.Synchronized;
4+
5+
/**
6+
* @author zhangzhe
7+
* @date 2021/3/22
8+
* @description
9+
*/
10+
11+
public class CalculatorJNI {
12+
13+
static {
14+
System.loadLibrary("eval");
15+
}
16+
17+
@Synchronized
18+
protected static native int newInstance();
19+
20+
@Synchronized
21+
protected static native void destroyInstance(int instanceId);
22+
23+
@Synchronized
24+
protected static native void formatExpression(int instanceId);
25+
26+
@Synchronized
27+
protected static native void eval(int instanceId);
28+
29+
@Synchronized
30+
protected static native void clearVariable(int instanceId);
31+
32+
@Synchronized
33+
protected static native double getAns(int instanceId);
34+
35+
@Synchronized
36+
protected static native void setExpression(int instanceId, String expression);
37+
38+
@Synchronized
39+
protected static native void setVariable(int instanceId, char c, double value);
40+
}

app/src/main/jni/Android.mk

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ include $(CLEAR_VARS)
44
LOCAL_MODULE := eval
55
LOCAL_SRC_FILES:= \
66
eval.c\
7-
utils.c
7+
utils.c\
8+
eval_pro.cpp
89

910
LOCAL_LDLIBS += -llog -lOpenSLES -landroid
1011
#LOCAL_SHARED_LIBRARIES := liblog libOpenSLES libandroid

0 commit comments

Comments
 (0)