在过去我使用eclipse进行NDK项目,Android.mk文件非常适合用API级别9编译NDK,同时让应用程序(SDK)在API级别22上编译 . 但似乎这在使用时不可能使用Android Studio 1.3 RC1的实验性Gradle构建系统(2.5) .
What can I do to compile ONLY the NDK on API level 9?
我的典型Android.mk文件如下所示:
APP_PLATFORM := android-9
APP_STL := stlport_static
APP_ABI := all
# Enable c++11 extentions in source code
APP_CPPFLAGS += -std=c++11
#Enable optimalization in release mode
APP_OPTIM := release
我的新gradle文件如下所示:
apply plugin: 'com.android.model.application'
model {
android {
compileSdkVersion = 22
buildToolsVersion = "23.0.0 rc3"
defaultConfig.with {
applicationId = "com.example"
minSdkVersion.apiLevel = 9
targetSdkVersion.apiLevel = 22
versionCode = 1
versionName = "1.0"
}
}
android.ndk {
moduleName = "NativeLibrary"
cppFlags += "-I${file("src/main/jni/some_folder")}".toString()
cppFlags += "-std=c++11"
//What should be added here to compile the NDK on API 9???
CFlags += "-DNDEBUG"
CFlags += "-fvisibility=hidden"
cppFlags += "-fvisibility=hidden"
ldLibs += ["log"]
stl = "stlport_static"
}
android.buildTypes {
release {
isMinifyEnabled = true
proguardFiles += file('D:/path/proguard-rules.pro')
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-v4:22.2.0'
}
我调查了Gradle源代码,似乎NDK构建目标的硬编码与compileSdkVersion相同 . 有没有一种方法可以避免或改变这种行为?
NdkCompile.groovy(创建文件)
// target
IAndroidTarget target = getPlugin().loadedSdkParser.target
if (!target.isPlatform()) {
target = target.parent
}
commands.add("APP_PLATFORM=" + target.hashString())
Sdk.groovy(目标是从compileSdkVersion获取的)
public SdkParser loadParser() {
checkNotNull(extension, "Extension has not been set")
// call getParser to ensure it's created.
SdkParser theParser = getParser()
if (!isSdkParserInitialized) {
String target = extension.getCompileSdkVersion()
if (target == null) {
throw new IllegalArgumentException("android.compileSdkVersion is missing!")
}
FullRevision buildToolsRevision = extension.buildToolsRevision
if (buildToolsRevision == null) {
throw new IllegalArgumentException("android.buildToolsVersion is missing!")
}
theParser.initParser(target, buildToolsRevision, logger)
isSdkParserInitialized = true
}
return theParser
}
3 回答
如果您使用的是实验性插件版本0.4 . 你可以设置
好吧,这是一个可怕的黑客,但它很容易 . 找到NDK,它应该在android-sdk / ndk-bundle中 . 进入平台文件夹 . 将android-21重命名为其他内容 . 制作android-19的副本,然后将android-21中的三个64位文件夹复制到其中,然后将其重命名为android-21 . Gradle将认为它使用的是android-21,它将用于64位目标,但对于32位目标,它将使用android 19 .
我不是100%确定这是安全的,但我在一些设备上测试了它,我现在有一个Beta测试应用程序 . 我认为它比为ndk-build制作gradle任务更清晰 . 每当Google修复实验性gradle时,除了重新安装ndk之外,没有什么需要改变 .