Build static library of Skia to link against on android

1,974 views
Skip to first unread message

Jon C

unread,
Sep 5, 2012, 2:54:57 PM9/5/12
to skia-d...@googlegroups.com
I am trying to build a static library of skia that my existing C++ code could link with to be built with the android ndk, and the latest skia has me thoroughly confused.  The older versions of the makefile simply create libskia.a or libskia.so, albeit without explicit android build configurations.  However, in the latest skia, it seems that things are built and linked into existing apps.

I have tried following these directions: https://sites.google.com/site/skiadocs/user-documentation/quick-start-guides/android, but the results of the build don't give me a static library to work with.  Digging around the generated makefiles, it seems that libcore.a and other .a's in the out/Debug/obj.target/gyp folder are relevant for me, but they are thin archives.  So, I have modified make.py to disable the T argument to ar.  However, simply pulling in the .a's that I need as prebuilt static libraries and linking them against a trivial native Skia application that simply contains SkCanvas canvas; causes a segfault after the native code executes for both 4.0.3 and 4.1.

Is this the right way to for me to build a static library of skia that I could link to with my code, or is there another method? I have looked through just about all of the threads online related to this issue, and none of them contained enough information for me to get this working.  This seemed promising: https://groups.google.com/forum/#!topic/skia-discuss/GvTW_j0Kp38, but the OP did not include instructions on how he got things working.

Any help would be greatly appreciated.

Eric

unread,
Sep 5, 2012, 3:27:42 PM9/5/12
to skia-d...@googlegroups.com
Hi Jon,

