How to Use jar files for eclipse-based Android Projects

Source: Internet
Author: User

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!




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.