NDK Error "Unexpected native build target xyz. Valid targets are:" - gradle

I have an Android Studio project which depends on a native shared library. I have created a cmake file to compile the library and I have added a soft link to the shared library inside the android project (in src/main/jniLibs/armeabi). That way when the android project is built, the shared library is included in the package.
Here is the relevant part of build.gradle:
android {
...
externalNativeBuild {
cmake {
path "../cpp/CMakeLists.txt"
}
}
}
The problem is that gradle tries to open the shared library before invoking the instructions to build it.
Information:Gradle tasks [:app:assembleDebug]
Error:Could not list contents of 'app/src/main/jniLibs/armeabi/libfoo.so'. Couldn't follow symbolic link.
How can I invoke the cmake from inside the project and include the library in the project at the same time?
--
EDIT
In the cmake the shared library is built with ExternalProject_Add. Unfortunately gradle doesn't see that target, nor does it see imported shared libraries as targets. So this does not work:
add_library(libfoo SHARED IMPORTED GLOBAL)
add_dependencies(libfoo libactual)
I tried to invoke building the particular target with a gradle config:
defaultConfig {
...
externalNativeBuild {
cmake {
targets "libfoo"
}
}
}
But gradle still doesn't see it and fails with:
Unexpected native build target libfoo. Valid values are:
The valid values are basically an empty list.
Currently I work around this by creating a fictional executable depending on the library.
add_executable(libfoo a.c)
add_dependencies(libfoo libactual)

In my case, I added a new CMake target, but having none was cached somehow (by CMake or Gradle).
Simply close Android Studio, remove the entire build or .build directory, then open Android Studio and build again.
Note that sub-projects have their own separate build directory.
So, you may need to search for the word build, and after ensuring found result is not required, remove them too.
If still not fixed, remember that CMake has it's own separate cache files, which normallay are inside said directories unless you run CMake directly (outside of Android Studio).

Related

Android Studio NDK error, couldn't find GLES3/gl3.h although it exist

I'm trying to make an app on Android Studio that use the NDK and OpenGL ES 3.0.When I #include < GLES3/gl3.h >, the IDE has auto completion as I typing, I think it's a sign meaning that the IDE can find it
However, I got the error : "Error:(22, 10) fatal error: 'GLES3/gl3.h' file not found" when I build the project. I check the NDK path in Project Structure, which is :
sdk\ndk-bundle\platforms\android-21\arch-arm64\usr\include\GLES3
it's correct and the GLES3/gl3.h does exist there.
I have declared my CMakeList with GLESv3 already:
cmake_minimum_required(VERSION 3.4.1)
# now build app's shared lib
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall")
add_library(gl3jni SHARED
gl_code.cpp
stb_image.cpp
)
# add lib dependencies
target_link_libraries(gl3jni
android
log
EGL
GLESv3)
I also declare the OpenGL ES version in the Manifest.xml with:
<uses-feature android:glEsVersion="0x00030000" android:required="true"/>
What am I doing wrong?
Android ndk-bundle has openglES3 since api 18, but in arm platform.
I mean, if you are going to compile your project in armV8_64, you must set your min sdk to 21. But if you are going to use armeabi or armeabiV7 the minimum api will be 18.
So change your minSDK dependig on your preferences in the app/build.gradle file.
I suggest you to define the API 21 and define your product flavours to support for all architectures, besides you can make other 3rdparty library linkings, the code should be something like this:
android.productFlavors {
// for detailed abiFilter descriptions, refer to "Supported ABIs" #
// https://developer.android.com/ndk/guides/abis.html#sa
create("arm") {
ndk.abiFilters.add("armeabi")
ndk.ldFlags.add("-L${file(''your_libraries_path'')}".toString())
ndk.ldLibs.addAll(["your_armeabi_library"])
}
create("arm7") {
ndk.abiFilters.add("armeabi-v7a")
ndk.ldFlags.add("-L${file('your_libraries_path')}".toString())
ndk.ldLibs.addAll(["your_armv7_library"])
}
create("arm8") {
ndk.abiFilters.add("arm64-v8a")
ndk.ldFlags.add("-L${file(''your_libraries_path')}".toString())
ndk.ldLibs.addAll(["your_armv8_library"])
}
}
This gradle code is from the gradle 0.8.3 experimental plugin, so if you have not this version, you need to make some changes to fit into your gradle version.
For me I only have to change the minSdkVersion in build.gradle to 19.
do not forget to set
APP_PLATFORM := android-23 (or other depending on uelordi answer)
to application.mk