There currently is no simple way to build a static library from Skia source, although it's something we're working on (see http://code.google.com/p/skia/issues/detail?id=459).  This should be fixed within a couple of weeks, although it admittedly is not a high priority.

In the meantime, there is a patch uploaded here which should do the trick (you should be able to run "make skia" to build libskia.a inside out/Debug/obj.target/gyp).  You can apply the patch inside the trunk directory by running "curl http://codereview.appspot.com/download/issue6498090_7004.diff | patch -p0".  That patch is not quite ready to go, however, and you'll have to keep your gyp hack in order to build a "fat" archive.

I'd suggest subscribing to that issue to keep updated.

- Eric

Jon C

unread,
Sep 5, 2012, 6:50:55 PM9/5/12
to skia-d...@googlegroups.com
Hi Eric,

Thanks a lot for the info, I managed to apply the patch to a new checkout of skia.  However, even using the libskia.a that is generated here, my android app is still crashing.  Simply trying to run this example: http://efyx.io/android/TestSkia.zip, but with my own built libskia.a, or even with just a native JNI function that just calls SkCanvas canvas.  Any ideas what could be wrong?  My checkout of Skia is completely fresh, although I had to manually apply part of the patch since there were some extra source files in core.gypi, and I simply changed make.py in gyp to make fat archives instead.

The segfault is happening in libc after my native jni function has already completed (in efyx.io's TestSkia, it even draws the red screen before the crash, indicating that the skia code actually works), so I'm at a loss with how to debug this.

Eric

unread,
Sep 5, 2012, 6:59:55 PM9/5/12
to skia-d...@googlegroups.com
Do you still get the segfault if you comment out all of the Skia code?

Jon C

unread,
Sep 5, 2012, 7:10:58 PM9/5/12
to skia-d...@googlegroups.com
No, if I do not have SkCanvas canvas in my native code, the segfault no longer happens.

Jon C

unread,
Sep 5, 2012, 7:14:12 PM9/5/12
to skia-d...@googlegroups.com
The backtrace shows: __sync_fetch_and_add_4 (ptr=0x2a00b9e0, val=1153351680) at /usr/local/google/home/andrewhsieh/ndk-andrewhsieh/src/build/../gcc/gcc-4.6/libgcc/../gcc/config/arm/linux-atomic.c:65, surrounded by ?? ()'s

Eric

unread,
Sep 5, 2012, 10:08:34 PM9/5/12
to skia-d...@googlegroups.com
Interesting... How are you building libskia.a?  Try checking out the 'android' subdirectory (http://skia.googlecode.com/svn/android/) and then building skia from the trunk directory with:

../android/bin/android_make -d arm_no_opts skia

The android_make script sets some environment variables which may have been missing before.  Optionally, if you know the capabilities of your test device, you can substitute "arm_no_opts" with one of the other options.

Jon C

unread,
Sep 5, 2012, 10:51:54 PM9/5/12
to skia-d...@googlegroups.com
I was building with the android directory, like using ../android/bin/android_make, with the environment variables.  I think I know what the problem was now, for my ndk build, I wasn't including the same preprocessor flags as what the skia build had, only -DSK_, so I think the sizes of certain things were different, and the return address was getting overwritten as a result when I tried to initialize SkCanvas.  After declaring the same preprocessor variables in the ndk, skia behaves correctly.

Thanks for your help.

By the way, for the patch, I believe that the android specific expats and ft2 dependencies from ports.gyp need to be in ports.gypi instead, since skia.gyp only includes ports.gypi.

Janez Zemva

unread,
Sep 6, 2012, 2:38:13 AM9/6/12
to skia-d...@googlegroups.com
I've been building a static version of skia for android for a long
time directly from source via my own Makefile. What you need to list
are the relevant source files. Here's an example:

This is the bjam file I'm using. You similarly need to compile and
link the freetype font library. The graphics gets blitted via a custom
SkDevice, otherwise only software rendering into SkBitmap s would be
available.

BTW: A big problem with skia is the lack of a gui library. You need to
write your own or just use it to draw.

lib android_neon_skia
: [ glob skia/src/core/*.cpp ]
[ glob skia/src/opts/*arm*.cpp ]
[ glob skia/src/opts/*neon*.S ]
[ glob skia/src/ports/*stdio*.cpp ]
# skia/src/ports/FontHostConfiguration_android.cpp
# skia/src/ports/SkFontHost_android.cpp
skia/src/ports/SkDebug_android.cpp
skia/src/ports/SkFontDescriptor.cpp
skia/src/ports/SkFontHost_FreeType.cpp
skia/src/ports/SkFontHost_FreeType_common.cpp
skia/src/ports/SkFontHost_linux.cpp
skia/src/ports/SkFontHost_tables.cpp
skia/src/ports/SkMemory_malloc.cpp
skia/src/ports/SkThread_none.cpp
skia/src/utils/SkOSFile.cpp

# [ glob skia/src/gpu/*.cpp ]
# [ glob skia/src/gpu/unix/*.cpp ]
[ glob skia/third_party/glu/libtess/*.c ]

: <define>__ARM_HAVE_NEON
<include>../freetype/freetype/include
<include>skia/include/config
<include>skia/include/core
<include>skia/include/utils
<include>skia/src/core
<include>skia/src/image

<include>skia/include/gpu
<include>skia/gpu/include
<include>skia/third_party/glu
<include>skia/third_party/glu/libtess

<link>static
<optimization>speed
<cflags>-ffast-math

Eric

unread,
Sep 6, 2012, 7:46:54 AM9/6/12
to skia-d...@googlegroups.com
Jon,

Great!  I'm glad you got it to work, and thanks for the tip.  As I said, that patch isn't ready to go yet.

- Eric

Athos Bacchiocchi

unread,
Sep 12, 2012, 10:53:54 AM9/12/12
to skia-d...@googlegroups.com, Jon C
Il 06/09/2012 04:51, Jon C ha scritto:
> I was building with the android directory, like using
> ../android/bin/android_make, with the environment variables. I think
> I know what the problem was now, for my ndk build, I wasn't including
> the same preprocessor flags as what the skia build had, only -DSK_, so
> I think the sizes of certain things were different, and the return
> address was getting overwritten as a result when I tried to initialize
> SkCanvas. After declaring the same preprocessor variables in the ndk,
> skia behaves correctly.

hi Jon, I'm running into the same issue, could you please give me
informations on how you actually understand which flags were used and
which to add to the android application's makefile?

Are they the ones found in any of the generated makefiles in
trunk/out/gyp? In that case, do I need the ones defined in
"DEFS_debug/DEFS_release" only, or the "CFLAGS_debug/CFLAGS_release"
ones as well?

Also, what do I have to modify to get a "fat" .a file?

One more question, slightly off-topic: I also need gpu acceleration, so
I need to build and include in libskia.a the sources related to gpu. I
tried to add gpu.gyp and gpu.gypi in the include section of skia.gyp,
but it didn't work. Do you (or anyone reading this) know what I can try?

Thanks!
athos

Athos Bacchiocchi

unread,
Sep 12, 2012, 2:02:45 PM9/12/12
to skia-d...@googlegroups.com
Il 12/09/2012 16:53, Athos Bacchiocchi ha scritto:
> hi Jon, I'm running into the same issue, could you please give me
> informations on how you actually understand which flags were used and
> which to add to the android application's makefile?

It seems I could make this part work. I set in my android.mk the flags
found in the gyp generated mkfiles (DEFS_debug/DEFS_release) sand I get
no more crashes in my android application. Though I'm using libbench.so
generated launching the android_make script with bench as target, since
I wasn't able to understand how to hack the gyp files to generate "fat"
.a archives.

So, if someone has the answer, my other questions remain valid.

Thank you for the hints!
athos

Jon C

unread,
Sep 12, 2012, 2:08:54 PM9/12/12
to skia-d...@googlegroups.com
Hi Athos,

Yeah, I used the ones found in the generated makefiles, you can probably find them in the trunk/gyp files as well, from which these are generated, but the makefiles have all the flags compiled into one place.  I only used the DEFS_* flags, I don't think you need the CFLAGS_* for the android.mk.

As for the "fat" archives, you can find the file in: trunk/third_party/externals/gyp/pylib/gyp/generator/make.py, and search for crsT being set to arflags_target and arflags_host and comment those out, and then regenerate your Makefiles.

For your last question, if you just need to include these libs as part of your native code, you can do one of two things: 1. Add another (prebuilt_static_library) in your android.mk for libgpu.a, or, 2. Look through the patch posted and convert gpu.gyp and gpu.gypi the same way as the patch (moving all the source files into the gypi, and just including the gypi in skia.gyp).

jiajun zhang

unread,
Sep 28, 2012, 11:59:06 AM9/28/12
to skia-d...@googlegroups.com
Hi Jon C
Did you resolved this problem?
Can you help me .I want use own skia library because system skia library will crash when i used SkCanvas.
I seem some threads ,but cann't resovled build libskia.a . Can you tell me how can i do?use skia -r1236 or newest version
在 2012年9月6日星期四UTC+8上午10时51分54秒,Jon C写道:

Robert Phillips

unread,
Sep 28, 2012, 12:44:42 PM9/28/12
to skia-d...@googlegroups.com
You should make sure that you are compiling your app with the same compiler flags Skia is using. In particular, the SK_ENABLE_INST_COUNT (which changes the size of the SkCanvas class) may be enabled in debug builds.

   Rob

--
You received this message because you are subscribed to the Google Groups "skia-discuss" group.
To view this discussion on the web visit https://groups.google.com/d/msg/skia-discuss/-/hT-xKoKxeHcJ.
To post to this group, send email to skia-d...@googlegroups.com.
To unsubscribe from this group, send email to skia-discuss...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/skia-discuss?hl=en.

Reply all
Reply to author
Forward
0 new messages