Skip to content

Commit

Permalink
This fixes Mobbeel#18 - Support for local external dependencies
Browse files Browse the repository at this point in the history
Added a new  example that builds a native library (example-local-native-lib-in-libs).  
The built native library was added manually to example-lib/libs/
  • Loading branch information
jonbryantnz committed Jan 10, 2019
1 parent fbffcf0 commit 8f56739
Show file tree
Hide file tree
Showing 10 changed files with 250 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ class CopyDependenciesTask extends DefaultTask {
dependencyPath = project.gradle.getGradleUserHomeDir().path + "/caches/modules-2/files-2.1/"
dependencyPath += dependency.group + "/" + dependency.name + "/" + dependency.version + "/"

def file = new File(dependencyPath)
if(false == file.exists()) {
dependencyPath = project.projectDir.path + "/libs/"
}

processDependency(dependency, archiveName, dependencyPath)
} else {
println "Not recognize type of dependency for " + dependency
Expand Down
7 changes: 7 additions & 0 deletions example-lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,15 @@ android {
}
}

repositories {
flatDir {
dirs "$projectDir/libs"
}
}

dependencies {
api fileTree(dir: 'libs', include: '*.jar') // Example with local libs folder
api (name: 'example-local-native-lib-in-libs', ext: 'aar') // Example with native aar in local libs folder

api project(path: ':example-nested-lib') // Example with internal android dependency
api project(path: ':example-nested-native-lib') // Example with internal native dependency
Expand Down
Binary file not shown.
25 changes: 25 additions & 0 deletions example-local-native-lib-in-libs/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 26

defaultConfig {
minSdkVersion 16
targetSdkVersion 26
versionCode 1
versionName "1.0"
}

externalNativeBuild {
ndkBuild {
path './jni/Android.mk'
}
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
12 changes: 12 additions & 0 deletions example-local-native-lib-in-libs/jni/Android.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := example-local-native-lib-in-libs
LOCAL_SRC_FILES :=

LOCAL_C_INCLUDES += $(LOCAL_PATH)

LOCAL_LDLIBS += -llog -ldl

include $(BUILD_SHARED_LIBRARY)
4 changes: 4 additions & 0 deletions example-local-native-lib-in-libs/jni/Application.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
APP_STL := c++_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := armeabi armeabi-v7a arm64-v8a
APP_PLATFORM := android-9
21 changes: 21 additions & 0 deletions example-local-native-lib-in-libs/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
172 changes: 172 additions & 0 deletions example-local-native-lib-in-libs/src/log.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
#ifndef __LOG_H__
#define __LOG_H__

#include <sstream>
#include <string>
#include <stdio.h>

inline std::string NowTime();

enum TLogLevel {logERROR, logWARNING, logINFO, logDEBUG, logDEBUG1, logDEBUG2, logDEBUG3, logDEBUG4};

template <typename T>
class Log
{
public:
Log();
virtual ~Log();
std::ostringstream& Get(TLogLevel level = logINFO);
public:
static TLogLevel& ReportingLevel();
static std::string ToString(TLogLevel level);
static TLogLevel FromString(const std::string& level);
protected:
std::ostringstream os;
private:
Log(const Log&);
Log& operator =(const Log&);
};

template <typename T>
Log<T>::Log()
{
}

template <typename T>
std::ostringstream& Log<T>::Get(TLogLevel level)
{
os << "- " << NowTime();
os << " " << ToString(level) << ": ";
os << std::string(level > logDEBUG ? level - logDEBUG : 0, '\t');
return os;
}

template <typename T>
Log<T>::~Log()
{
os << std::endl;
T::Output(os.str());
}

template <typename T>
TLogLevel& Log<T>::ReportingLevel()
{
static TLogLevel reportingLevel = logDEBUG4;
return reportingLevel;
}

template <typename T>
std::string Log<T>::ToString(TLogLevel level)
{
static const char* const buffer[] = {"ERROR", "WARNING", "INFO", "DEBUG", "DEBUG1", "DEBUG2", "DEBUG3", "DEBUG4"};
return buffer[level];
}

template <typename T>
TLogLevel Log<T>::FromString(const std::string& level)
{
if (level == "DEBUG4")
return logDEBUG4;
if (level == "DEBUG3")
return logDEBUG3;
if (level == "DEBUG2")
return logDEBUG2;
if (level == "DEBUG1")
return logDEBUG1;
if (level == "DEBUG")
return logDEBUG;
if (level == "INFO")
return logINFO;
if (level == "WARNING")
return logWARNING;
if (level == "ERROR")
return logERROR;
Log<T>().Get(logWARNING) << "Unknown logging level '" << level << "'. Using INFO level as default.";
return logINFO;
}

class Output2FILE
{
public:
static FILE*& Stream();
static void Output(const std::string& msg);
};

inline FILE*& Output2FILE::Stream()
{
static FILE* pStream = stderr;
return pStream;
}

inline void Output2FILE::Output(const std::string& msg)
{
FILE* pStream = Stream();
if (!pStream)
return;
fprintf(pStream, "%s", msg.c_str());
fflush(pStream);
}

#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
# if defined (BUILDING_FILELOG_DLL)
# define FILELOG_DECLSPEC __declspec (dllexport)
# elif defined (USING_FILELOG_DLL)
# define FILELOG_DECLSPEC __declspec (dllimport)
# else
# define FILELOG_DECLSPEC
# endif // BUILDING_DBSIMPLE_DLL
#else
# define FILELOG_DECLSPEC
#endif // _WIN32

class FILELOG_DECLSPEC FILELog : public Log<Output2FILE> {};
//typedef Log<Output2FILE> FILELog;

#ifndef FILELOG_MAX_LEVEL
#define FILELOG_MAX_LEVEL logDEBUG4
#endif

#define FILE_LOG(level) \
if (level > FILELOG_MAX_LEVEL) ;\
else if (level > FILELog::ReportingLevel() || !Output2FILE::Stream()) ; \
else FILELog().Get(level)

#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)

#include <windows.h>

inline std::string NowTime()
{
const int MAX_LEN = 200;
char buffer[MAX_LEN];
if (GetTimeFormatA(LOCALE_USER_DEFAULT, 0, 0,
"HH':'mm':'ss", buffer, MAX_LEN) == 0)
return "Error in NowTime()";

char result[100] = {0};
static DWORD first = GetTickCount();
std::sprintf(result, "%s.%03ld", buffer, (long)(GetTickCount() - first) % 1000);
return result;
}

#else

#include <sys/time.h>

inline std::string NowTime()
{
char buffer[11];
time_t t;
time(&t);
tm r = {0};
strftime(buffer, sizeof(buffer), "%X", localtime_r(&t, &r));
struct timeval tv;
gettimeofday(&tv, 0);
char result[100] = {0};
std::sprintf(result, "%s.%03ld", buffer, (long)tv.tv_usec / 1000);
return result;
}

#endif //WIN32

#endif //__LOG_H__
2 changes: 2 additions & 0 deletions example-local-native-lib-in-libs/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mobbeel.github.native_local_lib_in_libs" />
3 changes: 2 additions & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
include ':example-lib'
include ':example-nested-lib'
include ':example-nested-java-lib'
include ':example-nested-native-lib'
include ':example-nested-native-lib'
include ':example-local-native-lib-in-libs'

0 comments on commit 8f56739

Please sign in to comment.