Project Practice ①-Kochi zhihu daily (0) -- & gt; NDK hides private data and Kochi ndk

Source: Internet
Author: User

Project Practice ①-Kochi zhihu daily (0) --> NDK hides private data and Kochi ndk

① Android programmers are really tired. GAP, NDK. To tell the truth, I am basically familiar with it. If you want to deepen the foundation of Android, well, NDK is a threshold, and the later architecture layer, this Nima won't take a year for you to decide. What about GAP? If you really want to learn it, okay, js css html5, and the web framework. This Nima is another year, the premise that you can learn these things is that, when the android foundation is relatively good, gap has some benefits for rapid development. If you forget it, go back to the article.

② I started to learn how to add NDK to my project based on my own level. At present, I can only use NDK to obtain the privacy data of some apps, such as the url Connection of the APP, which is also a small application.
I configure the NDK environment. You know what I am most worried about the configuration of the NDK environment, that is, cywin PS necessary for the ndk: cywin is a software that can simulate the linux environment. Now you can go to the android official website to download NDK. Http://developer.android.com/
If you cannot get in, please refer to my post so that you can browse Google's website and carefully study androidAPI ====== to have the ability to read English Google Documents independently.
OK. I believe you will go to the NDK official website.

Decompress the downloaded android-ndk32-r10-windows-x86_64.zip file and put it in the desired directory.

Note: For versions earlier than R7, Cygwin must be installed to use NDK. From R7, Windows NDK provides a ndk-build.cmd script that can be compiled directly using it. To achieve this, you only need to add a Builder for the Eclipse Android project to implement Eclipse's automatic NDK compilation. This will be introduced in the subsequent steps.

2. Create a project and create a separate package C to create a class API. java pay attention to this sentence: System. loadLibrary ("api"); it can be used to load library files, whether JNI library files or non-JNI library file APIs. java
Package com. qf. teach. project. zhihudaily. c; public class API {static {System. loadLibrary ("api");}/*** subject * @ return */public static native String getThemesUrl (); /*** welcome image ** @ return */public static native String getStartImageUrl ();/*** today's hot news * @ return */public static native String getLatestUrl (); /*** historical news <br/> * The returned String must be String. format (); format <br/> * example: String. format (API. getBefore (), "20141201"); * @ return */public static native String getBefore ();/*** topic list <br/> * The returned String must be String. format (); format <br/> * example: String. format (API. getTheme (), "11"); * @ return */public static native String getTheme ();/*** details * The returned String must use String. format (); format <br/> * example: String. format (API. getStory (), "4354228"); * @ return */public static native String getStory ();/*** details-Comment * The returned String must use String. format (); format <br/> * example: String. format (API. getStoryExtra (), "4354228"); * @ return */public static native String getStoryExtra ();}
// String. format replaces each format item in the specified String with the text equivalent item of the value of the corresponding object.

3. Add NDK to your project. generate the java header file, and then focus on creating a jni directory in the SRC directory, and then find the api. open the CMD command in the java directory and enter the full name of the javah api class. In this way, you will get a header file and copy it to the jni directory 2. some people say that the generated header file will go wrong when writing C code. I can only say that, please leave a message and I will send you the detailed tutorial. follow the steps below, the ndk environment is ready in 3 minutes. I'll show you my header file first.
/* DO NOT EDIT THIS FILE - it is machine generated */#include <jni.h>/* Header for class com_xiaoxin_ZhiHuDateNews_c_API */#ifndef _Included_com_xiaoxin_ZhiHuDateNews_c_API#define _Included_com_xiaoxin_ZhiHuDateNews_c_API#ifdef __cplusplusextern "C" {#endif/* * Class:     com_xiaoxin_ZhiHuDateNews_c_API * Method:    getThemesUrl * Signature: ()Ljava/lang/String; */JNIEXPORT jstring JNICALL Java_com_xiaoxin_ZhiHuDateNews_c_API_getThemesUrl  (JNIEnv *, jclass);/* * Class:     com_xiaoxin_ZhiHuDateNews_c_API * Method:    getStartImageUrl * Signature: ()Ljava/lang/String; */JNIEXPORT jstring JNICALL Java_com_xiaoxin_ZhiHuDateNews_c_API_getStartImageUrl  (JNIEnv *, jclass);/* * Class:     com_xiaoxin_ZhiHuDateNews_c_API * Method:    getLatestUrl * Signature: ()Ljava/lang/String; */JNIEXPORT jstring JNICALL Java_com_xiaoxin_ZhiHuDateNews_c_API_getLatestUrl  (JNIEnv *, jclass);/* * Class:     com_xiaoxin_ZhiHuDateNews_c_API * Method:    getBefore * Signature: ()Ljava/lang/String; */JNIEXPORT jstring JNICALL Java_com_xiaoxin_ZhiHuDateNews_c_API_getBefore  (JNIEnv *, jclass);#ifdef __cplusplus}#endif#endif


 
Copy the java header file code
JNIEXPORT jstring JNICALL Java_com_xiaoxin_ZhiHuDateNews_c_API_getThemesUrl  (JNIEnv *, jclass);

