Some Android Basics

to play

Posted by pic4xiu on August 10, 2020

Environment construction

#编译环境
sudo apt install gcc-arm-linux-gnueabi

#愉快编译
arm-linux-gnueabi-gcc test.c -o test

#运行环境
sudo apt install qemu qemu-kvm qemu-system-arm

#愉快使用qemu跑起来
qemu-arm ./test

#root@ubuntu:~/Desktop# qemu-arm test
#/lib/ld-linux.so.3: No such file or directory
#出现这种问题跑一下
sudo cp  /usr/arm-linux-gnueabi/lib/ld-linux.so.3 /lib
sudo cp /usr/arm-linux-gnueabi/lib/libgcc_s.so.1 /lib
sudo cp /usr/arm-linux-gnueabi/lib/libc.so.6 /lib

#调试环境
sudo apt install gdb-arm-none-eabi 

#开启remote调试
qemu-arm -g 1234 test

(gdb) target remote localhost:1234
Remote debugging using localhost:1234
warning: A handler for the OS ABI "GNU/Linux" is not built into this configuration
of GDB.  Attempting to continue with the default armv5t settings.

0xf67ceb00 in ?? ()
(gdb) b main
Breakpoint 1 at 0x844c
(gdb) r
The "remote" target does not support "run".  Try "help target" or "continue".
(gdb) c
Continuing.

Breakpoint 1, 0x0000844c in main ()
(gdb) dis
disable      disassemble  disconnect   display      
(gdb) disassemble 
Dump of assembler code for function main:
   0x00008444 <+0>:	push	{r11, lr}
   0x00008448 <+4>:	add	r11, sp, #4
=> 0x0000844c <+8>:	ldr	r0, [pc, #12]	; 0x8460 <main+28>
   0x00008450 <+12>:	bl	0x82e0 <printf@plt>
   0x00008454 <+16>:	mov	r3, #0
   0x00008458 <+20>:	mov	r0, r3
   0x0000845c <+24>:	pop	{r11, pc}
   0x00008460 <+28>:	ldrdeq	r8, [r0], -r8	; <UNPREDICTABLE>
End of assembler dump.

A small example of jni

首先就是java文件(用于调用)和c文件(用于被调用)

public  class NativeDemo {
	{
		System.load("C:\\Users\\14555\\Desktop\\test\\sayHello.dll");//dll文件绝对路径
	}
	public native void sayHello();
	
	
	public static void main(String[] args) {
		new NativeDemo().sayHello();
	}
}

#include "NativeDemo.h"
#include <windows.h>
#include <stdio.h>

JNIEXPORT void JNICALL Java_NativeDemo_sayHello(JNIEnv *, jobject){
	using namespace std;
	printf("hello!jniworld");
} 

其中NativeDemo.h是根据javac -h -jni NativeDemo.java自动生成,但这得注意把生成的#include <jni.h>改成#include "jni.h"然后把需要的文件拷到当前目录(最简单的方式)

dll文件生成就是这样,比较简单 g++ -m64 -shared hello.cpp -o sayHello.dll

之后就是java文件编译运行了

C:\Users\14555\Desktop\test
λ javac NativeDemo.java

C:\Users\14555\Desktop\test
λ java NativeDemo
hello!jniworld