Skip to content

Commit

Permalink
Simplify Flac extension build process
Browse files Browse the repository at this point in the history
Removed `Android.mk` and `Application.mk`, allowing `CMake` to run directly from the build.gradle file. Users no longer need to check out `NDK` or depend on it, simplifying the usage of the Flac extension.

Also fixed a copy-pasted comment in `CMakeLists.txt` of Opus and IAMF.

PiperOrigin-RevId: 684769561
  • Loading branch information
rohitjoins authored and copybara-github committed Oct 11, 2024
1 parent a2eda33 commit c78abaa
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 132 deletions.
28 changes: 11 additions & 17 deletions libraries/decoder_flac/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ To use the module you need to clone this GitHub project and depend on its
modules locally. Instructions for doing this can be found in the
[top level README][].

In addition, it's necessary to build the module's native components as follows:
In addition, it's necessary to fetch libflac as follows:

* Set the following environment variables:

Expand All @@ -26,30 +26,24 @@ cd "<path to project checkout>"
FLAC_MODULE_PATH="$(pwd)/libraries/decoder_flac/src/main"
```

* Download the [Android NDK][] and set its location in an environment variable.
This build configuration has been tested on NDK r21.

```
NDK_PATH="<path to Android NDK>"
```

* Download and extract flac-1.3.2 as "${FLAC_MODULE_PATH}/jni/flac" folder:
* Fetch libflac:

```
cd "${FLAC_MODULE_PATH}/jni" && \
curl https://ftp.osuosl.org/pub/xiph/releases/flac/flac-1.3.2.tar.xz | tar xJ && \
mv flac-1.3.2 flac
git clone https://github.com/xiph/flac.git libflac
```

* Build the JNI native libraries from the command line:
* [Install CMake][]

```
cd "${FLAC_MODULE_PATH}"/jni && \
${NDK_PATH}/ndk-build APP_ABI=all -j4
```
Having followed these steps, gradle will build the module automatically when run
on the command line or via Android Studio, using [CMake][] and [Ninja][] to
configure and build libflac and the module's [JNI wrapper library][].

[top level README]: ../../README.md
[Android NDK]: https://developer.android.com/tools/sdk/ndk/index.html
[Install CMake]: https://developer.android.com/studio/projects/install-ndk
[CMake]: https://cmake.org/
[Ninja]: https://ninja-build.org
[JNI wrapper library]: src/main/jni/flac_jni.cc

## Build instructions (Windows)

Expand Down
30 changes: 26 additions & 4 deletions libraries/decoder_flac/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,19 @@ android {
namespace 'androidx.media3.decoder.flac'

sourceSets {
main {
jniLibs.srcDir 'src/main/libs'
jni.srcDirs = [] // Disable the automatic ndk-build call by Android Studio.
}
androidTest.assets.srcDir '../test_data/src/test/assets'
}

defaultConfig {
externalNativeBuild {
cmake {
arguments "-DWITH_OGG=OFF"
arguments "-DINSTALL_MANPAGES=OFF"
targets "flacJNI"
}
}
}

// TODO(Internal: b/372449691): Remove packagingOptions once AGP is updated
// to version 8.5.1 or higher.
packagingOptions {
Expand All @@ -33,6 +39,22 @@ android {
}
}

// Configure the native build only if libflac is present to avoid gradle sync
// failures if libflac hasn't been built according to the README instructions.
if (project.file('src/main/jni/libflac').exists()) {
android.externalNativeBuild.cmake {
path = 'src/main/jni/CMakeLists.txt'
version = '3.21.0+'
if (project.hasProperty('externalNativeBuildDir')) {
if (!new File(externalNativeBuildDir).isAbsolute()) {
ext.externalNativeBuildDir =
new File(rootDir, it.externalNativeBuildDir)
}
buildStagingDirectory = "${externalNativeBuildDir}/${project.name}"
}
}
}

dependencies {
implementation project(modulePrefix + 'lib-decoder')
// TODO(b/203752526): Remove this dependency.
Expand Down
40 changes: 0 additions & 40 deletions libraries/decoder_flac/src/main/jni/Android.mk

This file was deleted.

20 changes: 0 additions & 20 deletions libraries/decoder_flac/src/main/jni/Application.mk

This file was deleted.

51 changes: 51 additions & 0 deletions libraries/decoder_flac/src/main/jni/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#
# Copyright 2024 The Android Open Source Project
#
# 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.
#

cmake_minimum_required(VERSION 3.21.0 FATAL_ERROR)

# Enable C++11 features.
set(CMAKE_CXX_STANDARD 11)

# Define project name for your JNI module
project(libflacJNI C CXX)

set(libflac_jni_root "${CMAKE_CURRENT_SOURCE_DIR}")

# Build libflac.
add_subdirectory("${libflac_jni_root}/libflac"
EXCLUDE_FROM_ALL)

# Build libflacJNI.
add_library(flacJNI
SHARED
flac_jni.cc
flac_parser.cc)

# Add the include directory from libflac.
include_directories("${libflac_jni_root}/libflac/include")

# Locate NDK log library.
find_library(android_log_lib log)

# Link libflacJNI against used libraries.
target_link_libraries(flacJNI
PRIVATE android
PRIVATE FLAC
PRIVATE ${android_log_lib})

# Enable 16 KB ELF alignment.
target_link_options(flacJNI
PRIVATE "-Wl,-z,max-page-size=16384")
45 changes: 0 additions & 45 deletions libraries/decoder_flac/src/main/jni/flac_sources.mk

This file was deleted.

8 changes: 4 additions & 4 deletions libraries/decoder_flac/src/main/jni/include/flac_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@

#include <stdint.h>

// libFLAC parser
#include <FLAC/stream_decoder.h>

#include <array>
#include <cstdlib>
#include <string>
#include <vector>

// libFLAC parser
#include "FLAC/stream_decoder.h"

#include "include/data_source.h"
#include "../include/data_source.h"

typedef int status_t;

Expand Down
2 changes: 1 addition & 1 deletion libraries/decoder_iamf/src/main/jni/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ add_library(iamfJNI
# Locate NDK log library.
find_library(android_log_lib log)

# Link libgav1JNI against used libraries.
# Link libiamfJNI against used libraries.
target_link_libraries(iamfJNI
PRIVATE android
PRIVATE iamf
Expand Down
2 changes: 1 addition & 1 deletion libraries/decoder_opus/src/main/jni/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ add_library(opusV2JNI
# Locate NDK log library.
find_library(android_log_lib log)

# Link libgav1JNI against used libraries.
# Link libopusJNI against used libraries.
target_link_libraries(opusV2JNI
PRIVATE android
PRIVATE opus
Expand Down

0 comments on commit c78abaa

Please sign in to comment.