1.配置MainActivity.java
package xiziyunqi.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
// Used to load the 'native-lib' library on application startup.
static {
System.loadLibrary("totoc");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Example of a call to a native method
findViewById(R.id.button).setOnClickListener((v)->{
boolean res = isEquals("123456");
Log.i("jw", "res:" + res);
});
}
/**
* A native method that is implemented by the 'totoc' native library,
* which is packaged with this application.
*/
private native boolean isEquals(String str);
}
2.生成头文件
第一步:
cd 到app\src\main(工作路径,在次路径下生成jni文件夹)
第二步,执行:
javah -d jni -classpath C:\Users\BFS039\AppData\Local\Android\sdk\extras\android\m2repository\com\android\support\appcompat-v7\26.0.0-alpha1\appcompat-v7-26.0.0-alpha1-sources.jar;….\build\intermediates\classes\debug xiziyunqi.myapplication.MainActivity
注意:
appcompat-v7-26.0.0-alpha1-sources.jar后要有分号
….\build\intermediates\classes\debug(进入到生成class类文件的目录)
xiziyunqi.myapplication.MainActivity(对目录下的该类文件执行javah产生头文件到工作路径)
如果提示找不到类文件(在-classpath加上sdk/…/android.jar)
3.根据头文件对函数的声明补全c程序
4.配置
1配置ndk
ndk {
moduleName “totoc”
abiFilters “armeabi”,”armeabi-v7a”,”x86”
}
2C++的编译配置
Put this part in build.gradle(Module:app) above buildTypes{}
sourceSets {
main {
jni.srcDirs = []
}
}
出现的问题
1.不支持Lambda语法(gradle编译)
解决办法:
在app目录的build.gradle中加上:
Android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
和
defaultConfig {
jackOptions {
enabled true
}
2.如果默认勾选了include C++
在cmakelist下做相应更改
#Sets the minimum version of CMake required to build the native
# library. You should either keep the default value or only pass a
# value of 3.4.0 or lower.
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 it 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).
# Associated headers in the same location as their source
# file are automatically included.
src/main/cpp/native-lib.cpp )
# Searches for a specified prebuilt library and stores the path as a
# variable. Because system libraries are included 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 the
# 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} )
3布局UI的路径
参考网址:
1.布局UI:
https://developer.android.com/studio/write/layout-editor.html
2.AS添加按钮:
http://blog.youkuaiyun.com/mghhz816210/article/details/51673558
3.JNI开发流程:
http://www.jianshu.com/p/e18ee69a8ba1
4.拓展:
http://www.wjdiankong.cn/android%E9%80%86%E5%90%91%E4%B9%8B%E6%97%85-%E5%8A%A8%E6%80%81%E6%96%B9%E5%BC%8F%E7%A0%B4%E8%A7%A3apk%E8%BF%9B%E9%98%B6%E7%AF%87ida%E8%B0%83%E8%AF%95so%E6%BA%90%E7%A0%81/