I'm trying to use the function AMotionEvent_getActionButton from Android NDK
But I get the following error
C/C++: /usr/home/ndk-project/main.c:188:26: error: 'AMotionEvent_getActionButton' is unavailable: introduced in Android 33
C/C++: /usr/android-sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include/android/input.h:1370:9: note: 'AMotionEvent_getActionButton' has been explicitly marked unavailable here
It says the function was introduced in Android 33
I am using Android 33
The concerned line of code
int32_t buttonPress = AMotionEvent_getActionButton(event);
Module Level build.gradle
plugins {
id 'com.android.application'
}
android {
compileSdk 33
ndkVersion '25.1.8937393'
defaultConfig {
applicationId = 'com.example.app'
minSdkVersion 14
targetSdkVersion 33
externalNativeBuild {
cmake {
arguments '-DANDROID_STL=c++_static'
}
}
ndk {
abiFilters 'x86_64'
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
}
lintOptions {
abortOnError false
}
externalNativeBuild {
cmake {
path 'src/main/cpp/CMakeLists.txt'
}
}
namespace 'com.example.app'
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
}
Why I am I getting this error?
How do I fix it?
From the documentation:
compileSdkVersion
This property has no effect on NDK builds. API availability for the NDK is instead governed by minSdkVersion. This is because C++ symbols are eagerly resolved at library load time rather than lazily resolved when first called (as they are in Java). Using any symbols that are not available in the minSdkVersion will cause the library to fail to load on OS versions that do not have the newer API, regardless of whether or not those APIs will be called.
Related
I'm trying to get Mobile GTS' prebuilt Candy Jump app to run and build an APK in Android Studio. I've followed this video precisely: https://www.youtube.com/watch?v=YpYVdJVYWg0.
However, I am using Gradle 4.7, JDK 1.8.0, Android Plugin Version 3.1.2 and Build Tools Version 27.0.3. I'm not sure if that would have anything to do with why I can't get it to build, but I've had error after error and I've kind of boiled it down to what I hope is the last error.
Supplied String module notation 'libs/dagger-1.2.2.jar' is invalid. Example notations: 'org.gradle:gradle-core:2.2', 'org.mockito:mockito-core:1.9.5:javadoc'.
How can I fix this error?
Here's my build.gradle (module: app)
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
buildToolsVersion '27.0.3'
defaultConfig {
applicationId "net.mobilegts.candyjump"
minSdkVersion 19
targetSdkVersion 27
multiDexEnabled true
ndk {
moduleName "player_shared"
}
}
buildTypes {
release {
minifyEnabled false
shrinkResources false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
sourceSets.main {
jni.srcDirs = []// <-- disable automatic ndk-build call
}
productFlavors {
}
}
dependencies {
implementation('com.google.android.gms:play-services:+') { exclude module: 'support-v4' }
implementation 'libs/dagger-1.2.2.jar'
implementation 'libs/javax.inject-1.jar'
implementation 'libs/nineoldandroids-2.4.0.jar'
implementation 'libs/support-v4-19.0.1.jar'
implementation 'com.android.support:multidex:1.0.3'
implementation 'com.android.support:support-core-utils:27.1.1'
}
You should use "implementation files" for jar files. Try the following code:
implementation files('libs/dagger-1.2.2.jar')
implementation files('libs/javax.inject-1.jar')
implementation files('libs/nineoldandroids-2.4.0.jar')
implementation files('libs/support-v4-19.0.1.jar')
Is there a way to get an Android Instant App working with a native C++ library?
I'm attempting to publish an Android Instant App to a device/simulator, but ran into problems with my native C++ library. It publishes fine as an installable app, but fails to find the library when published as an Instant App.
To eliminate any other issues, I started a new project in Android Studio 3.0 (Canary 1 171.4010489) with the new project wizard and selected the following settings:
First Page:
Include C++ support checked
Second Page:
Phone and Tablet selected
Include Android Instant App support checked
Sixth Page:
C++ Standard set to 'C++11'
Exceptions Support (-fexceptions) checked
Runtime Type Information Support (-frtti) checked
The resulting project will publish as an installable app (showing the 'Hello from C++' screen), but not an instant app... it gives the following error that it can't find the library, which is the same error I get in my actual app's project:
couldn't find "libnative-lib.so"
Full error:
05-24 17:48:30.316 7519-7519/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.mycompany.instantapp, PID: 7519
java.lang.UnsatisfiedLinkError: byc[DexPathList[[zip file "/data/user/0/com.google.android.instantapps.supervisor/files/atom-cache/com.mycompany.instantapp/atom-download--feature-1495662507463/feature.jar"],nativeLibraryDirectories=[/data/user/0/com.google.android.instantapps.supervisor/files/native-lib/com.mycompany.instantapp, /system/lib, /vendor/lib]]] couldn't find "libnative-lib.so"
...
I'm pasting the relevant gradle files below (all generated by Android Studio):
app/build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "26.0.0 rc2"
defaultConfig {
applicationId "com.mycompany.instantapp"
minSdkVersion 23
targetSdkVersion 25
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation project(':feature')
implementation project(':base')
}
base/build.gradle:
apply plugin: 'com.android.feature'
android {
compileSdkVersion 25
buildToolsVersion "26.0.0 rc2"
baseFeature true
defaultConfig {
minSdkVersion 23
targetSdkVersion 25
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
feature project(':feature')
compile 'com.android.support:appcompat-v7:25.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
}
feature/build.gradle:
apply plugin: 'com.android.feature'
android {
compileSdkVersion 25
buildToolsVersion "26.0.0 rc2"
defaultConfig {
minSdkVersion 23
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags "-std=c++11 -frtti -fexceptions"
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
implementation project(':base')
testCompile 'junit:junit:4.12'
}
instantapp/build.gradle:
apply plugin: 'com.android.instantapp'
dependencies {
implementation project(':feature')
implementation project(':base')
}
Updates:
I've filed an issue with Google:
Link: Google Issue Tracker
Though I feel like the tools to make this happen are already available (Gradle, CMake, NDK, etc)
Also thanks #Anirudh for letting me know that this is a known issue on Android N.
Does publishing an Instant App with no C++ library work on my device?
Yes... if I create a new Android Studio project with only Include Android Instant App support it publishes to my Samsung Galaxy 7S and shows the 'Hello World!' screen.
Does publishing a signed APK work?
Generating a signed APK works, and upon inspection the native C++ library is bundled with the feature-debug.apk but not the base-debug.apk. This is what I would expect given the gradle configuration, but doesn't explain why it won't publish to a device/simulator.
I haven't tried sideloading these APKs... but I'm skeptical if that is even possible given that the Instant App is never installed... ex: how would you even launch it after sideloading it (click a url?)
Does adding the C++ library to both APKs work?
I've tried adding the externalNativeBuild gradle properties to both the base/build.gradle and the feature/build.gradle files, but the same error still occurs. I verified that the native C++ library is then included in both APKs by inspecting both the feature-debug.apk and the base-debug.apk after generating a signed APK.
modified base/build.gradle:
apply plugin: 'com.android.feature'
android {
compileSdkVersion 25
buildToolsVersion "26.0.0 rc2"
baseFeature true
defaultConfig {
minSdkVersion 23
targetSdkVersion 25
versionCode 1
versionName "1.0"
externalNativeBuild {
cmake {
cppFlags "-std=c++11 -frtti -fexceptions"
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "../feature/CMakeLists.txt"
}
}
}
dependencies {
feature project(':feature')
compile 'com.android.support:appcompat-v7:25.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
}
Does publishing a signed APK work?
Android Studio 3.0 preview Generate Signed APK feature has a bug currently where the final zip doesn't include all feature apks. Use Gradle SigningConfig in each feature module's gradle file to sign your feature apks
Does adding the C++ library to both APKs work?
Not required. Adding to base feature apk should be enough
The actual crash is known issue with NDK support for Android Instant Apps on Android M/N. The app works on Android O emulator
Android Studio 1.1 generated apk file (located # app/build/outputs/apk folder) contains the lib directory, and for every cpu type there exists a non-empty folder, like /x86. Each of these folder contain a libapp.so shared library that is around 5Kb in size per cpu.
I've searched the net and the only thing I found so far is this link from Intel https://software.intel.com/en-us/articles/building-native-android-apps-using-intelr-c-compiler-in-android-studio that shows how to change the default libapp.so to user-provided library.
So, I guess that this library (libapp.so) is somehow built by gradle.
In fact I do my own native library building, using ndk-build command line tool, and my libs are placed alongside that libapp.so. It's not causing any issues btw, but I feel that I'm losing the control over what is built and why.
Here's my humble build.gradle file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
/**
* Path to *.so files
*/
sourceSets {
main {
jniLibs.srcDirs = ['src/main/libs']
jni.srcDirs = [] //disable automatic ndk-build
}
}
defaultConfig {
applicationId "com.sample.android"
minSdkVersion 9
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
}
How can I disable this libapp.so being built?
defaultConfig {
ndk {
moduleName "yourSelfLib"
}
}
I need to develop an audio app in NDK using Android Studio. I have added
the ndk path to local.properties -
ndk.dir=/opt/android-ndk-r10
sdk.dir=/opt/adt-bundle-linux-x86_64-20140702/sdk
In build.gradle I added an entry for OpenSLES -
apply plugin: 'com.android.application'
android {
compileSdkVersion 20
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.example.hellojni"
minSdkVersion 8
targetSdkVersion 21
ndk {
moduleName "HelloJNI"
ldLibs "OpenSLES" // Link with these libraries!
stl "stlport_shared"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile 'com.android.support:support-v4:20.0.0'
compile 'com.android.support:appcompat-v7:20.0.0'
}
Next I tried to add #includes for opensl -
#include <SLES/OpenSLES.h>
#include <SLES/OpenSLES_Android.h>
but the IDE is not recognising the headers saying it cannot find the include files. I also tried importing the native-audio project into android studio but that too did not compile or run. I am aware that the official support is still not there for using NDK with Android Studio. But I have seen some videos that show how to integrate ndk with android studio.
Is there a way to do it.
Thanks in advance
OpenSL library is available for android platforms with API 9+, so you may want to change the mininimum required sdk.
Not sure how NDK chooses for which platform to compile, but you may need to compile yourself also using a custom Application.mk file like this:
APP_ABI := armeabi
APP_PLATFORM := android-9
TARGET_PLATFORM := android-9
Also, you should always target and compile with the highest available sdk.
Here's an example of what your build file should look like:
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.example.hellojni"
minSdkVersion 9
targetSdkVersion 22
ndk {
moduleName "HelloJNI"
ldLibs "OpenSLES" // Link with these libraries!
stl "stlport_shared"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.1.1'
}
Note: With the above configuration, the libraries are found for me (with the built-in ndk configurations).
I want to use c++ STL in android, but when I include STL (ex: hash_map), then run that occur error.
I use android studio 1.0.2 and android ndk r10d
This is my build.gradle file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.example.ndktest"
minSdkVersion 15
targetSdkVersion 21
versionCode 1
versionName "1.0"
ndk {
moduleName 'JNI'
stl 'stlport_shared'
}
}
productFlavors {
arm {
ndk {
abiFilters "armeabi", "armeabi-v7a"
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
}
Error Message:
WARNING [Project: :app] Current NDK support is deprecated. Alternative will be provided in the future.
WARNING [Project: :app] Current NDK support is deprecated. Alternative will be provided in the future.
:app:preBuild
:app:compileArmDebugNdk
In file included from C:/Users/Sheng/Developer/android/SDK_Libs/android-ndk/sources/cxx-stl/stlport/stlport/stl/_algobase.h:46:0,
from C:/Users/Sheng/Developer/android/SDK_Libs/android-ndk/sources/cxx-stl/stlport/stlport/stl/_vector.h:34,
from C:/Users/Sheng/Developer/android/SDK_Libs/android-ndk/sources/cxx-stl/stlport/stlport/stl/_hashtable.h:34,
from C:/Users/Sheng/Developer/android/SDK_Libs/android-ndk/sources/cxx-stl/stlport/stlport/stl/_hash_map.h:34,
from C:/Users/Sheng/Developer/android/SDK_Libs/android-ndk/sources/cxx-stl/stlport/stlport/hash_map:38,
from C:\Users\Sheng\tmp\NDKTEST\app\src\main\jni\maps.h:5,
from C:\Users\Sheng\tmp\NDKTEST\app\src\main\jni\maps.cpp:1:
C:/Users/Sheng/Developer/android/SDK_Libs/android-ndk/sources/cxx-stl/stlport/stlport/stl/_cstdlib.h: In function 'long int abs(long int)':
C:/Users/Sheng/Developer/android/SDK_Libs/android-ndk/sources/cxx-stl/stlport/stlport/stl/_cstdlib.h:131:25: error: declaration of C function 'long int abs(long int)' conflicts with
inline long abs(long __x) { return _STLP_VENDOR_CSTD::labs(__x); }
^
more...
How can I solve?
Thanks.