Cannot resolve symbol TexturePacker

Using libgdx 1.7.0/Android Studio, TexturePacker is supposed to be included out of the box if checking the tools option when creating the project (and so I did).
In fact, if I check my build.gradle file, in the project(":desktop") section I have the compile "com.badlogicgames.gdx:gdx-tools:$gdxVersion" added.
But even with that, the build is not able to find the tools package (even though I can successfully use the Controllers extension, which should be the same I think)
I'll leave here the desktop part of the build.gradle, just in case:
project(":desktop") {
apply plugin: "java"
dependencies {
compile project(":core")
compile "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion"
compile "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
compile "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-desktop"
compile "com.badlogicgames.gdx:gdx-controllers-desktop:$gdxVersion"
compile "com.badlogicgames.gdx:gdx-controllers-platform:$gdxVersion:natives-desktop"
compile "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-desktop"
compile "com.badlogicgames.gdx:gdx-tools:$gdxVersion"
}
}
And a image with the libraries in the project, where you can see the tools...
This is an issue caused by importing tools in Core Dependency. Remove the dependency in the project(":desktop") of the Core Dependency and add it to desktop dependency.
You can also solve it by downloading the gdx tools and importing the jar file. Just create a library folders and paste the jar file. Then go to File > Project Structure > Modules and add the File Dependency which is your jar file.
dependencies {
compile files('libs/runnable-texturepacker.jar')
}
This should work fine.
I was trying to use the TexturePacker class within a class in the core module. However I discovered it to only be available in the desktop module.
This seems logical as the dependency of the tools extension is placed within the desktop project in the root build.gradle file when using the setup utility or following the official instructions to add the extension manually (see Add tools dependency).
Technically, you could move the dependency in the module you want to use TexturePacker in (say core), but according to the provided link this is discouraged. So I recommend you just write your class using TexturePacker within the desktop module.
PS: Note that due to the deprecation of "compile" a replacement by "implementation" in build.gradle might become necessary, but Android Studio will inform you in that case (use ctrl + r for efficient replacements).

Gradle plugin for Android not creating source code folder

In my Android Studio project, I added the following build type to the build.gradle file:
jnidebug.initWith(buildTypes.debug)
jnidebug {
packageNameSuffix ".jnidebug"
jniDebuggable true
}
The documentation at:
http://tools.android.com/tech-docs/new-build-system/user-guide
says:
For each Build Type, a new matching sourceSet is created, with a
default location of src/<buildtypename>/
But when I resync gradle, the source code folder src/jnidebug never gets created. What am I doing wrong?
The documentation says that for each build type, a source set (which is a logical concept/domain object) is created and configured with a default location. It doesn't say that a source directory is created. You'll probably have to create the directory yourself. (Android Studio could certainly help with that, so perhaps file a feature request.)

Publishing a precompiled CocoaPods

