1:编写java
package com.example.zhaohu.test;
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
...................
final TextView infoText = (TextView)findViewById(R.id.info);
infoText.setText(stringFromJNI());
}
public native String stringFromJNI();
public native String unimplementedStringFromJNI();
static {
System.loadLibrary("avcodec-57");
System.loadLibrary("avfilter-6");
System.loadLibrary("avformat-57");
System.loadLibrary("avutil-55");
System.loadLibrary("swresample-2");
System.loadLibrary("swscale-4");
System.loadLibrary("hello");
}
}
在Android视图下的manifests中的AndroidManifest.xml中添加访问SDK卡和网络的权限
在<manifest......>中添加
- <uses-permission android:name="android.permission.INTERNET"/>
- <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
2:在AS下面的Terminal中,生成C/CPP文件
- zhaohudeMacBook-Air:main zhaohu$ cd /Users/zhaohu/AndroidStudioProjects/Test/app/src/main
- zhaohudeMacBook-Air:main zhaohu$ javah -d ../jni com.example.zhaohu.test.MainActivity
- 生成C头文件(com_example_zhaohu_test_MainActivity.h)
3:在AS左侧Project视图中添加实现文件
4:在Project视图中app下编辑(build.gradle)
sourceSets.main{
jniLibs.srcDir 'libs'
jni.srcDirs = []
}
5:在工程的gradle.properties中添加
- android.useDeprecatedNdk=true
6:在local.properties中添加SDK,NDK路径
sdk.dir=/Users/zhaohu/android-sdk-macosx
ndk.dir=/Users/zhaohu/android-ndk-r10e
7:编译库文件
- zhaohudeMacBook-Air:main zhaohu$ pwd
- /Users/zhaohu/AndroidStudioProjects/Test/app/src/main
- zhaohudeMacBook-Air:main zhaohu$ ndk-build
- [armeabi] Install : libavcodec-57.so => libs/armeabi/libavcodec-57.so
- [armeabi] Install : .......................
- zhaohudeMacBook-Air:main zhaohu$ cp libs/armeabi/* /Users/zhaohu/AndroidStudioProjects/Test/app/libs/armeabi-v7a/