Remember, add env thiz.
JNIEXPORT jstring JNICALL Java_com_xiaoxin_ZhiHuDateNews_c_API_getThemesUrl  (JNIEnv *env, jclass thiz){

Okay, here's the C code.
# include "com_xiaoxin_ZhiHuDateNews_c_API.h"JNIEXPORT jstring JNICALL Java_com_xiaoxin_ZhiHuDateNews_c_API_getThemesUrl  (JNIEnv *env, jclass thiz){return (*env)->NewStringUTF(env, "http://news-at.zhihu.com/api/3/themes");}JNIEXPORT jstring JNICALL Java_com_xiaoxin_ZhiHuDateNews_c_API_getStartImageUrl  (JNIEnv *env, jclass thiz) {return (*env)->NewStringUTF(env,"http://news-at.zhihu.com/api/3/start-image/480*728");}JNIEXPORT jstring JNICALL Java_com_xiaoxin_ZhiHuDateNews_c_API_getLatestUrl  (JNIEnv *env, jclass thiz) {return (*env)->NewStringUTF(env,"http://news-at.zhihu.com/api/3/stories/latest");}JNIEXPORT jstring JNICALL Java_com_xiaoxin_ZhiHuDateNews_c_API_getBefore  (JNIEnv *env, jclass thiz){return (*env)->NewStringUTF(env,"http://news-at.zhihu.com/api/3/stories/before/%s");}

There is a URL in the class, that is, the URL to be obtained, and then copy an Android. mk to the code below:
# Copyright (C) 2009 The Android Open Source Project## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at##      http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.#LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE    := apiLOCAL_SRC_FILES := com_xiaoxin_ZhiHuDateNews_c_API.cinclude $(BUILD_SHARED_LIBRARY)

LOCAL_MODULE is the name of the class called by java code, not the full class name ~~~~~~~~ ·
LOCAL_SRC_FILES is the C code under jni. The name is not the full class name ~~~~~~~~~~~~~~~


3. The hybrid compilation of java and c has not been related to NDK any more. It has been related here.

Create and configure a Builder

Click Project-> Properties-> Builders-> New to create a New Builder. In the displayed dialog box, click Program, OK!




In the displayed dialog box [Edit Configuration], the Configuration tab [Main ]:

Location needs to fill in the path of the nkd-build.cmd (under the NDK installation directory ).

In Working Diretcoty, enter the project root directory of TestNDK. :

Note: Do not miss it




Configuration tab [Refresh ],:

Check "Refresh resources upon completion ",

Select The "The entire workspace ",

Select Recuresively include sub-folders ".


Configure the "Build Options" tab, as shown in figure 7:

Select "After a" Clean "",

Check "During manual builds ",

Check "During auto builds ",

Select Specify working set of relevant resources ".

Click "Specify Resources ..." Check the "jni" directory of the TestNDK project and Finish!



Then, when you press save eclipse, it will automatically help us compile it. If you see it, it means you have succeeded.

OK. The next section officially starts the tutorial on zhihu daily ~~~~~~~~~~~~~~~~~~ ''' If an error occurs in the NDK configuration environment, please send me a message and send you a detailed tutorial.


Related Article

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.