Current location - Quotes Website - Personality signature - How to build an android development environment
How to build an android development environment
1. Understand the architecture of android.

The essence of Android is to add the JAVA virtual machine Dalvik to the standard Linux system and build a JAVA application framework on the Dalvik virtual machine. All applications are based on the Java application framework.

Android is divided into four layers, namely, application layer, application framework layer, system running layer and linux core layer from top to bottom.

Step 2 create an environment

Build a development environment

The most painful thing for domestic developers is that they can't access the android development website. In order to better understand the world, it is also a technology for programmers to take you over the wall to appreciate the world outside the wall. All right, cut the crap. Domestic developers have all the resources you want on androiddevtools, which can be downloaded to our protagonist framework.

However, this structure can only read the source code and cannot further realize its own rom. We saw the hammer system re-implement the framework code in the early open rom. Now it seems that he succeeded, so we need to build the source code compilation environment for android system.

Build a source code compilation environment

Three. Opening theme

Write a C program with a running entry at the beginning, such as

# include & ltiostream & gt

# include & ltcmath & gt

# include & lt algorithm & gt

Use namespace std

//This is mainly the entrance of the application.

int main(int argc,const char * argv[]){

Returns 0;

}

In the principle of computer network, we use socket to realize a server, which constantly answers the client's access. His code implementation is as follows:

# include & ltwinsock2.h & gt

#pragma comment (lib, "WS2_32.lib")

# include & ltstdio.h & gt

void main()

{

WORD wVersionRequested// version number

WSADATA wsaData

int err

wVersionRequested = MAKEWORD(2,2); //Version 2.2 Sockets

//Load socket library, and return if it fails.

err = wsa startup(wVersionRequested,& ampwsa data);

If (ugh! = 0)

{

Return;

}

//Judge whether the high and low bytes are 2, and if they are not version 2.2, exit.

if (LOBYTE(wsaData.wVersion)! = 2 ||

HIBYTE(wsaData.wVersion)! = 2)

{

Return;

}

//Create a TCP-based stream socket (SOCK_STREAM).

SOCKET socSrv = socket(AF_INET,SOCK_STREAM,0);

//Create//Socket Address Structure

SOCKADDR _ IN addrSrv

addrSrv.sin_addr。 S_un。 s _ addr = htonl(in addr _ ANY); //Converts an unsigned long integer into a network byte order lattice.

AddrSrv.sin _ family = AF _ INET///Specify the address cluster.

addr SRV . sin _ port = htons(6000);

//Specify the port number. Except for the sin_family parameter, all other parameters are in network byte order, so they need to be converted.

//Bind the socket to the port number and local address.

bind(socSrv,(SOCKADDR *)& amp; addrSrv,sizeof(SOCKADDR)); //sizeof must be used, but strlen can't.

Listen (socSrv, 5);

The word SOCKADDR _ IN addrClient// meaning is used to receive the structure of the client socket.

int len = sizeof(SOCKADDR); //Initialize parameters, which must be initialized, sizeof.

//Loop waiting to accept the request sent by the client.

while ( 1)

{

//Wait for the customer's request; When the request comes, accept the connection request,

//Returns the new socket corresponding to this connection.

//The program is blocked here at this time.

SOCKET sockConn = accept(socSrv,(SOCKADDR *)& amp; addr client & amp; len);

char send buf[ 100];

Sprintf(sendBuf, "Welcome %s to JoyChou",

inet _ ntoa(addr client . sin _ addr)); //print format

//Use the returned socket to communicate with the client.

send(sockConn,sendBuf,strlen(sendBuf)+ 1,0); //Send another byte.

//receiving data

char recvBuf[ 100];

recv(sockConn,recvBuf, 100,0);

printf("%s\\n ",recvBuf);

close socket(sock conn);

}

}

He uses a while infinite loop to listen to customers' requests.

The first is the source code.

Public Final Class Activity Thread (

Public static void main(String[] args) {

samplingprofilerintegration . start();

close guard . set enabled(false);

environment . initforcurrentuser();

event logger . set reporter(new EventLoggingReporter());

security . add provider(new AndroidKeyStoreProvider());

The final file configdir = environment.getuserconfigdirect (userhandle.myuserid ());

trustedcertificatestore . set defaultuser directory(configDir);

process . setargv 0(" & lt; Pre-initialization >);

looper . prepare main looper();

//You can see that a thread has been opened for the app and entered the looper.

activity thread thread = new activity thread();

thread . attach(false);

if (sMainThreadHandler == null) {

sMainThreadHandler = thread . gethandler();

}

async task . init();

if (false) {

Looper.myLooper()。 SetMessageLogging (new

Log printer (log. DEBUG," activity thread "));

}

looper . loop();

Throw a new RuntimeException ("main thread loop unexpectedly exits");

}

}

I was disappointed to see the source code. There is no while loop. In fact, it is achieved by other methods.

//Use the loop mechanism to loop and listen for the response.

looper . prepare main looper();

looper . loop();

Dig into the code

Public static void loop () {

Finally looper me = mylooper ();

if (me == null) {

Throw new runtime exception ("nolooper; Looper.prepare () is not called on this thread. );

}

final message queue queue = me . mqueue;

binder . clearcallingidentity();

final long ident = binder . clearcallingidentity();

//I see a circular listening message here.

for(; ; ) {

message msg = queue . next(); //may block

if (msg == null) {

//No message indicates that the message queue is exiting.

Return;

}

Printer log = me.mLogging

If (logging! = null) {

logging . println(" & gt; & gt& gt& gt& gt is assigned to "+msg.target+""+

msg . callback+":"+msg . what);

}

msg . target . dispatch message(msg);

If (logging! = null) {

logging . println(" & lt; & lt& lt& lt& lt complete to "+msg.target+"+msg.callback);

}

//Ensure that during the scheduling process

//The identity of the thread is not damaged.

final long new ident = binder . clearcallingidentity();

If (ident! = newIdent) {

Log.wtf (tag, "Thread ID changed from 0x"

+Long.toHexString(ident)+"to 0x"

+Long.toHexString(newIdent)+"When assigned to"

+ msg.target.getClass()。 getName() +" "

+msg . callback+" what = "+msg . what);

}

msg . recycle checked();

}

}