Skip to content

Commit 50147c5

Browse files
committed
c 和 java 互调
1 parent e2ee64f commit 50147c5

File tree

6 files changed

+116
-5
lines changed

6 files changed

+116
-5
lines changed

build.bat

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1+
chcp 65001
12
set JAVA_HOME=D:\Program Files\Java\jdk1.8.0_60
23
set PATH=%PATH%;%JAVA_HOME%\bin;E:\Android\sdk\ndk-bundle\android-ndk-r20
34
java -version
45
rem ndk-build NDK_PROJECT_PATH=. NDK_APPLICATION_MK=Application.mk APP_BUILD_SCRIPT=Android.mk
56

7+
call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\vcvars32.bat"
68
mkdir dyzbuild
79
pushd dyzbuild
810
cmake -G "Visual Studio 14 2015 Win64" ..
9-
call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\vcvars32.bat"
1011
msbuild CppCallJni.vcxproj
1112
Debug\CppCallJni.exe
1213
dumpbin /dependents Debug\CppCallJni.exe
1314
popd
1415

16+
1517
echo "调试时如果报异常,记得不要点击中断,要点击继续"
1618

1719
pause

callc/CallC.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22

33
public class CallC{
44
public native String[] CInterface(String[] name, int[] age, float[] height, int num);
5+
public native int CInterfaceTest();
56
static {
67
System.loadLibrary("CLibrary");
78
}
9+
public void JInterface()
10+
{
11+
System.out.println("JInterface");
12+
}
813
public static void main(String[] args){
914
int idx = 0;
1015
int num = 2;
@@ -18,7 +23,9 @@ public static void main(String[] args){
1823
}
1924
String[] vals = new CallC().CInterface(name, age, height, num);
2025
for (idx = 0; idx < vals.length; ++idx){
21-
System.out.println(vals[idx]);
26+
System.out.println("vals:[" + idx +"]:" + vals[idx]);
2227
}
28+
int ret = new CallC().CInterfaceTest();
29+
System.out.println("ret :" + ret);
2330
}
2431
};

callc/build.bat

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
set JAVA_HOME=D:\Program Files\Java\jdk1.8.0_60
2-
set PATH=%PATH%;%JAVA_HOME%\bin;E:\Android\sdk\ndk-bundle\android-ndk-r20
2+
set PATH=%PATH%;%JAVA_HOME%\bin;
3+
set PATH=%PATH%;E:\Android\sdk\ndk-bundle\android-ndk-r20;D:\Android\ndk\android-ndk-r19c
34
javac -encoding utf-8 -d . callc.java
5+
javap -s com.durongze.jni.CallC
46
rem javah -jni com.durongze.jni.CallC
57
javac -encoding utf-8 -h . callc.java
68

79
rem ndk-build NDK_PROJECT_PATH=. NDK_APPLICATION_MK=Application.mk APP_BUILD_SCRIPT=Android.mk
8-
10+
call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\vcvars32.bat"
911
mkdir dyzbuild
1012
pushd dyzbuild
1113
cmake -G "Visual Studio 14 2015 Win64" ..
12-
call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\vcvars32.bat"
1314
msbuild CLibrary.vcxproj
1415
popd
1516

