Android 7.0 user version debugging method

ifeq (true,$(strip $(enable_target_debugging)))
  # Target is more debuggable and adbd is on by default
  ADDITIONAL_DEFAULT_PROPERTIES += ro.debuggable=1
  # Enable Dalvik lock contention logging.
  ADDITIONAL_BUILD_PROPERTIES += dalvik.vm.lockprof.threshold=500
  # Include the debugging/testing OTA keys in this build.
  INCLUDE_TEST_OTA_KEYS := true
else # !enable_target_debugging
  # Target is less debuggable and adbd is off by default
  ADDITIONAL_DEFAULT_PROPERTIES += ro.debuggable=1//0-->1

endif # !enable_target_debugging


Add Log printing method in jni:

Reference: https://blog.csdn.net/yf210yf/article/details/9305623

1. Import the log header file

in the .c/.cpp file you use

Import the log.h header file

 #include<android/log.h>



2. In Android.mk

plus

LOCAL_LDLIBS: = - interest

Note that there is a line include $(CLEAR_VARS) in Android.mk

Must put LOCAL_LDLIBS :=-llog after it to be useful,

Otherwise, it is equivalent to not writing.



3. Define the LOG function

First define a global variable, and then define some output LOG functions:

  1. #define TAG "myDemo-jni" // This is the logo of the custom LOG   
  2. #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,TAG ,__VA_ARGS__) // define LOGD type   
  3. #define LOGI(...) __android_log_print(ANDROID_LOG_INFO,TAG ,__VA_ARGS__) // define LOGI type   
  4. #define LOGW(...) __android_log_print(ANDROID_LOG_WARN,TAG ,__VA_ARGS__) // define LOGW type   
  5. #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,TAG ,__VA_ARGS__) // define LOGE type   
  6. #define LOGF(...) __android_log_print(ANDROID_LOG_FATAL,TAG ,__VA_ARGS__) // define LOGF type   

The function defined in the above code

Corresponding to Android's Java code respectively

 Log.d(), Log.i(), Log.w(),Log.e(), Log.f()等方法.



4. Examples

  1. #include <jni.h>  
  2. #include <string.h>  
  3. #include <android/log.h>  
  4.   
  5. #define TAG    "myhello-jni-test" // 这个是自定义的LOG的标识  
  6. #define LOGD(...)  __android_log_print(ANDROID_LOG_DEBUG,TAG,__VA_ARGS__) // 定义LOGD类型  
  7.   
  8.  extern "C" {  
  9.      JNIEXPORT jstring JNICALL Java_com_snail_helloworld_MainActivity_myhello(JNIEnv * env, jobject obj);  
  10.  };  
  11.   
  12.  JNIEXPORT jstring JNICALL Java_com_snail_helloworld_MainActivity_myhello(JNIEnv * env, jobject obj)  
  13.  {  
  14.      int i = 0;  
  15.      LOGD("########## i = %d", i);  
  16.      return env->NewStringUTF("Hello From CPP");  
  17.  }  


Android.mk

  1. LOCAL_PATH := $(call my-dir)  
  2.   
  3. include $(CLEAR_VARS)  
  4.   
  5. LOCAL_MODULE    := hello  
  6. LOCAL_SRC_FILES := hello.cpp  
  7. LOCAL_LDLIBS :=-llog  
  8.   
  9. include $(BUILD_SHARED_LIBRARY)  



实际使用情况如下:

#define LOG_TAG "SysPropJNI"


#include "cutils/properties.h"
#include "utils/misc.h"
#include <utils/Log.h>
#include "jni.h"
#include "core_jni_helpers.h"
#include <nativehelper/JNIHelp.h>
#include <android/log.h>


#define TAG    "SysPropJNI" // 这个是自定义的LOG的标识  
#define LOGD(...)  __android_log_print(ANDROID_LOG_DEBUG,TAG,__VA_ARGS__) // 定义LOGD类型 


namespace android
{


static jstring SystemProperties_getSS(JNIEnv *env, jobject clazz,
                                      jstring keyJ, jstring defJ)
{
    int len;
    const char* key;
    char buf[PROPERTY_VALUE_MAX];
    jstring rvJ = NULL;




    if (keyJ == NULL) {
        jniThrowNullPointerException(env, "key must not be null.");
        goto error;
    }
    key = env->GetStringUTFChars(keyJ, NULL);
LOGD("lucky SystemProperties_getSS");
    LOGD("lucky SystemProperties_getSS 22key = [%s]", key);
    len = property_get(key, buf, "");
LOGD("lucky SystemProperties_getSS len = [%d],[%s]", len,buf);
    if ((len <= 0) && (defJ != NULL)) {
        rvJ = defJ;
    } else if (len >= 0) {
        rvJ = env->NewStringUTF(buf);
    } else {
        rvJ = env->NewStringUTF("");
    }


    env->ReleaseStringUTFChars(keyJ, key);


error:
    return rvJ;

}

....


result:

logcat |grep lucky

04-01 20:01:28.240 D/SysPropJNI( 5610): lucky SystemProperties_getSS04-01 20:01:28.240 D/SysPropJNI( 5610): lucky SystemProperties_getSS 22key = [ro.serialno]04-01 20:01:28.240 D/SysPropJNI( 5610): lucky SystemProperties_getSS len = [19],[ABC0M0A1S19000008]



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325983410&siteId=291194637