如何将c程序执行文件打包入APK中,两种方法

如何将一个可执行的文件打包到APK中,用户在下载APK后直接使用,我现在想到了两种方法:一种是将C程序直接打包到APK中,这种适合于独立执行程序或者你手上没有源码;另一种是将C程序作为库然后使用JNI来调用C程序;


解压缩即可看到APK如下目录:


|-AndroidManifest.xml

|-res

|-assets/

|-META-INF

|-class.dex

|-resource.arsc


1. 方法一

我们可以在/assets 和 /res/raw 中放置一些费标准的文件,但是需要通过输入流来访问。当用户将APK文件安装后,可以将非标准指定目录下的assets和res/raw下的可执行文件复制到到data目录下(注意:每一个APK在安装后都会在系统创建一个” /data/data/com.报名/ “ 此目录属于此APK的私有目录,然后你就可以在此目录中添加删除文件。有人说就象是linux home目录一样,的确很形象)。    下面是一个实现文件复制的代码,还有注意的地方是如果你的是可执行文件记得将此文件设置成可执行权限的。SDK API 9 以后有 File.Executable(true, true); API 8 及以下都不可使用此方法。


    public static boolean copyStream(InputStream in, OutputStream out) {
        Log.d(TAG, "copyStream("+ in + " ," + out+ ")");
        try {
            byte[] buf = new byte[8192];
            int len;
            while ((len = in.read(buf)) > 0)
               out.write(buf, 0, len); 
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
     }
	
    public static String getAppPrivateDir(Context ctx) {

        File dataDir = ctx.getDir("data", Context.MODE_PRIVATE);
        return dataDir.getAbsolutePath();

    }

    public static boolean copyAsset(Context ctx, String assetFile,
            String saveToFile) {

        Log.d(TAG, "copyAssets(" + assetFile + " -> " + saveToFile);
        File outputFile = new File(saveToFile);

        if (outputFile.exists()) {
            return true;
        }

        // init output stream(file)

        OutputStream out;

        try {
            out = new FileOutputStream(outputFile);
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
            return false;
        }

        boolean copyResult = false;

        InputStream in = null;

        try {
            in = ctx.getAssets().open(assetFile);
            copyResult = copyStream(in, out);
            Log.d(TAG, "copy " + assetFile + " - " + copyResult);
        } catch (IOException e1) {
            e1.printStackTrace();
        } finally {
            // close input stream(file)
            try {
                if (in != null)
                    in.close();
            } catch (IOException e) {

            }
        }
        // close output stream (file)

        try {
            if (out != null)
                out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // return copy result

        // add file execute permission
        File fs = new File(saveToFile);
        try {
            fs.setExecutable(true, true);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Log.d(TAG, "copyAsset() return " + copyResult);
        return copyResult;

    }

字符串处理优化方法

		StringBuilder sb = new StringBuilder();
		sb.append(getAppPrivateDir(this)).append("/server");



2. 方法二

将C程序作为库,在java层通过JNI来调用,此处不做细说。




  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要将Python程序打包APK文件,你需要使用Kivy或Buildozer这样的工具。 Kivy是一个用于创建跨平台应用程序的Python框架。它可以帮助你构建具有动态用户界面和复杂逻辑的应用程序。Buildozer是一个基于Python的命令行工具,可以将Python应用程序打包为Android APK文件。 以下是将Python程序打包APK文件的步骤: 1. 安装Kivy和Buildozer ``` pip install kivy pip install buildozer ``` 2. 创建一个Kivy应用程序 创建一个简单的Kivy应用程序,并确保它能够在你的本地计算机上运行。你可以使用Kivy的官方文档来学习如何创建一个Kivy应用程序。例如,下面是一个简单的Kivy应用程序: ``` from kivy.app import App from kivy.uix.button import Button class TestApp(App): def build(self): return Button(text='Hello World') TestApp().run() ``` 3. 创建一个buildozer.spec文件 在你的Kivy应用程序的根目录创建一个名为buildozer.spec的文件。该文件包含了应用程序的元数据和其他配置信息。你可以在Buildozer的官方文档查看所有可用选项。以下是一个简单的buildozer.spec文件示例: ``` [app] # (str) Title of your application title = My Awesome App # (str) Package name package.name = myapp # (str) Package domain (needed for android/ios packaging) package.domain = com.example.myapp # (str) Source code where the main.py live source.dir = . # (list) Source files to include (let empty to include all the files) source.include_exts = py,png,jpg,kv,atlas # (list) Application requirements requirements = kivy # (str) Your application version version = 0.1 # (int) Minimum API required android.minapi = 21 # (int) Android SDK version to use android.sdk = 28 # (str) Android NDK version to use android.ndk = 19b # (list) Permissions android.permissions = INTERNET # (list) Services android.services = # (str) Path to the default icon of the application icon.filename = icon.png # (str) Path to the background image of the application #background.filename = background.png # (str) Path to the presplash image of the application #presplash.filename = presplash.png # (str) Path to the splash image of the application #splash.filename = splash.png # (list) Opaque binary blobs (e.g. OpenGL ES 2 shaders) #p4a.opaque_binaries = # (list) Java classes to add to the compilation classpath #android.add_jars = foo.jar,bar.jar # (list) Java files to add to the android project #android.add_src = # (list) AAR libraries to add to the Android project #android.add_aars = # (list) Gradle dependencies to add #android.gradle_dependencies = # (bool) Indicate whether the app should be fullscreen or not fullscreen = 0 ``` 请注意,你需要将title、package.name和package.domain等值替换为你自己的应用程序元数据。 4. 创建一个Android虚拟环境 在你的本地计算机上创建一个Android虚拟环境。你可以使用Android Studio来创建一个虚拟设备并安装必要的SDK和NDK版本。 5. 打包应用程序 在你的应用程序根目录运行以下命令: ``` buildozer android debug ``` 这将使用Buildozer工具来打包你的应用程序并生成一个名为myapp-0.1-debug.apkAPK文件。请注意,这可能需要一段时间才能完成,并且你需要确保你的Android虚拟环境已经启动。 6. 在Android设备上安装应用程序 将生成的APK文件复制到你的Android设备上,并在设备上安装它。 以上就是将Python程序打包APK文件的步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值