Skip to content

Commit

Permalink
update build.gradle
Browse files Browse the repository at this point in the history
  • Loading branch information
fengzhizi715 committed Aug 7, 2020
2 parents e8951c3 + f8b5cbc commit c02f0e1
Show file tree
Hide file tree
Showing 21 changed files with 176 additions and 11 deletions.
1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/build
.cxx
6 changes: 5 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ android {
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
buildTypes {
release {
minifyEnabled false
Expand All @@ -44,7 +49,6 @@ android {
packagingOptions {
exclude 'META-INF/proguard/coroutines.pro'
}

}

dependencies {
Expand Down
Binary file added app/libs/arm64-v8a/liblogger.so
Binary file not shown.
Binary file added app/libs/arm64-v8a/libtest.so
Binary file not shown.
Binary file added app/libs/armeabi-v7a/liblogger.so
Binary file not shown.
Binary file added app/libs/armeabi-v7a/libtest.so
Binary file not shown.
Binary file added app/libs/x86/liblogger.so
Binary file not shown.
Binary file added app/libs/x86/libtest.so
Binary file not shown.
Binary file added app/libs/x86_64/liblogger.so
Binary file not shown.
Binary file added app/libs/x86_64/libtest.so
Binary file not shown.
5 changes: 5 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="portrait"/>

<activity
android:name="cn.salesuite.saf.kotlin.activity.JNIActivity"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="portrait"/>

</application>

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cn.salesuite.saf.kotlin.activity;

import android.app.Activity;
import android.os.Bundle;

import com.safframework.utils.ToastUtilsKt;

import cn.salesuite.saf.kotlin.utils.JNIUtils;

/**
* @FileName: cn.salesuite.saf.kotlin.activity.JNIActivity
* @author: Tony Shen
* @date: 2020-07-22 21:36
* @version: V1.0 <描述当前版本功能>
*/
public class JNIActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// ToastUtilsKt.showLong(this, JNIUtils.stringFromJNI());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,11 @@ class MainActivity : BaseActivity() {
val intent = Intent(this@MainActivity, UseJavaActivity::class.java)
startActivity(intent)
}

// text8.setOnClickListener {
//
// val intent = Intent(this@MainActivity, JNIActivity::class.java)
// startActivity(intent)
// }
}
}
16 changes: 16 additions & 0 deletions app/src/main/java/cn/salesuite/saf/kotlin/utils/JNIUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package cn.salesuite.saf.kotlin.utils;

/**
* @FileName: cn.salesuite.saf.kotlin.utils.JNIUtils
* @author: Tony Shen
* @date: 2020-07-22 20:53
* @version: V1.0 <描述当前版本功能>
*/
public class JNIUtils {

static {
System.loadLibrary("test");
}

// public static native String stringFromJNI();
}
9 changes: 9 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,13 @@
android:gravity="center"
android:text="在 Java 中使用 L"/>

<!-- <TextView-->
<!-- android:id="@+id/text8"-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="50dp"-->
<!-- android:layout_margin="10dp"-->
<!-- android:background="@drawable/textview_border"-->
<!-- android:gravity="center"-->
<!-- android:text="在 JNI 中使用 L"/>-->

</LinearLayout>
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ ext {
buildscript {
System.properties['com.android.build.gradle.overrideVersionCheck'] = 'true'
ext.kotlin_version = '1.3.72'
ext.l_version = '2.5.0'
ext.l_version = '2.5.1'

repositories {
google()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ class FileWriter(var folderPath:String) {

fun open(newFileName: String): Boolean {

lastFileName = newFileName
// lastFileName = newFileName
file = File(folderPath, newFileName)
lastFileName = file?.absolutePath

file?.let {

Expand Down
10 changes: 5 additions & 5 deletions ndklog/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ android {
'-DANDROID_TOOLCHAIN=clang',
'-DANDROID_STL=c++_static'
cppFlags "-std=c++11 -frtti -fexceptions"
abiFilters 'arm64-v8a' ,'armeabi-v7a'
abiFilters 'arm64-v8a', 'armeabi-v7a', 'x86', 'x86_64'
}
}
ndk{
moduleName "detect"
abiFilters 'arm64-v8a' ,'armeabi-v7a'
moduleName "logger"
abiFilters 'arm64-v8a', 'armeabi-v7a', 'x86', 'x86_64'
}
}

Expand All @@ -33,14 +33,14 @@ android {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
ndk{
abiFilters 'arm64-v8a' ,'armeabi-v7a'
abiFilters 'arm64-v8a', 'armeabi-v7a', 'x86', 'x86_64'
}
}
debug {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
ndk{
abiFilters 'arm64-v8a' ,'armeabi-v7a'
abiFilters 'arm64-v8a', 'armeabi-v7a', 'x86', 'x86_64'
}
}
}
Expand Down
79 changes: 76 additions & 3 deletions ndklog/src/main/cpp/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,79 @@

#include "utility.h"

static JavaVM * __logback_jvm = NULL;
static jclass __logback_jclass_logger;
static jmethodID __logback_jmethod_logWrite;
typedef enum {
ERROR,
WARN,
INFO,
DEBUG
}LogLevel;

static const char *FilePrinterClassName = "com/safframework/log/printer/FilePrinter";

JavaVM *javaVM = NULL;
jobject filePrinter_ref = NULL;

void onLoad(JavaVM *vm,jobject jFilePrinter) {
javaVM = vm;
filePrinter_ref = jFilePrinter;
}

JNIEnv *getJNIEnv() {
JNIEnv *env = NULL;
if (javaVM == NULL || javaVM->GetEnv((void **) &env, JNI_VERSION_1_6) != JNI_OK) {
return NULL;
}
return env;
}

void onUnload() {
JNIEnv *env = getJNIEnv();
env->DeleteGlobalRef(filePrinter_ref); // 删除全局引用
}

void Log(LogLevel logLevel, const char *tag, const char *msg) {
JNIEnv *env = getJNIEnv();
if (env == NULL) {
return;
}
jclass clazz = env->FindClass(FilePrinterClassName);
if (clazz == NULL) {
utility::logError("can't find jclass");
return;
}

jclass clazzLogLevel = env->FindClass("com/safframework/log/LogLevel");
jobject objLogLevel;
jfieldID j_fieldID_ERROR = env->GetStaticFieldID(clazzLogLevel,"ERROR","Lcom/safframework/log/LogLevel;");
jfieldID j_fieldID_WARN = env->GetStaticFieldID(clazzLogLevel,"WARN","Lcom/safframework/log/LogLevel;");
jfieldID j_fieldID_INFO = env->GetStaticFieldID(clazzLogLevel,"INFO","Lcom/safframework/log/LogLevel;");
jfieldID j_fieldID_DEBUG = env->GetStaticFieldID(clazzLogLevel,"DEBUG","Lcom/safframework/log/LogLevel;");

switch (logLevel){
case ERROR:
objLogLevel = env->GetStaticObjectField(clazzLogLevel,j_fieldID_ERROR);
break;
case WARN:
objLogLevel = env->GetStaticObjectField(clazzLogLevel,j_fieldID_WARN);
break;
case INFO:
objLogLevel = env->GetStaticObjectField(clazzLogLevel,j_fieldID_INFO);
break;
case DEBUG:
objLogLevel = env->GetStaticObjectField(clazzLogLevel,j_fieldID_DEBUG);
break;
default:
objLogLevel = nullptr;
}
env->DeleteLocalRef(clazzLogLevel);

if (env->IsInstanceOf(filePrinter_ref, clazz)) { // 判断 filePrinter_ref 是否为 FilePrinter 类
jmethodID methodId = env->GetMethodID(clazz, "printLog","(Lcom/safframework/log/LogLevel;Ljava/lang/String;Ljava/lang/String;)V");
env->CallVoidMethod(filePrinter_ref, methodId, objLogLevel,env->NewStringUTF(tag), env->NewStringUTF(msg));
}
}

void LogD(const char *tag, const char *msg) {
utility::logDebug(tag,msg);
Log(DEBUG, tag, msg);
}
10 changes: 10 additions & 0 deletions ndklog/src/main/cpp/logger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//
// Created by tony on 2020/7/23.
//

#ifndef SAF_KOTLIN_LOG_LOGGER_H
#define SAF_KOTLIN_LOG_LOGGER_H

void LogD(const char *tag, const char *msg);

#endif //SAF_KOTLIN_LOG_LOGGER_H
16 changes: 16 additions & 0 deletions ndklog/src/main/cpp/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,33 @@ namespace utility
LOGE("%s", message);
}

void logError(const char *tag, const char *message) {
__android_log_print(ANDROID_LOG_ERROR,tag,"%s", message);
}

void logWarn(const char *message) {
LOGW("%s", message);
}

void logWarn(const char *tag, const char *message) {
__android_log_print(ANDROID_LOG_WARN,tag,"%s", message);
}

void logDebug(const char *message) {
LOGD("%s", message);
}

void logDebug(const char *tag, const char *message) {
__android_log_print(ANDROID_LOG_DEBUG,tag,"%s", message);
}

void logInfo(const char *message) {
LOGI("%s", message);
}

void logInfo(const char *tag, const char *message) {
__android_log_print(ANDROID_LOG_INFO,tag,"%s", message);
}
}

#endif //SAF_KOTLIN_LOG_UTILITY_H

0 comments on commit c02f0e1

Please sign in to comment.