python 调用命令行编译 android apk

下面是编程之家 jb51.cc 通过网络收集整理的代码片段。

编程之家小编现在分享给大家,也给大家做个参考。

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

import os
import subprocess
import shutil

def main():
    sdk_path = os.getenv('ANDROID_HOME')
    ndk_path = os.getenv('NDK_HOME')
    standalone_path = os.getenv('NDK_STANDALONE')
    directory = build_directory(sdk_path,args.output.filestem_str,native_shared_libs)

    # // Copy the additional native libs into the libs directory.
    for name,path in native_shared_libs.items():
        shutil.copy( path,os.path.join( directory,"libs/","armeabi/",name)

    # compile android_native_app_glue.c
    cmd = os.path.join( standalone_path,"bin/","arm-linux-androideabi-gcc ")
    arg1 = os.path.join( ndk_path,"sources/","android/","native_app_glue/","android_native_app_glue.c ")
    arg2 = " -c "
    arg3 = " -o "
    arg4 = directory + "android_native_app_glue.o"

    os.system(cmd + arg1 + arg2 + arg3 + arg4)

    """
    calling gcc to link a shared object
    """
    cmd = os.path.join(standalone_path,"arm-linux-androideabi-gcc ")
    arg1 = passthrough
    arg2 = os.path.join( directory,"android_native_app_glue.o")
    arg3 = " -o " + os.path.join( directory,"libs","armeabi","libmain.so")
    arg4 = " -shared"
    arg5 = " -Wl,-E"
    os.system(cmd + arg1 + arg2 + arg3 + arg4 + arg5 )

    """
    call ant debug
    """

    ant_command = "ant debug"
    os.system(ant_command )

    #copy apk file to required dest
    shutil.copy( os.path.join( directory,"rust-android-debug.apk"),output)



def find_native_libs(args: &Args) -> HashMap<String,Path> {
    """
    args  HashMap
    """
    base_path = os.path.join( args,"native");
    native_shared_libs = {}
    #for dirpath,dirname,filenames in os.walk(base_path):

    for dirs in os.listdir(base_path):
        if os.path.isdir(dirs):
            for dir in dirs:
                path = os.path.join(base_path,dirs,dir )
                for file in os.listdir(path):
                    if file.starts_with("lib") and file.endwith(".so"):
                        native_shared_libs.update(file,path)

    return native_shared_libs


def build_directory(sdk_dir,crate_name,libs):
    """
    sdk_dir : Path
    crate_name : str
    libs: HashMap<String,Path>

    return: Tempdir

    """
    temp_dir = "android-rs-glue-rust-to-apk"
    build_directory = os.mkdir( temp_dir )

    if len(libs) > 0:
        src_path = os.path.join(temp_dir,"src/rust/glutin");
        os.mkdirs(src_path )
        java_file = open(os.path.join( src_path,"MainActivity.java"),"rw")
        java_file.write( (java_src(libs) )
        activity_name = "rust.glutin.MainActivity"
        java_file.flush()
        java_file.close()
    else:
        activity_name = "android.app.NativeActivity"

        manifest_file=os.path.join(build_directory,"AndroidManifest.xml")
        manifest_file = open( manifest_file,"rw")
        manifest_file.write(build_manifest(crate_name,activity_name))
        manifest_file.close()

        build_xml = os.path.join(build_directory,"build.xml")
        build_xml = open( build.xml,"rw")
        build_xml.write( build_build_xml() )
        build_xml.close()

        local_pro = os.path.join(build_directory,"local.properties")
        local_pro  = open(local_pro,"rw")
        local_pro.write(build_local_properties())
        local_pro.close()

        project_pro = os.path.join(build_directory,"project.properties")
        project_pro = open(project_pro,"rw")
        project_pro.write(build_project_properties())
        project_pro.close()

        libs_path = os.path.join(build_directory,"armeabi")
        os.makedirs(libs_path)

        return build_directory

def java_src(libs) {
    """
    libs: HashMap,returns string
    """
    libs_string = ""

    for  name,_ in libs.items():
        """
        // Strip off the 'lib' prefix and ".so" suffix. This is safe since libs only get added
        // to the hash map if they start with lib.
        """
        line = "        System.loadLibrary(\"{}\");\n".format( name[3:len(name)-3]);
        libs_string = libs_string + line;

    ret = """package rust.glutin;

    public class MainActivity extends android.app.NativeActivity {{
        static {{
            {0}
        }}
    }}""".format(libs_string)
    return ret



def  build_manifest(crate_name,activity_name):
        return """<?xml version="1.0" encoding="utf-8"?>
<!-- BEGIN_INCLUDE(manifest) -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.native_activity"
        android:versionCode="1"
        android:versionName="1.0">

    <uses-sdk android:minSdkVersion="9" />

    <uses-feature android:glEsVersion="0x00020000" android:required="true"></uses-feature>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application android:label="{0}">
        <activity android:name="{1}"
                android:label="{0}"
                android:configChanges="orientation|keyboardHidden">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
<!-- END_INCLUDE(manifest) -->
""".format(crate_name,activity_name)

def build_build_xml():
        return """<?xml version="1.0" encoding="UTF-8"?>
<project name="rust-android" default="help">
    <property file="local.properties" />
    <loadproperties srcFile="project.properties" />
    <import file="custom_rules.xml" optional="true" />
    <import file="${{sdk.dir}}/tools/ant/build.xml" />
</project>
"""


def build_local_properties(sdk_dir):
    return "sdk.dir={0}".format(os.path.abspath(sdk_dir))

def build_project_properties():
    return "target=android-19"

main()

以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Centos系统之Shell编程基础知识
从Export理解Shell环境和变量生存期
linux shell数组变量、类型及规则
Centos编程Shell基本工作原理方案
Centos操作系统编程之Shell 问答录
rsync-linux备份脚本
Linux Shell编程入门 1-4
用shc加密shell脚本
centos每天自动备份mysql数据库
shell字符串处理
awk&#160;用法:awk&#160;&#39;&#160;pattern&#160;{action}&#160;&#39; 变量名 含义 ARGC 命令行变元个数 ARGV 命令行变元数组 FI
sed之仅打印相邻重复的行 cat file aaa bbb bbb ccc ddd eee eee fff 只显示重复的行: bbb bbb eee eee sed -n &#39;:a;N;/\(
压缩: tar -zcvf 压缩后文件名.tar.gz 被压缩文件 解压: tar -zxvf 被解压文件 注意:不要有多余的空格,一个空格即可。 具体的可以在linux环境下 用 tar --hel
sed命令行格式为: sed [-nefri] ‘command’ 输入文本/文件 常用选项: -n∶取消默认的输出,使用安静(silent)模式。在一般 sed 的用法中,所有来自 STDIN的资料
#假设文件名是:fortest.gtfdeclare -i fileLinesfileLines=`sed -n &#39;$=&#39; fortest.gtf`echo $fileLines#--
获得每行的最后一个逗号后边的内容.例如:KIAA1967 KIAA1967, xxxxSECIS biding proin 2-like, SECISBP2L, yyyy 1234ankyrin re
bash 正则表达式匹配,一行文本中 “包含 ABC” 并且 “不包含 XYZ”A文件: XXXX ABC XXX4444444444444444XXXX ABC XXX XYZ66666666666
shell/bash 让vi/vim显示空格,及tab字符Vim 可以用高亮显示空格和TAB。文件中有 TAB 键的时候,你是看不见的。要把它显示出来::set listTAB 键显示为 ^I, $显
输出到文件log中,并在屏幕上显示:#ls &gt;&amp;1 | tee log追加输出到文件log中,并在屏幕上显示:#ls &gt;&amp;1 | tee -a log
Suppose we have a file contains the following information, termed input_file:A 0B 1C 21.Read file on