Android NDK: 使用Python生成下载地址

该文章提供了一个Python脚本,用于生成AndroidNDK从r17到r25各个版本针对Linux、Windows和Darwin操作系统的下载链接。这些链接遵循特定的URL模式,脚本可验证于AndroidStudio的SDKManager。此外,还包含了一个额外的Python脚本,用于创建展示这些链接的表格。
摘要由CSDN通过智能技术生成


在这里插入图片描述

1. 目的

Android NDK 的 github wiki 中给出了部分历史版本 NDK 的下载地址,有些版本的下载地址并没有在网页中给出。实际上这些下载地址很有规律。本文给出具体的链接,以及生成这些链接的 Python 脚本。

2. NDK下载链接

ndk versionlinuxwindowsdarwin
r17https://dl.google.com/android/repository/android-ndk-r17-linux.ziphttps://dl.google.com/android/repository/android-ndk-r17-windows.ziphttps://dl.google.com/android/repository/android-ndk-r17-darwin.zip
r18https://dl.google.com/android/repository/android-ndk-r18-linux.ziphttps://dl.google.com/android/repository/android-ndk-r18-windows.ziphttps://dl.google.com/android/repository/android-ndk-r18-darwin.zip
r19https://dl.google.com/android/repository/android-ndk-r19-linux.ziphttps://dl.google.com/android/repository/android-ndk-r19-windows.ziphttps://dl.google.com/android/repository/android-ndk-r19-darwin.zip
r20https://dl.google.com/android/repository/android-ndk-r20-linux.ziphttps://dl.google.com/android/repository/android-ndk-r20-windows.ziphttps://dl.google.com/android/repository/android-ndk-r20-darwin.zip
r21https://dl.google.com/android/repository/android-ndk-r21-linux.ziphttps://dl.google.com/android/repository/android-ndk-r21-windows.ziphttps://dl.google.com/android/repository/android-ndk-r21-darwin.zip
r22https://dl.google.com/android/repository/android-ndk-r22-linux.ziphttps://dl.google.com/android/repository/android-ndk-r22-windows.ziphttps://dl.google.com/android/repository/android-ndk-r22-darwin.zip
r23https://dl.google.com/android/repository/android-ndk-r23-linux.ziphttps://dl.google.com/android/repository/android-ndk-r23-windows.ziphttps://dl.google.com/android/repository/android-ndk-r23-darwin.zip
r24https://dl.google.com/android/repository/android-ndk-r24-linux.ziphttps://dl.google.com/android/repository/android-ndk-r24-windows.ziphttps://dl.google.com/android/repository/android-ndk-r24-darwin.zip
r25https://dl.google.com/android/repository/android-ndk-r25-linux.ziphttps://dl.google.com/android/repository/android-ndk-r25-windows.ziphttps://dl.google.com/android/repository/android-ndk-r25-darwin.zip

3. 生成链接的 Python 脚本

#!/usr/bin/env python
#coding: utf-8

"""
Purpose:
    Android official website and github wiki page no longer gives ndk-r23 but only gives ndk-r23c.
    This script provide standalone download links for android ndk zip files.
    Those links can be verified with Android Studio's SDK manager.

https://github.com/android/ndk/wiki
"""

def get_ndk_url(os_name, ndk_version):
    valid_os_names = ['linux', 'windows', 'darwin']
    if os_name not in valid_os_names:
        raise AssertionError("os_name invalid! Should be one of: ", valid_os_names)

    if (ndk_version[0] != 'r'):
        raise AssertionError("ndk_version should starts with 'r'")

    # url = 'https://dl.google.com/android/repository/android-ndk-r23-linux.zip'
    url = 'https://dl.google.com/android/repository/android-ndk-{}-{}.zip'.format(ndk_version, os_name)
    return url

4. Bonus: 生成表格的 Python 脚本


class CodeWriter(object):
    def __init__(self, indent_len):
        self.lines = []
        self.indent_num = 0
        self.indent_len = indent_len

    def write(self, content):
        padding = (self.indent_len * self.indent_num) * ' '
        line = padding + content
        self.lines.append(line)

    def save(self, filename):
        with open(filename, "w") as fout:
            for line in self.lines:
                fout.write(line + "\n")

    def dump(self):
        for line in self.lines:
            print(line)

    def tab(self):
        self.indent_num += 1

    def backspace(self):
        if (self.indent_num > 0):
            self.indent_num -= 1



if __name__ == '__main__':
    #url = get_ndk_url('linux', 'r23')
    os_names = ['linux', 'windows', 'darwin']
    w = CodeWriter(4)
    head = '| ndk version |'
    sepline = '| ------ |'
    for os_name in os_names:
        head += ' {:s} |'.format(os_name)
        sepline += ' ---------- |'
    w.write(head)
    w.write(sepline)
    for ndk_version in ['r17', 'r18', 'r19', 'r20', 'r21', 'r22', 'r23', 'r24', 'r25']:
        line = "| " + ndk_version + " |"
        for os_name in os_names:
            url = get_ndk_url(os_name, ndk_version)
            line += ' <' + str(url) + '> |'
        w.write(line)
    w.dump()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值