1. 程式人生 > >Cordova生成安卓原始碼無法執行

Cordova生成安卓原始碼無法執行

說明: cordova編譯生成的android原始碼在Android Studio中執行, 無法找到

Gradle 'android' project refresh failed
Error:No installed build tools found. Install the Android build tools version 19.1.0 or higher.

解決方式一:

  1. 全域性搜尋 availableBuildToolsVersions = getAvailableBuildTools()
    修改前的照片
    修改為
availableBuildToolsVersions  = ['21.0.0','20.0.0','19.1.0','19.0.3'] as String[]

修改後的原始碼截圖
分析:


String doFindLatestInstalledBuildTools(String minBuildToolsVersion) {
    def availableBuildToolsVersions
    try {
    	// TODO: 獲得可用的 build-tools
        availableBuildToolsVersions  = getAvailableBuildTools()
    } catch (e) {
        println "An exception occurred while trying to find the Android build tools."
        throw e
    }
    if (availableBuildToolsVersions.length > 0) {
        def highestBuildToolsVersion = availableBuildToolsVersions[0]
        if (compareVersions(highestBuildToolsVersion, minBuildToolsVersion) < 0) {
            throw new RuntimeException(
                "No usable Android build tools found. Highest installed version is " +
                highestBuildToolsVersion + "; minimum version required is " +
                minBuildToolsVersion + ".")
        }
        highestBuildToolsVersion
    } else {
    	// TODO 上面報錯正是這丟擲的異常, 為什麼會走這呢, 因為沒找到build-tools的版本
        throw new RuntimeException(
            "No installed build tools found. Install the Android build tools version " +
            minBuildToolsVersion + " or higher.")
    }
}

/**
 * 獲取可用的build-tool
 * @return
 */
String[] getAvailableBuildTools() {
    def buildToolsDir = new File(getAndroidSdkDir(), "build-tools")
    buildToolsDir.list()
        .findAll { it ==~ /[0-9.]+/ } 
        .sort { a, b -> compareVersions(b, a) }
}

/**
 * 獲取SDK位置
 * @return
 */
String getAndroidSdkDir() {
    def rootDir = project.rootDir
    def androidSdkDir = null
    String envVar = System.getenv("ANDROID_HOME")
    def localProperties = new File(rootDir, 'local.properties')
    String systemProperty = System.getProperty("android.home")
    if (envVar != null) {
        androidSdkDir = envVar
    } else if (localProperties.exists()) {
        Properties properties = new Properties()
        localProperties.withInputStream { instr ->
            properties.load(instr)
        }
        def sdkDirProp = properties.getProperty('sdk.dir')
        if (sdkDirProp != null) {
            androidSdkDir = sdkDirProp
        } else {
            sdkDirProp = properties.getProperty('android.dir')
            if (sdkDirProp != null) {
                androidSdkDir = (new File(rootDir, sdkDirProp)).getAbsolutePath()
            }
        }
    }
    if (androidSdkDir == null && systemProperty != null) {
        androidSdkDir = systemProperty
    }
    if (androidSdkDir == null) {
        throw new RuntimeException(
            "Unable to determine Android SDK directory.")
    }
    androidSdkDir
}