我使用ndk
+ openGL
制作了一个示例项目。该应用程序的想法很简单,点击屏幕上出现的AR对象时,请打开相机。
在debug mode
工作时一切正常,但是在切换release mode
后,编译正常,我可以打开相机,但是当我点击屏幕时,应用程序崩溃了。
当然有任何日志...因此,为了打开日志选项(以找出崩溃的原因),我在debuggable true
部分中添加了这样的行release
。
但是,现在我在屏幕上点击时,应用程序不会崩溃...
崩溃的可能原因是什么?如果我仅因debuggable false
崩溃而看不到日志,该如何检查??
这是我的gradle
apply plugin: 'com.android.application'
/*
The arcore aar library contains the native shared libraries. These are
extracted before building to a temporary directory.
*/
def arcore_libpath = "${buildDir}/arcore-native"
// Create a configuration to mark which aars to extract .so files from
configurations { natives }
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.google.ar.core.examples.c.helloar"
// 24 is the minimum since ARCore only works with 24 and higher.
minSdkVersion 24
targetSdkVersion 27
versionCode 1
versionName "1.0"
signingConfig signingConfigs.debug
externalNativeBuild {
cmake {
cppFlags "-std=c++11", "-Wall"
arguments "-DANDROID_STL=c++_static",
"-DARCORE_LIBPATH=${arcore_libpath}/jni",
"-
DARCORE_INCLUDE=${project.rootDir}
/../../libraries/include"
}
}
ndk {
abiFilters "arm64-v8a", "armeabi-v7a", "x86"
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
buildTypes {
release {
debuggable true
minifyEnabled false
proguardFiles getDefaultProguardFile
('proguard-android.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}
dependencies {
// ARCore library
implementation 'com.google.ar:core:1.9.0'
natives 'com.google.ar:core:1.9.0'
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:design:27.1.1'
}
// Extracts the shared libraries from aars in the natives configuration.
// This is done so that NDK builds can access these libraries.
task extractNativeLibraries() {
// Always extract, this insures the native libs
are updated if the version changes.
outputs.upToDateWhen { false }
doFirst {
configurations.natives.files.each { f ->
copy {
from zipTree(f)
into arcore_libpath
include "jni/**/*"
}
}
}
}
tasks.whenTaskAdded {
task-> if (task.name.contains("external") && !task.name.contains("Clean")) {
task.dependsOn(extractNativeLibraries)
}
}
和应用gradle
// Top-level build file where you can add
configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
mavenLocal()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
编辑
这里我有一些日志输出
*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
Build fingerprint: 'samsung/crownltexx/crownlte:9/PPR1.180610.011/N960FXXU2CSDE:user/release-keys'
Revision: '28'
ABI: 'arm64'
pid: 11411, tid: 11600, name: GLThread 82400 >>> com.myapp.ar <<<
signal 6 (SIGABRT), code -6 (SI_TKILL), fault addr --------
Abort message: 'terminating with uncaught exception of type std::bad_alloc: std::bad_alloc'
x0 0000000000000000 x1 0000000000002d50 x2 0000000000000006 x3 0000000000000008
x4 fefefefefefefeff x5 fefefefefefefeff x6 fefefefefefefeff x7 7f7f7f7f7f7f7f7f
x8 0000000000000083 x9 0000007f5c9fa890 x10 fffffff87ffffbdf x11 0000000000000001
x12 0000000000000018 x13 000000005d0788d4 x14 00057c28f4018340 x15 00001ef146270368
x16 0000007f5ca312b0 x17 0000007f5c970958 x18 0000000000000000 x19 0000000000002c93
x20 0000000000002d50 x21 0000000000000083 x22 ffffff80ffffffc8 x23 0000007e94b00cb0
x24 0000007e94b00b90 x25 0000007e94b00bd0 x26 0000000000000016 x27 ffffffffffffffb0
x28 0000007ebfc24e9c x29 0000007e94b00b00
sp 0000007e94b00ac0 lr 0000007f5c963da0 pc 0000007f5c963dcc
backtrace:
#00 pc 0000000000021dcc /system/lib64/libc.so (abort+124)
#01 pc 00000000000cccac /data/app/com.myapp.ar-jewGNQDDH4aeZ8zLn7v2aA==/lib/arm64/libhello_ar_native.so
#02 pc 00000000000cce0c /data/app/com.myapp.ar-jewGNQDDH4aeZ8zLn7v2aA==/lib/arm64/libhello_ar_native.so
#03 pc 00000000000c9bf0 /data/app/com.myapp.ar-jewGNQDDH4aeZ8zLn7v2aA==/lib/arm64/libhello_ar_native.so
#04 pc 00000000000c92f8 /data/app/com.myapp.ar-jewGNQDDH4aeZ8zLn7v2aA==/lib/arm64/libhello_ar_native.so
#05 pc 00000000000c9278 /data/app/com.myapp.ar-jewGNQDDH4aeZ8zLn7v2aA==/lib/arm64/libhello_ar_native.so (__cxa_throw+120)
#06 pc 00000000000cc588 /data/app/com.myapp.ar-jewGNQDDH4aeZ8zLn7v2aA==/lib/arm64/libhello_ar_native.so (operator new(unsigned long)+84)
#07 pc 000000000006c3c0 /data/app/com.myapp.ar-jewGNQDDH4aeZ8zLn7v2aA==/lib/arm64/libhello_ar_native.so (std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char>>::append(char const*, unsigned long)+264)
#08 pc 000000000006e59c /data/app/com.myapp.ar-jewGNQDDH4aeZ8zLn7v2aA==/lib/arm64/libhello_ar_native.so (FrameManager::LoadImage(std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char>>)+280)
#09 pc 0000000000050ed8 /data/app/com.myapp.ar-jewGNQDDH4aeZ8zLn7v2aA==/lib/arm64/libhello_ar_native.so (hello_ar::VideoRender::thread_task2(std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char>>, std::__ndk1::shared_ptr<FrameManager>)+60)
#10 pc 000000000006c800 /data/app/com.myapp.ar-jewGNQDDH4aeZ8zLn7v2aA==/lib/arm64/libhello_ar_native.so (_ZNSt6__ndk114__thread_proxyINS_5tupleIJNS_10unique_ptrINS_15__thread_structENS_14default_deleteIS3_EEEEMN8hello_ar11VideoRenderEFvNS_12basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEENS_10shared_ptrI12FrameManagerEEEPS8_SE_SH_EEEEEPvSM_+132)
#11 pc 0000000000084df8 /system/lib64/libc.so (__pthread_start(void*)+208)
#12 pc 0000000000023ac4 /system/lib64/libc.so (__start_thread+68)
编辑
LoadImage
方法
void FrameManager::LoadImage(std::string const & path)
{
std::ifstream file(path, std::ios::binary);
const int PKM_HEADER_SIZE = 16;
char pkmHeader[PKM_HEADER_SIZE];
file.read(pkmHeader, sizeof(pkmHeader));
std::string magicNumber = "";
magicNumber.append("" + ((char) pkmHeader[0]));
magicNumber.append("" + ((char) pkmHeader[1]));
magicNumber.append("" + ((char) pkmHeader[2]));
magicNumber.append("" + ((char) pkmHeader[3]));
m_image_width = ((int) pkmHeader[9]) | (((int) pkmHeader[8]) << 8);
m_image_height = ((int) pkmHeader[11]) | (((int) pkmHeader[10]) << 8);
const int BLOCK_DIM = 4;
const int BLOCK_PIXELS = BLOCK_DIM * BLOCK_DIM;
const int BLOCK_BITS = 64;
int size = (BLOCK_BITS / 8) * m_image_width * m_image_height /
(BLOCK_PIXELS);
m_imageData.resize(size);
int pos = 0;
while (size > 0)
{
file.read((char *) &m_imageData[pos], size);
pos += (int) file.gcount();
size -= (int) file.gcount();
}
file.close();
}
答案 0 :(得分:3)
"" + ((char) pkmHeader[0])
是const char*
,因此std::string::append()可以收到它。但是此const char*
的值不是"A"
(假设pkmHeader[0] == 'A'
)。 "" + ((char) pkmHeader[0])
的结果是一个指向(预分配的)""
常量的指针,该指针与该指针的偏移量为65
。对我来说,
printf("%p %p\n", "", "" + ((char) 'A') );
打印:
0x7f3cbf201025 0x7f3cbf201066
请注意,0x7f3cbf201066 - 0x7f3cbf201025 = 0x41
是'A'的值。
要修复代码,请使用
magicNumber.append(pkmHeader, 4);