Android studio1.5.1 NDK configuration Development

Source: Internet
Author: User

Android studio1.5.1 NDK configuration Development

Note: The process may be different because the tool used is different from the SDK version. I will explain several cases I have tried.

I. Tools and SDK versions: Android studio1.51, SDK Version: 23 (latest 6.0)

Ii. Tools and SDK versions: Android studio1.51; SDK Version: 21

You must do one thing before these two cases: Install NDK. The process is as follows:

In setting, check appearance-> Android SDK-> SDK Tools-> Android NDk and click apply-> OK. For example:

After the DN card is automatically installed, it will be displayed in local. properties:

At this point, the ND installation is complete, and the next step is the critical moment. Let's take a look at the specific process of situation 1:

1. Creating a new project is a simple blank project with no Functions

2. Define the native Interface and load the library to be generated. The Code is as follows:

  • Packagezhanghuan.cn. jnitest1;
  • Importandroid. support. v7.app. AppCompatActivity; importandroid. OS. Bundle;
  • Importandroid. widget. TextView;
  • PublicclassMainActivityextendsAppCompatActivity {static {
  • System. loadLibrary ("JniTest ");}
  • PublicnativeStringgetStringFromNative ();
  • @ Override
  • ProtectedvoidonCreate (BundlesavedInstanceState) {super. onCreate (savedInstanceState );
  • SetContentView (R. layout. activity_main );
  • TextViewtextView = (TextView) findViewById (R. id. text); textView. setText (getStringFromNative (); // call the interface
  • }}
    3. After writing the interface, the next step is the key. Click Build-> Make Project, for example:

    Check the Build status in the status bar Messages at the bottom. It must be 0 errors, for example:

    4. After the Build is successful, generate the. h file in the Terminal command line.

    Command:

  • Javah-djni-classpathD: \ tools \ android-sdk \ platforms \ android-23 \ android. jar; D: \ MyProject \ AndroidStudio \ JniTest1 \ app \ src \ main \ javazhanghuan.cn. jnitest1.MainActivity

    This step has been stuck for a long time. Every command in the previous tutorial is as follows:

  • Javah-djni-classpathD: \ tools \ android-sdk \ platforms \ android. jar;... \ build \ intermediates \ classes \ debugzhanghuan.cn. jnitest1.MainActivity

    The error "android. support. v7.app. AppCompatActivity class file not found" is returned. For example:

     

    I fail to try it. I finally found that, unless the V7 feature is not used and the AppCompatActivity class is not inherited, it will be okay if it is inherited into a common Activity. I will explain the difference later. However, I think it should not work in AppCompatActivity. Although this is a new feature, it must be able to accommodate the previous features. Since the class file cannot be found in debug, I will directly specify the class file in main, not found in debug. The result shows that the class file succeeded! At that time, I took a long breath. The successful command is the first command above, pointing directly to the class file, and then generating. h file, but this. h file and the class file generated in debug. h files are different. The difference will be later. h file comparison will be known.

    Return to the topic. After the command is successful, generate the jni directory under the main directory, and then have a. h file, for example:

     

    I paste the code of the. h file:

  • /* DONOTEDITTHISFILE-itismachinegenerated */# include
  • /* Headerforclasszhanghuan_cn_jnitest1_MainActivity */
  • # Ifndef_Included_zhanghuan_cn_jnitest1_MainActivity # define_Included_zhanghuan_cn_jnitest1_MainActivity
  • # Ifdef _ cplusplusextern "C "{
  • # Endif /*
  • * Class: zhanghuan_cn_jnitest1_MainActivity * Method: getStringFromNative
  • * Signature :() Ljava/lang/String ;*/
  • JNIEXPORTjstringJNICALLJava_zhanghuan_cn_jnitest1_MainActivity_getStringFromNative (JNIEnv *, jobject );
  • # Ifdef _ cplusplus
  • } # Endif
  • # Endif 5. Copy the. h file and place it in the same directory. Change the name (name arbitrary) To my. c. The Code is as follows:
  • # Include "zhanghuan_cn_jnitest1_MainActivity.h"
  • JNIEXPORTjstringJNICALLJava_zhanghuan_cn_jnitest1_MainActivity_getStringFromNative (JNIEnv * env, jobjectjob ){
  • Return (* env)-> NewStringUTF (env, "HelloformJNI! ");}
    However, you will find an error. NDK support is an experimental feature and all use cases are not yet supported, for example:

    The solution is to add a code in the gradle. properties file:

  • Android. useDeprecatedNdk = true

     

    6. Add the NDK configuration to the defaultConfig of the build. gradle file in the app, and specify the generated so file name and the appropriate arm type. Add the Code:
  • Ndk {moduleName "JniTest"
  • AbiFilters "armeabi", "armeabi-v7a", "x86"} My build. gradle file code is as follows:
  • Applyplugin: 'com. android. application'
  • Android {compileSdkVersion23
  • BuildToolsVersion "23.0.2"
  • DefaultConfig {applicationId "zhanghuan.cn. jnitest1"
  • MinSdkVersion11targetSdkVersion23
  • VersionCode1versionName "1.0"
  • Ndk {
  • ModuleName "JniTest" abiFilters "armeabi", "armeabi-v7a", "x86"
  • }}
  • BuildTypes {release {
  • Minifyenabledfalseproguardfilesgetdefadefaproguardfile('proguard-android.txt '), 'proguard-rules. Pro'
  • }}
  • }
  • Dependencies {compilefileTree (dir: 'libs', include: ['*. jar'])
  • TestCompile 'junit: junit: 4.12 'compile 'com. android. support: appcompat-v7: 23.2.1'
  • }
    At this point, the basic steps are all completed, and then you can run them directly to see the effect, as shown below:


    Completed successfully! If you are interested, you can see the second case below.

     

    Now let's take a look at the second situation: V7 is not used.

    1. The steps in the previous steps are the same as those in the first case. When there is an important difference between the command line in step 1, I will jump directly to the command line step. The command is as follows:

  • Javah-djni-classpathD: \ tools \ android-sdk \ platforms \ android-21 \ android. jar; D: \ MyProject \ AndroidStudio \ JinTest \ app \ build \ intermediates \ classes \ debugzhanghuan.cn. jintest. mainActivity

    Note: if an error occurs, you need to write an absolute address. What are the online tutorials ".. \.. \ buidl \... "I have never succeeded in writing this method, but I haven't succeeded it for a long time. If you have succeeded, please let me know.

    The final. h file code is as follows:

  • /* DONOTEDITTHISFILE-itismachinegenerated */# include
  • /* Headerforclasszhanghuan_cn_jintest_MainActivity */
  • # Ifndef_Included_zhanghuan_cn_jintest_MainActivity # define_Included_zhanghuan_cn_jintest_MainActivity
  • # Ifdef _ cplusplusextern "C "{
  • # Endif # undefzhanghuan_cn_jintest_MainActivity_BIND_ABOVE_CLIENT
  • # Definezhanghuan_cn_jintest_mainactivity_bind_abve_client8l # undefzhanghuan_cn_jintest_MainActivity_BIND_ADJUST_WITH_ACTIVITY
  • # Definezhanghuan_cn_jintest_MainActivity_BIND_ADJUST_WITH_ACTIVITY128L # undefzhanghuan_cn_jintest_MainActivity_BIND_ALLOW_OOM_MANAGEMENT
  • # Definezhanghuan_cn_jintest_MainActivity_BIND_ALLOW_OOM_MANAGEMENT16L # undefzhanghuan_cn_jintest_MainActivity_BIND_AUTO_CREATE
  • # Definezhanghuan_cn_jintest_MainActivity_BIND_AUTO_CREATE1L # undefzhanghuan_cn_jintest_MainActivity_BIND_DEBUG_UNBIND
  • # Definezhanghuan_cn_jintest_MainActivity_BIND_DEBUG_UNBIND2L # undefzhanghuan_cn_jintest_MainActivity_BIND_IMPORTANT
  • # Definezhanghuan_cn_jintest_MainActivity_BIND_IMPORTANT64L # undefzhanghuan_cn_jintest_MainActivity_BIND_NOT_FOREGROUND
  • # Definezhanghuan_cn_jintest_MainActivity_BIND_NOT_FOREGROUND4L # undefzhanghuan_cn_jintest_MainActivity_BIND_WAIVE_PRIORITY
  • # Definezhanghuan_cn_jintest_MainActivity_BIND_WAIVE_PRIORITY32L # undefzhanghuan_cn_jintest_MainActivity_CONTEXT_IGNORE_SECURITY
  • # Definezhanghuan_cn_jintest_MainActivity_CONTEXT_IGNORE_SECURITY2L # undefzhanghuan_cn_jintest_MainActivity_CONTEXT_INCLUDE_CODE
  • # Definezhanghuan_cn_jintest_MainActivity_CONTEXT_INCLUDE_CODE1L # undefzhanghuan_cn_jintest_MainActivity_CONTEXT_RESTRICTED
  • # Definezhanghuan_cn_jintest_MainActivity_CONTEXT_RESTRICTED4L # undefzhanghuan_cn_jintest_MainActivity_MODE_APPEND
  • # Definezhanghuan_cn_jintest_MainActivity_MODE_APPEND32768L # undefzhanghuan_cn_jintest_MainActivity_MODE_ENABLE_WRITE_AHEAD_LOGGING
  • # Definezhanghuan_cn_jintest_MainActivity_MODE_ENABLE_WRITE_AHEAD_LOGGING8L # undefzhanghuan_cn_jintest_MainActivity_MODE_MULTI_PROCESS
  • # Definezhanghuan_cn_jintest_MainActivity_MODE_MULTI_PROCESS4L # undefzhanghuan_cn_jintest_MainActivity_MODE_PRIVATE
  • # Definezhanghuan_cn_jintest_MainActivity_MODE_PRIVATE0L # undefzhanghuan_cn_jintest_MainActivity_MODE_WORLD_READABLE
  • # Definezhanghuan_cn_jintest_MainActivity_MODE_WORLD_READABLE1L # undefzhanghuan_cn_jintest_MainActivity_MODE_WORLD_WRITEABLE
  • # Definezhanghuan_cn_jintest_MainActivity_MODE_WORLD_WRITEABLE2L # undefzhanghuan_cn_jintest_MainActivity_DEFAULT_KEYS_DIALER
  • # Definezhanghuan_cn_jintest_MainActivity_DEFAULT_KEYS_DIALER1L # undefzhanghuan_cn_jintest_MainActivity_DEFAULT_KEYS_DISABLE
  • # Definezhanghuan_cn_jintest_MainActivity_DEFAULT_KEYS_DISABLE0L # undefzhanghuan_cn_jintest_MainActivity_DEFAULT_KEYS_SEARCH_GLOBAL
  • # Definezhanghuan_cn_jintest_MainActivity_DEFAULT_KEYS_SEARCH_GLOBAL4L # undefzhanghuan_cn_jintest_MainActivity_DEFAULT_KEYS_SEARCH_LOCAL
  • # Definezhanghuan_cn_jintest_MainActivity_DEFAULT_KEYS_SEARCH_LOCAL3L # undefzhanghuan_cn_jintest_MainActivity_DEFAULT_KEYS_SHORTCUT
  • # Definezhanghuan_cn_jintest_MainActivity_DEFAULT_KEYS_SHORTCUT2L # undefzhanghuan_cn_jintest_MainActivity_RESULT_CANCELED
  • # Definezhanghuan_cn_jintest_MainActivity_RESULT_CANCELED0L # undefzhanghuan_cn_jintest_MainActivity_RESULT_FIRST_USER
  • # Definezhanghuan_cn_jintest_MainActivity_RESULT_FIRST_USER1L # undefzhanghuan_cn_jintest_MainActivity_RESULT_ OK
  • # Definezhanghuan_cn_jintest_MainActivity_RESULT_OK-1L /*
  • * Class: zhanghuan_cn_jintest_MainActivity * Method: getStringFromNative
  • * Signature :() Ljava/lang/String ;*/
  • JNIEXPORTjstringJNICALLJava_zhanghuan_cn_jintest_MainActivity_getStringFromNative (JNIEnv *, jobject );
  • # Ifdef _ cplusplus
  • } # Endif
  • # Endif

     

    Is it different from the. h file generated in the first case? There are a lot more things. However, these things seem to be quite simple. If you know the difference, please leave a message and tell me. Thank you very much.

    Of course, you can also use the command in the first case, which is also OK without adding-classpath and android. jar, only specify the class file to be compiled and. the H file directory is as follows:

  • Javah-djniD: \ MyProject \ AndroidStudio \ JinTest \ app \ build \ intermediates \ classes \ debugzhanghuan.cn. jintest. MainActivity

    -D jni specifies the location where the. h file is generated and stored. Do not create it by yourself.

    The last generated. so file is in app \ build \ intermediates \ ndk \ debug \ lib, for example:

    At this point, all the cases have been described. Of course, if you have any questions during your work, please leave a message and we will discuss and discuss them together.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.