内容简介:Ever wanted to write C code and run it on Android? Sick of multi-megabyte packages just to do the most basic of things. Well, this is a demo of how to make your own APKs and build, install and automatically run them in about 2 seconds, and with an apk size
rawdrawandroid
Ever wanted to write C code and run it on Android? Sick of multi-megabyte packages just to do the most basic of things. Well, this is a demo of how to make your own APKs and build, install and automatically run them in about 2 seconds, and with an apk size of about 25kB.
With this framework you get a demo which has:
AAssetManager
Youtube Video:
DISCLAIMER: I take no warranty or responsibility for this code. Use at your own risk. I've never released an app on the app store, so there may be some fundamental issue with using this toolset to make commercial apps!
For support, you can try chatting with folks on my discord: https://discord.com/invite/ZUWdwXk
Why?
Becaue sometimes you want to do things that don't fit into the normal way of doing it and all the discussions online revolve around doing it with all the normal processes. And those processes change, making it difficult to keep up and do specific things. By using Makefile
s it's easy to see what exact commands are executed and add custom rules and tweak your build. C is a universal language. Rawdraw operates on everything from an ESP8266, to RaspberryPi, Windows Linux and now, even Android. Write code once, use it everywhere.
When you don't fill your build process with hills of beans, you end up being left with the parts that are important, and not the frivilous parts. This makes it easier to develop, deploy, etc, because everything takes much less time.
A little bit of this also has to do to stick it to all those luddites people on the internet who post "that's impossible" or "you're doing it wrong" to Stack Overflow questions... Requesting permissions in the JNI "oh you have to do that in Java" or other dumb stuff like that. I am completely uninterested in your opinions of what is or is not possible. This is computer science. There aren't restrictions. I can do anything I want. It's just bits. You don't own me.
P.S. If you want a bunch of examples of how to do a ton of things in C on Android that you "need" java for, scroll to the bottom of this file: https://github.com/cntools/rawdraw/blob/master/CNFGEGLDriver.c - it shows how to use the JNI to marshall a ton of stuff to/from the Android API without needing to jump back into Java/Kotlin land.
Development Environment
Most of the testing was done on Linux, however @AEFeinstein has done at least cursory testing in Windows. You still need some components of Android studio set up to use this, so it's generally easier to just install Android studio completely, but there are instructions on sort of how to do it piecemeal for Windows.
Linux install Android Studio with NDK.
This set of steps describes how to install Android Studio with NDK support in Linux. It uses the graphical installer and installs a lot more stuff than the instructions below. You may be able to mix-and-match these two sets of instructions. For instance if you are on Linux but don't want to sacrifice 6 GB of disk to the Googs.
- Install prerequisites:
# sudo apt install openjdk-11-jdk-headless adb
- Download Android Studio: https://developer.android.com/studio
- Start 'studio.sh' in android-studio/bin
- Let it install the SDK.
- Go to sdkmanager ("Configure" button in bottom right)
- Probably want to use Android 24, so select that from the list.
- Select "SDK Tools" -> "NDK (Side-by-side)"
- Download this repo
# git clone https://github.com/cnlohr/rawdrawandroid --recurse-submodules
- Turn on developer mode on your phone (will vary depending on android version)
- Make your own key
make keystore
- Go into developer options on your phone and enable "USB debugging" make sure to select always allow.
- Plug your phone into the computer.
- Run your program.
make push run
Steps for GUI-less install (Windows, WSL)
If you're developing in Windows Subsystem for Linux (WSL), follow the "Steps for GUI-less install" to install the Android components from the command line, without any GUI components.
Extra note for actually deploying to device in Windows
In order to push the APK to your phone, you need adb
installed in Windows as well. You can do that by getting the full Android Studio from https://developer.android.com/studio#downloads or directly https://dl.google.com/android/repository/platform-tools_r24.0.4-windows.zip . Installing the full Android Studio is easier, but you can also get the "Command line tools only" and install adb
from there. The steps below outline how to do this with the direct link.
Rest of steps
-
Install Windows Subsystem for Linux (WSL). You can find instructions here: https://docs.microsoft.com/en-us/windows/wsl/install-win10 - we use "Ubuntu" for this.
-
Install prerequisites:
# sudo apt install openjdk-11-jdk-headless adb unzip zip
- Download "Command line tools only": https://developer.android.com/studio#downloads - you can get a URL and use
wget
in WSL to download the tools by clicking on the "Linux" toolset, then right-clicking on the accept link and saying copy link to location. Then you can saywget <link>
in WSL. - Create a folder for the Android SDK and export it. You may want to add that export to your
~/.bashrc
:
# mkdir ~/android-sdk # export ANDROID_HOME=~/android-sdk # printf "\nexport ANDROID_HOME=~/android-sdk\n" >> ~/.bashrc
- Unzip the "Command line tools only" file so that
tools
is in your brand newandroid-sdk
folder. - Install the SDK and NDK components:
# yes | $ANDROID_HOME/tools/bin/sdkmanager --sdk_root=${ANDROID_HOME} --licenses # $ANDROID_HOME/tools/bin/sdkmanager --sdk_root=${ANDROID_HOME} "build-tools;29.0.3" "cmake;3.10.2.4988404" "ndk;21.1.6352462" "patcher;v4" "platform-tools" "platforms;android-24" "tools"
- Install the Windows ADB toolset.
mkdir -p $ANDROID_HOME/windows cd $ANDROID_HOME/windows wget https://dl.google.com/android/repository/platform-tools_r24.0.4-windows.zip unzip platform-tools_r24.0.4-windows.zip export ADB=$ANDROID_HOME/windows/platform-tools/adb.exe printf "\nexport ADB=$ANDROID_HOME/windows/platform-tools/adb.exe\n" >> ~/.bashrc
- NOTE: because of updates to environment variables, you may want to close and re-open your WSL terminal.
- Download this repo
# git clone https://github.com/cnlohr/rawdrawandroid --recurse-submodules
- Turn on developer mode on your phone (will vary depending on android version)
- Go into developer options on your phone and enable "USB debugging" make sure to select always allow.
- Plug your phone into the computer.
- Make your keystore.
make keystore
- Compile and run your program.
make run
If you are going to use this
- Check out the example here: https://github.com/cnlohr/rawdrawandroidexample
- You may want to copy-and-paste this project, but, you could probably use it as a submodule. You may also want to copy-and-paste the submodule.
- You MUST override the app name. See in Makefile
APPNAME
- you should be able to include this project's makefile and override that. You must also updateAndroidManifest.xml
with whatever name and org you plan to use. That means updating all three fields. Bothandroid:name
fields and thepackage
field in the header. - If you are using permission you have to prompt for, you must both add it to your
AndroidManifest.xml
as well as check if you have it, and if not, prompt the user. See helper functions below. You can see an example of this withsound_android.c
from ColorChord. https://github.com/cnlohr/colorchord/blob/master/colorchord2/sound_android.c - Be sure to uninstall any previously installed apps which would look like this app, if you have a different build by the same name signed with another key, bad things will happen.
- You can see your log with:
adb logcat
- If your app opens and closes instantly, try seeing if there are any missing symbols:
adb logcat | grep UnsatisfiedLinkError
Helper functions
Because we are doing this entirelly in the NDK, with the JNI, we won't have the luxury of writing any Java/Kotlin code and calling it. That means all of the examples online have to be heavily marshalled. In rawdraw's EGL driver, we have many examples of how to do that. That said, you can use the following functions which get you most of the way there.
struct android_app * gapp;
int AndroidHasPermissions(const char* perm_name);
void AndroidRequestAppPermissions(const char * perm);
void AndroidDisplayKeyboard(int pShow);
int AndroidGetUnicodeChar( int keyCode, int metaState );
int android_width, android_height;
Departures from regular rawdraw.
Also, above and beyond rawdraw, you must implement the following two functions to handle when your apps is suspended or resumed.
void HandleResume();
void HandleSuspend();
In addition to that, the syntax of HandleMotion(...)
is different, in that instead of the mask
variable being a mask, it is simply updating that specific pointer.
TODO
Try a bunch of these cool priveleges, see what they all do.
- permission.ACCESS
- permission.INTERNET
- permission.HIDE_NON_SYSTEM_OVERLAY_WINDOWS
- permission.ACCESS_NETWORK_STATE
- permission.WRITE_EXTERNAL_STORAGE
- permission.READ_PHONE_STATE
- permission.GET_TASKS
- permission.REORDER_TASKS
- permission.WRITE_APN_SETTINGS
- permission.READ_SECURE_SETTINGS
- permission.READ_SETTINGS
- permission.REAL_GET_TASKS
- permission.INTERACT_ACROSS_USERS
- permission.MANAGE_USERS
- permission.INSTALL_PACKAGES
- permission.DELETE_PACKAGES
- permission.INTERACT_ACROSS_USERS_FULL
- permission.READ_MEDIA_STORAGE
- permission.WRITE_MEDIA_STORAGE
- android.permission.VR
- android.permission.INSTALL_PACKAGES
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。