Skip to content

Commit

Permalink
Add option to use Amber as a shared library on Android (google#708)
Browse files Browse the repository at this point in the history
  • Loading branch information
asuonpaa authored and dj2 committed Nov 13, 2019
1 parent 72614a0 commit 956d9bb
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
4 changes: 4 additions & 0 deletions samples/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ LOCAL_MODULE:=amber_ndk
LOCAL_CPP_EXTENSION := .cc .cpp .cxx
LOCAL_SRC_FILES:= \
amber.cc \
android_main.cc \
config_helper.cc \
config_helper_vulkan.cc \
log.cc \
Expand All @@ -30,5 +31,8 @@ LOCAL_LDLIBS:=-landroid -lvulkan -llog
LOCAL_CXXFLAGS:=-std=c++11 -fno-exceptions -fno-rtti -Werror -Wno-unknown-pragmas -DAMBER_ENGINE_VULKAN=1 -DAMBER_ENABLE_LODEPNG=1
LOCAL_STATIC_LIBRARIES:=amber lodepng
include $(BUILD_EXECUTABLE)
LOCAL_MODULE:=amber_ndk_sharedlib
LOCAL_MODULE_FILENAME:=libamber_ndk
include $(BUILD_SHARED_LIBRARY)

include $(LOCAL_PATH)/../Android.mk
45 changes: 45 additions & 0 deletions samples/android_main.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2019 The Amber Authors.
//
// 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.

#include <jni.h>

#include <sstream>
#include <string>
#include <vector>

extern int main(int argc, const char** argv);

extern "C" JNIEXPORT JNICALL int Java_com_google_amber_Amber_androidMain(
JNIEnv* env,
jobject,
jstring args_jstring) {
// Redirect std output to a file
freopen("/sdcard/amberlog.txt", "w", stdout);
freopen("/sdcard/amberlog.txt", "a", stderr);

std::string args(env->GetStringUTFChars(args_jstring, NULL));

// Parse argument string and add -d by default
std::stringstream ss(args);
std::vector<std::string> argv_string{std::istream_iterator<std::string>{ss},
std::istream_iterator<std::string>{}};
std::vector<const char*> argv;
argv.push_back("amber");
argv.push_back("-d");

for (auto s : argv_string)
argv.push_back(s.c_str());

return main(argv.size(), argv.data());
}

0 comments on commit 956d9bb

Please sign in to comment.