E:\AndroidProject\TheTestPro\NDKDemo\app\build\intermediates\javac\debug\classes>javap -s -p com.lwp.ndkdemo.MainActivity
Compiled from "MainActivity.java"
public class com.lwp.ndkdemo.MainActivity extends androidx.appcompat.app.AppCompatActivity {
public static java.lang.String TAG;
descriptor: Ljava/lang/String;
public java.lang.String s;
descriptor: Ljava/lang/String;
public static int count;
descriptor: I
private java.util.Date date;
descriptor: Ljava/util/Date;
public com.lwp.ndkdemo.MainActivity();
descriptor: ()V
public native java.lang.String accessFiled();
descriptor: ()Ljava/lang/String;
public native void accessStaticFiled();
descriptor: ()V
public native java.lang.String accessStaticMethod();
descriptor: ()Ljava/lang/String;
public static java.lang.String getUUID();
descriptor: ()Ljava/lang/String;
public native int accessMethod();
descriptor: ()I
public int genRandomInt(int);
descriptor: (I)I
public native long accessConstructor();
descriptor: ()J
protected void onCreate(android.os.Bundle);
descriptor: (Landroid/os/Bundle;)V
public native java.lang.String stringFromJNI();
descriptor: ()Ljava/lang/String;
public native java.lang.String stringFromJNITwo();
descriptor: ()Ljava/lang/String;
public native java.lang.String hehedaFromJNI();
descriptor: ()Ljava/lang/String;
static {};
descriptor: ()V
}
MainActivity
package com.lwp.ndkdemo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import java.util.Date;
import java.util.Random;
import java.util.UUID;
public class MainActivity extends AppCompatActivity {
// Used to load the 'native-lib' library on application startup.
static {
System.loadLibrary("native-lib");
}
//***********************************************
public static String TAG = "MainActivity";
//待修改的属性,一会儿我们通过C++把它修改为super shadow
public String s = "shadow";
//这个方法就是触发点,告诉C/C++层代码去修改上面的成员属性s
public native String accessFiled();
//***********************************************
//静态属性
public static int count = 3;
public native void accessStaticFiled();
//触发native层调用静态方法
public native String accessStaticMethod();
//静态方法,用来产生一个随机的UUID字符串
public static String getUUID(){
return UUID.randomUUID().toString();
}
//触发native层调用非静态方法
public native int accessMethod();
////产生指定范围的随机数
public int genRandomInt(int max){
return new Random().nextInt(max);
}
private Date date;
//访问Date类的构造函数
public native long accessConstructor();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Example of a call to a native method
TextView tv = findViewById(R.id.sample_text);
// tv.setText(hehedaFromJNI());
//***********************************************
//通过打印log我们看一下修改是否成功
Log.e(TAG + "修改前s是:" ,s);
//调用native方法,让C代码修改成员变量s
accessFiled();
Log.e(TAG + "修改后s是:" ,s);
//***********************************************
Log.e(TAG + "修改前count是:" ,count + "");
//调用native方法,让C代码修改成员变量county
accessStaticFiled();
Log.e(TAG + "修改后count是:" ,count + "");
//触发native层 调用 静态方法
// tv.setText(accessStaticMethod());
//触发native层 调用 非静态方法
// tv.setText(accessMethod() + "");
//触发native层 调用 构造方法
tv.setText(accessConstructor() + "");
}
/**
* A native method that is implemented by the 'native-lib' native library,
* which is packaged with this application.
*/
public native String stringFromJNI();
public native String stringFromJNITwo();
public native String hehedaFromJNI();
}
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.4.1)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
native-lib.cpp heheheda.cpp )
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
native-lib
# Links the target library to the log library
# included in the NDK.
${log-lib} )