英文:
Can't use function from android ndk
问题
I'm trying to use the function AMotionEvent_getActionButton
from Android NDK
But I get the following error
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
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?
英文:
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?
答案1
得分: 0
从文档中可以看到:
compileSdkVersion
此属性不会影响 NDK 构建。NDK 的 API 可用性由
minSdkVersion
来管理。这是因为 C++ 符号会在库加载时被立即解析,而不是在首次调用时才懒惰解析(就像在 Java 中一样)。使用任何不在minSdkVersion
中可用的符号将导致库在不具备较新 API 的操作系统版本上加载失败,无论这些 API 是否被调用。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论