Android Gradle Plugin 1.1.0 - getNdkFolder() not found anymore, any replacement?

老子叫甜甜 提交于 2019-11-28 10:04:41

You can get it like this:

plugins.getPlugin('com.android.library').sdkHandler.getNdkFolder()

The correct way - android.ndkDirectory

The change from android.plugin.ndkFolder to android.ndkDirectory was documented in the technical doc Migrating Gradle Projects to version 1.0.0. In addition you should update your android plugin to 2.3.0.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.0'
    }
}

The sdkFolder is internal and will likely stop working just like ndkFolder stopped working.

Regarding experimental branches

Search around, the plugin is mature enough you'll have trouble finding reasons to use experimental branches with the NDK.

There is some discussion about an Experimental version of the plugin breaking ndkDirectory which is the documented way to access this value after 1.1. If that changes in a released version I'll update my answer, in the mean time if running experimental plugins is your thing you can hack your way around this bug/feature using @Alex Cohn's answer.

I am using gradle 2.2.3 version. The think that worked for me:

def ndkDir = project.android.ndkDirectory

Ref. Link: https://code.google.com/p/android/issues/detail?id=152810

If in the build.gradle file you can probably use this now that the bug referenced has been marked fixed.

project.android.ndkDirectory 

If you're in a custom Task or Plugin you'll want to use this

    def androidPluginExtension = project.getExtensions().getByName("android");
    // Use this for easily viewing the properties available in the extension.
    androidPluginExtension.properties.each { Object key, Object value ->
        logger.info("Extensionprop: ${key} ${value}")
    }
    String ndkDir = androidPluginExtension.getProperties().get("ndkDirectory");
    System.out.println("Using ndk dir: ${ndkDir}");

But remember that the Android Plugin would have had to have been initialized, so this will always work in a Task's execute method, but it may not work if Gradle hasn't parsed the Android DSL yet during the Gradle Configuration phase.

As Michael Pardo mentioned, you can get it by *

plugins.getPlugin('com.android.library').sdkHandler.getNdkFolder()

*

, In case you receive the following error:

Error:(9, 0) Plugin with id com.android.library has not been used.

You can use the applied plugin : 'com.android.application', therefore the final code will be :

def ndkDir = plugins.getPlugin('com.android.application').sdkHandler.ndkFolder
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!