Unable to generate available prefab AAR with shared library.
Error happened while building library implemented prefab/prefab_publishing/mylibrary.
The module nativelib is created as subproject of prefab_publishing using Android Studio default native library template.
build.gradle file of nativelib:
plugins {
id 'com.android.library'
}
android {
compileSdkVersion 31
defaultConfig {
minSdkVersion 16
targetSdkVersion 31
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
externalNativeBuild {
cmake {
cppFlags ""
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
version "3.10.2"
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
buildFeatures { // ⎤ added line
prefab true // ⎥
} // ⎦
}
dependencies {
implementation project(':mylibrary') // added line
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
CMakeLists.txt file of nativelib:
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.10.2)
# Declares and names the project.
project("nativelib")
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
add_library( # Sets the name of the library.
nativelib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
nativelib.cpp)
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log)
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
nativelib
# Links the target library to the log library
# included in the NDK.
${log-lib})
find_package(mylibrary REQUIRED CONFIG) // ⎤ added line
target_link_libraries(nativelib mylibrary::mylibrary) // ⎦
Error happened while executing nativelib:build:
1: Task failed with an exception.
-----------
* What went wrong:
Execution failed for task ':nativelib:generateJsonModelDebug'.
> /Users/aoramd/Code/Library/NdkSample/prefab/prefab-publishing/nativelib/src/main/cpp/CMakeLists.txt : C/C++ debug|armeabi-v7a : CMake Error at /Users/aoramd/Code/Library/NdkSample/prefab/prefab-publishing/nativelib/src/main/cpp/CMakeLists.txt:50 (find_package):
Could not find a package configuration file provided by "mylibrary" with
any of the following names:
mylibraryConfig.cmake
mylibrary-config.cmake
Add the installation prefix of "mylibrary" to CMAKE_PREFIX_PATH or set
"mylibrary_DIR" to a directory containing one of the above files. If
"mylibrary" provides a separate development package or SDK, be sure it has
been installed.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
==============================================================================
2: Task failed with an exception.
-----------
* What went wrong:
Execution failed for task ':nativelib:generateJsonModelRelease'.
> /Users/aoramd/Code/Library/NdkSample/prefab/prefab-publishing/nativelib/src/main/cpp/CMakeLists.txt : C/C++ release|armeabi-v7a : CMake Error at /Users/aoramd/Code/Library/NdkSample/prefab/prefab-publishing/nativelib/src/main/cpp/CMakeLists.txt:50 (find_package):
Could not find a package configuration file provided by "mylibrary" with
any of the following names:
mylibraryConfig.cmake
mylibrary-config.cmake
Add the installation prefix of "mylibrary" to CMAKE_PREFIX_PATH or set
"mylibrary_DIR" to a directory containing one of the above files. If
"mylibrary" provides a separate development package or SDK, be sure it has
been installed.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
==============================================================================
* Get more help at https://help.gradle.org
Building mylibrary as a static library is OK. Do prefab not support shared library?
Environment
macOS Big Sur 11.4
Android Studio Version
Android Studio Arctic Fox | 2020.3.1 Build #AI-203.7717.56.2031.7583922, built on July 27, 2021 Runtime version: 11.0.10+0-b96-7281165 x86_64 VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o. macOS 11.4 GC: G1 Young Generation, G1 Old Generation Memory: 16384M Cores: 20 Registry: external.system.auto.import.disabled=true Non-Bundled Plugins: org.jetbrains.kotlin
find_package(mylibrary REQUIRED CONFIG) ,the first parameter "mylibrary " should be the same with the name of your prefab-publishing project 's name !
find_package(mylibrary REQUIRED CONFIG) ,the first parameter "mylibrary " should be the same with the name of your prefab-publishing project 's name !
Yes, but the name of the sample library prefab/prefab_publishing/mylibrary is also "mylibrary". The name is not the problem.
Sample builds fine today, so presumably obsolete.