This blog is based on Eclipse 3.6.2, JDK 1.6, Android
2.1 practice.
1. Create a Java Project
Code
package com.zhang;public class Party {private String name;public void setName(String name) {this.name = name;}public String getName() {return name;}public String startParty() {return name + " starts happy!";}}
2. Export JAR File
Right-click the Java project name and select Export
Next
Directly click Next to finish. The highparty. jar file is generated under/home/mark/desktop.
3. Create an android Project
4. Import jar files
Create a new folder named libs in the veryparty project.
Copy the highparty. jar file to the libs directory.
Right-click the veryparty project and choose build path/Add External Archives/
Then select libs/highparty. Jar
Last Android code:
package com.net.veryplay;import com.zhang.Party;import android.os.Bundle;import android.app.Activity;import android.widget.TextView;public class MainActivity extends Activity {private TextView tv;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);setupViews();}@Overrideprotected void onResume() {super.onResume();try {Class.forName("com.zhang.Party");Party party = new Party();party.setName("mark");tv.setText(party.startParty());} catch (ClassNotFoundException e) {tv.setText("Oops... no content!");}}private void setupViews() {tv = (TextView) findViewById(R.id.tv);}}
5. Run
The APK generated after eclipse compilation contains the JAR file code.
You can still see the JAR file code through decompilation.
Finally, we would like to remind the general public that:
InStep 4The folder name is libs. Do not name it lib or another name. Otherwise, it will be difficult to add a jar file.
If you don't believe it, you can try it! (The following errors may occur)
If you have any questions, leave a message!