We are currently building an SDK for a customer using CocoaPods.
The main problem we have is that our boss would like the SDK to be a black box. He wants us to precompile the code in order to protect our source.
Is there anything we can do within the Podspec in order to protect our code?
You can do exactly that by creating a Static Framework and including it in the spec.vendored_frameworks property on your podspec.
http://guides.cocoapods.org/syntax/podspec.html#vendored_frameworks
Follow the tutorial below for how to create your own static framework.
https://github.com/jverkoey/iOS-Framework#walkthrough
How to Create a Static Framework for iOS
There are a few constraints that we want to satisfy when building a .framework:
Fast iterative builds when developing the framework. We may have a simple application that has the
.framework as a dependency and we want to quickly iterate on development of the .framework.
Infrequent distribution builds of the .framework.
Resource distribution should be intuitive and not bloat the application.
Setup for third-party developers using the .framework should be easy.
I believe that the solution I will outline below satisfies each of these constraints. I will outline
how to build a .framework project from scratch so that you can apply these steps to an existing
project if you so desire. I will also include project templates for easily creating a
.framework.
Overview
View a sample project that shows the result of following these steps in the sample/Serenity
directory.
Within the project we are going to have three targets: a static library, a bundle, and an aggregate.
The static library target will build the source into a static library (.a) and specify which headers
will be "public", meaning they will be accessible from the .framework when we distribute it.
The bundle target will contain all of our resources and will be loadable from the framework.
The aggregate target will build the static library for i386/armv6/armv7/armv7s, generate the fat framework
binary, and also build the bundle. You will run this target when you plan to distribute the
.framework.
When you are working on the framework you will likely have an internal application that links to the
framework. This application will link to the static library target as you normally would and copy
the .bundle in the copy resources phase. This has the benefit of only building the framework code
for the platform you're actively working on, significantly improving your build times. We'll do a
little bit of work in the framework project to ensure that you can use your framework in your app
the same way a third party developer would (i.e. importing should work
as expected). Jump to the dependent project walkthrough.
Create the Static Library Target
Step 1: Create a New "Cocoa Touch Static Library" Project
The product name will be the name of your framework. For example, Serenity will generate
Serenity.framework once we've set up the project.
Step 2: Create the Primary Framework Header
Developers expect to be able to import your framework by importing the <Serenity/Serenity.h>
header. Ensure that your project has such a header (if you created a new static library then there
should already be a Serenity.h and Serenity.m file; you can delete the .m).
Within this header you are going to import all of the public headers for your framework. For
example, let's assume that we have some Widget with a .h and .m. Our Serenity.h file would look
like this:
#import <Foundation/Foundation.h>
#import <Serenity/Widget.h>
Once you've created your framework header file, you need to make it a "public" header. Public
headers are headers that will be copied to the .framework and can be imported by those using your
framework. This differs from "project" headers which will not be distributed with the framework.
This distinction is what allows you to have a concept of public and private APIs.
To change a file's [target membership visibility in XCode 4.4+]
(Can't change target membership visibility in Xcode 4.5),
you'll need to select the Static Library target you created (Serenity), open the Build Phases tab:
Xcode 4.X:
Click on Add Build Phase > Add Copy Headers.
Xcode 5:
Add Build Phases from the menu. Click on Editor > Add Build Phase -> Add Copy Headers Build Phase. Note: If the menu options are grayed out, you'll need to click on the whitespace below the Build Phases to regain focus and retry.
You'll see 3 sections for Public, Private, and Project headers. To modify the scope of any header, drag and drop the header files between the sections. Alternatively you can open the Project Navigator and select the header. Next expand the Utilities pane for the File Inspector.
(Cmd+Option+0).
Look at the "Target Membership" group and ensure that the checkbox next to the .h file is checked.
Change the scope of the header from "Project" to "Public". You might have to uncheck and check the box to get the dropdown list. This will ensure that the header gets
copied to the correct location in the copy headers phase.
Step 3: Update the Public Headers Location
By default the static library project will copy private and public headers to the same folder:
/usr/local/include. To avoid mistakenly copying private headers to our framework we want to ensure
that our public headers are copied to a separate directory, e.g. $(PROJECT_NAME)Headers. To change this setting,
select the project in the Project Navigator and then click the "Build Settings" tab. Search for "public
headers" and then set the "Public Headers Folder Path" to "$(PROJECT_NAME)Headers" for all configurations.
If you are working with multiple Frameworks make sure that this folder is unique.
Ongoing Step: Adding New Sources to the Framework
Whenever you add new source to the framework you must decide whether to expose the .h publicly or
not. To modify a header's scope you will follow the same process as Step 2. By default a header's
scope will be "Project", meaning it will not be copied to the framework's public headers.
Step 4: Disable Code Stripping
We do not want to strip any code from the library; we leave this up to the application that is
linking to the framework. To disable code stripping we must modify the following configuration
settings:
"Dead Code Stripping" => No (for all settings)
"Strip Debug Symbols During Copy" => No (for all settings)
"Strip Style" => Non-Global Symbols (for all settings)
Step 5: Prepare the Framework for use as a Dependent Target
In order to use the static library as though it were a framework we're going to generate the basic
skeleton of the framework in the static library target. To do this we'll include a simple post-build
script. Add a post-build script by selecting your project in the Project Navigator, selecting the target, and then the
"Build Phases" tab.
Xcode 4.X: Click Add Build Phase > Add Run Script
Xcode 5: Select Editor menu > Add Build Phase > Add Run Script Build Phase
Paste the following script in the source portion of the run script build phase. You can rename the phase by clicking
the title of the phase (I've named it "Prepare Framework", for example).
prepare_framework.sh
set -e
mkdir -p "${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.framework/Versions/A/Headers"
# Link the "Current" version to "A"
/bin/ln -sfh A "${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.framework/Versions/Current"
/bin/ln -sfh Versions/Current/Headers "${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.framework/Headers"
/bin/ln -sfh "Versions/Current/${PRODUCT_NAME}" "${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.framework/${PRODUCT_NAME}"
# The -a ensures that the headers maintain the source modification date so that we don't constantly
# cause propagating rebuilds of files that import these headers.
/bin/cp -a "${TARGET_BUILD_DIR}/${PUBLIC_HEADERS_FOLDER_PATH}/" "${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.framework/Versions/A/Headers"
This will generate the following folder structure:
-- Note: "->" denotes a symbolic link --
Serenity.framework/
Headers/ -> Versions/Current/Headers
Serenity -> Versions/Current/Serenity
Versions/
A/
Headers/
Serenity.h
Widget.h
Current -> A
Try building your project now and look at the build products directory (usually
~/Library/Developer/Xcode/DerivedData/<ProjectName>-<gibberish>/Build/Products/...). You should
see a libSerenity.a static library, a Headers folder, and a Serenity.framework folder that
contains the basic skeleton of your framework.
Create the Framework Distribution Target
When actively developing the framework we only care to build the platform that we're testing on. For
example, if we're testing on the iPhone simulator then we only need to build the i386 platform.
This changes when we want to distribute the framework to third party developers. The third-party
developers don't have the option of rebuilding the framework for each platform, so we must provide
what is called a "fat binary" version of the static library that is comprised of the possible
platforms. These platforms include: i386, armv6, armv7, and armv7s.
To generate this fat binary we're going to build the static library target for each platform.
Step 1: Create an Aggregate Target
Click File > New Target > iOS > Other and create a new Aggregate target. Title it something like "Framework".
Step 2: Add the Static Library as a Dependent Target
Add the static library target to the "Target Dependencies".
Step 3: Build the Other Platform
To build the other platform we're going to use a "Run Script" phase to execute some basic commands.
Add a new "Run Script" build phase to your aggregate target and paste the following code into it.
build_framework.sh
set -e
set +u
# Avoid recursively calling this script.
if [[ $SF_MASTER_SCRIPT_RUNNING ]]
then
exit 0
fi
set -u
export SF_MASTER_SCRIPT_RUNNING=1
SF_TARGET_NAME=${PROJECT_NAME}
SF_EXECUTABLE_PATH="lib${SF_TARGET_NAME}.a"
SF_WRAPPER_NAME="${SF_TARGET_NAME}.framework"
# The following conditionals come from
# https://github.com/kstenerud/iOS-Universal-Framework
if [[ "$SDK_NAME" =~ ([A-Za-z]+) ]]
then
SF_SDK_PLATFORM=${BASH_REMATCH[1]}
else
echo "Could not find platform name from SDK_NAME: $SDK_NAME"
exit 1
fi
if [[ "$SDK_NAME" =~ ([0-9]+.*$) ]]
then
SF_SDK_VERSION=${BASH_REMATCH[1]}
else
echo "Could not find sdk version from SDK_NAME: $SDK_NAME"
exit 1
fi
if [[ "$SF_SDK_PLATFORM" = "iphoneos" ]]
then
SF_OTHER_PLATFORM=iphonesimulator
else
SF_OTHER_PLATFORM=iphoneos
fi
if [[ "$BUILT_PRODUCTS_DIR" =~ (.*)$SF_SDK_PLATFORM$ ]]
then
SF_OTHER_BUILT_PRODUCTS_DIR="${BASH_REMATCH[1]}${SF_OTHER_PLATFORM}"
else
echo "Could not find platform name from build products directory: $BUILT_PRODUCTS_DIR"
exit 1
fi
# Build the other platform.
xcrun xcodebuild -project "${PROJECT_FILE_PATH}" -target "${TARGET_NAME}" -configuration "${CONFIGURATION}" -sdk ${SF_OTHER_PLATFORM}${SF_SDK_VERSION} BUILD_DIR="${BUILD_DIR}" OBJROOT="${OBJROOT}" BUILD_ROOT="${BUILD_ROOT}" SYMROOT="${SYMROOT}" $ACTION
# Smash the two static libraries into one fat binary and store it in the .framework
xcrun lipo -create "${BUILT_PRODUCTS_DIR}/${SF_EXECUTABLE_PATH}" "${SF_OTHER_BUILT_PRODUCTS_DIR}/${SF_EXECUTABLE_PATH}" -output "${BUILT_PRODUCTS_DIR}/${SF_WRAPPER_NAME}/Versions/A/${SF_TARGET_NAME}"
# Copy the binary to the other architecture folder to have a complete framework in both.
cp -a "${BUILT_PRODUCTS_DIR}/${SF_WRAPPER_NAME}/Versions/A/${SF_TARGET_NAME}" "${SF_OTHER_BUILT_PRODUCTS_DIR}/${SF_WRAPPER_NAME}/Versions/A/${SF_TARGET_NAME}"
Important Note
The above script assumes that your library name matches your project name in the following line:
SF_TARGET_NAME=${PROJECT_NAME}
If this is not the case (e.g. your xcode project is named SerenityFramework and the target name is
Serenity) then you need to explicitly set the target name on that line. For example:
SF_TARGET_NAME=Serenity
Step 4: Build and Verify
You now have everything set up to build a distributable .framework to third-party developers. Try
building the aggregate target. Once it's done, expand the Products folder in Xcode, right click the
static library and click "Show in Finder". If this doesn't open Finder to where the static library
exists then try opening
~/Library/Developer/Xcode/DerivedData/<project name>/Build/Products/Debug-iphonesimulator/.
Within this folder you will see your .framework folder.
You can now drag the .framework elsewhere, zip it up, upload it, and distribute it to your
third-party developers.
I succeeded using that Podspec as an example :
Pod::Spec.new do |s|
s.name = "EstimoteSDK"
s.version = "1.3.0"
s.summary = "iOS library for Estimote iBeacon devices"
s.homepage = "http://estimote.com"
s.author = { "Estimote, Inc" => "contact#estimote.com" }
s.platform = :ios
s.source = { :git => "https://github.com/Estimote/iOS-SDK.git", :tag => " {s.version}" }
s.source_files = 'EstimoteSDK/Headers/*.h'
s.preserve_paths = 'EstimoteSDK/libEstimoteSDK.a'
s.vendored_libraries = 'EstimoteSDK/libEstimoteSDK.a'
s.ios.deployment_target = '7.0'
s.frameworks = 'UIKit', 'Foundation', 'SystemConfiguration', 'MobileCoreServices', 'CoreLocation'
s.requires_arc = true
s.xcconfig = { 'LIBRARY_SEARCH_PATHS' => '"$(PODS_ROOT)/EstimoteSDK"',
'HEADER_SEARCH_PATHS' => '"${PODS_ROOT}/Headers/EstimoteSDK"' }
s.license = {
:type => 'Copyright',
:text => <<-LICENSE
Copyright 2013 Estimote, Inc. All rights reserved.
LICENSE
}
end
Static Libraries are not supported in Swift, so for anyone coming here looking for the solution for a Swift SDK, here is a nice article explaining how it should be done.
Update
Swift 4 now natively supports static swift libraries.
Side note:
For Anyone still interested in creating a swift dynamic library, this is still a nice, very helpful article article

Android studio include aar dependency

I'm trying to add ActionbarSherlock as dependency using line I got from gradleplease
(Instead of these methods. At least according to this link:
"In Gradle you no longer need to add in these libraries as source code projects; you can simply refer to them as dependencies, and the build system will handle the rest; downloading, merging in resources and manifest entries, etc. For each library, look up the corresponding AAR library dependency name (provided the library in question has been updated as a android library artifact), and add these to the dependency section."
this setup should not be necessary anymore)
But it doesn't work and module settings in Android studio shows error: "Library 'ComActionbarsherlockComActionbarsherlock440.aar': Invalid classes root"
Any idea?
Add these line in your module build.gradle
dependencies {
compile 'com.actionbarsherlock:actionbarsherlock:4.4.0#aar'
}

Resources