Gradle:独立于SDK构建目标

时间:2015-07-14 10:14:08

标签: android android-studio gradle android-ndk android-gradle

过去我使用eclipse进行NDK项目,Android.mk文件非常适合用API级别9编译NDK,同时让应用程序(SDK)在API级别22上编译。但似乎这是不可能的在使用Android Studio 1.3 RC1的实验性Gradle构建系统(2.5)时。

如何在API级别9上仅编译NDK?

我的典型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
}

4 个答案:

答案 0 :(得分:3)

如果您使用的是实验性插件版本0.4。你可以设置

android.ndk {
   platformVersion = "19"
}

答案 1 :(得分:1)

好的,这是一个可怕的黑客,但它很容易。找到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之外,没有什么需要改变。

答案 2 :(得分:-1)

model {
    android {
        compileSdkVersion = 22
         buildToolsVersion '20'


        defaultConfig.with {
            applicationId = "com.example"
            minSdkVersion.apiLevel = 9
            targetSdkVersion.apiLevel = 22
            versionCode = 1
            versionName = "1.0"
        }
    }

答案 3 :(得分:-1)

渐变版本为3.1.2

defaultConfig {
    applicationId "com.xxx.player"
    minSdkVersion 19
    targetSdkVersion 28
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    externalNativeBuild {
        cmake {
            cppFlags ""
            abiFilters 'armeabi-v7a'
            arguments "-DANDROID_PLATFORM=android-21" //config ANDROID_PLATFORM version on here
        }
    }
}