callc/com_durongze_jni_CallC.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,74 @@
22
#include <jni.h>
33
/* Header for class com_durongze_jni_CallC */
44
#include <stdio.h>
5+
#include <string.h>
6+
#include <errno.h>
7+
58
#ifndef _Included_com_durongze_jni_CallC
69
#define _Included_com_durongze_jni_CallC
10+
711
#ifdef __cplusplus
812
#if __cplusplus
913
extern "C" {
1014
#endif
1115
#endif
16+
17+
#define LOGV(...) printf(__VA_ARGS__)
18+
#define LOGD(...) printf(__VA_ARGS__)
19+
#define LOGI(...) printf(__VA_ARGS__)
20+
#define LOGW(...) printf(__VA_ARGS__)
21+
#define LOGE(...) printf(__VA_ARGS__)
22+
23+
void TestMain()
24+
{
25+
int i;
26+
signed char c;
27+
short s;
28+
float f;
29+
30+
c = -1;
31+
f = (float)c;
32+
LOGE("1. c:0x%x\n", c);
33+
LOGE("1. f:%lf\n", f);
34+
c = 0;
35+
f = c;
36+
LOGE("2. c:0x%x\n", c);
37+
LOGE("2. f:%lf\n", f);
38+
c = -1;
39+
f = c;
40+
for (int i = 0; i < 1000; i++){
41+
f = f + c;
42+
}
43+
LOGI("3. f + c:%lf\n", f);
44+
i = -1;
45+
f = i;
46+
for (int idx = 0; idx < 1000; idx ++){
47+
f = f + i;
48+
}
49+
LOGI("3. f + i:%lf\n", f);
50+
51+
c = 0;
52+
i = 1;
53+
f = (!c)*(i);
54+
for (int idx = 0; idx < 5; idx++){
55+
f = f + f;
56+
}
57+
LOGE("4. f:%lf\n", f);
58+
59+
s = -1;
60+
f = s;
61+
LOGE("5. f:%lf\n", f);
62+
}
1263
/*
1364
* Class: com_durongze_jni_CallC
1465
* Method: CInterface
1566
* Signature: ([Ljava/lang/String;[I[FI)[Ljava/lang/String;
1667
*/
68+
// Java_com_example_myapplication_MainActivity_CInterface
1769
JNIEXPORT jobjectArray JNICALL Java_com_durongze_jni_CallC_CInterface
1870
(JNIEnv *env, jobject, jobjectArray names, jintArray ages, jfloatArray heights, jint num)
1971
{
72+
TestMain();
2073
jobjectArray result;
2174
jclass intArrCls = env->FindClass("java/lang/String");
2275
result = env->NewObjectArray(num, intArrCls, NULL);
@@ -38,6 +91,28 @@ JNIEXPORT jobjectArray JNICALL Java_com_durongze_jni_CallC_CInterface
3891
return result;
3992
}
4093

94+
JNIEXPORT jint JNICALL Java_com_durongze_jni_CallC_CInterfaceTest
95+
(JNIEnv *env, jobject)
96+
{
97+
TestMain();
98+
#if 0
99+
FILE* fp = fopen("/data/local/tmp/test.txt", "w+");
100+
if (fp == NULL) {
101+
return errno;
102+
}
103+
char buf[64] = {"xxxxxxxxxxxxxxx"};
104+
fwrite(buf, strlen(buf), 1, fp);
105+
fclose(fp);
106+
return strlen(buf);
107+
#else
108+
jclass jCallC = env->FindClass("com/durongze/jni/CallC");
109+
jobject jObject = env->AllocObject(jCallC);
110+
jmethodID jMethodId = env->GetMethodID(jCallC, "JInterface", "()V");
111+
env->CallVoidMethod(jObject, jMethodId);
112+
return 77;
113+
#endif
114+
}
115+
41116
#ifdef __cplusplus
42117
#if __cplusplus
43118
}

callc/com_durongze_jni_CallC.h

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

test/FileList.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,24 @@ public byte[] ReadBinary(File f) throws Exception
4545
}
4646
return data;
4747
}
48+
49+
public byte[] ReadText(File f) throws Exception
50+
{
51+
InputStream in = new FileInputStream(f);
52+
byte[] data = new byte[(int)f.length()];
53+
try {
54+
System.out.println("f size:" + f.length());
55+
in.read(data);
56+
OutputStream out = new FileOutputStream(new File("d:\\xx.txt"), true);
57+
// OutputStream out = new FileOutputStream(new File("d:\\xx.txt"));
58+
out.write(data);
59+
out.close();
60+
} catch (Exception e) {
61+
// System.out.println("dir:");
62+
}
63+
return data;
64+
}
65+
4866
public void ProcAllFile(File[] fileList) throws Exception
4967
{
5068
System.out.println("ShowFilePath");

0 commit comments

Comments
 (0)