This article is a detailed analysis of the method of referencing another Android project in Android. Need a friend to reference under
Now there's an Android project A. We wanted to expand the functionality of a, but did not want to develop on a basis, so we created another Android project B to refer to a in B.
One idea is to make project a a pure jar package so that other projects can be referenced directly.
However, if the reference in R.java is used in project A, it cannot be called a jar package. The reason is that R.java is generated automatically and is dynamic, and each compilation is not the same. If you must make a jar package, you can not use the auto-generated R file, use the resources to write code to get.
Another idea is to make project a into Android library project.
Set up project A, right-->properties->android, select the IS library item, and apply. Set Project B, right-->properties->android, in the library, click the Add button to add a project, and then apply. The resources and code in A are introduced in B at this point, and these resources and code can be called directly. It is important to note that because a is no longer a complete Android application, it is a class library project, so there are some things that need to be configured in B. For example, a in the Lib library reference, B will also be added to the Lib library, such as the contents of the Androidmanifest.xml file in a, in the B androidmanifest.xml file should also be added ...
If you do not need to reference a project's resource files, you just have to get the jar file, set up project A, right-click->properties->android, select the IS library item, and apply. A jar file can be obtained from the bin directory of the A project, which is directly referenced in the Libs directory in Project B. If you can use the jar of course the best, but the jar file can not be the Res directory resources in the package, so the Lib project appears.
Create and use Android Library project
Summary: To create a library for multiple projects to share code, resources are very common needs, online this kind of information is very small, basically speaking of creating Java project, and then export, this way drawback is very much, we can Google a bit. This article highlights how to create an Android library and ... To create a library for multiple projects to share code, resources are very common needs, online this kind of information is very small, basically speaking of creating Java project, and then export, this way disadvantage is very much, we can Google a bit.
This article focuses on how to create an Android library and use the resources provided by this library in your project, in the following steps:
1. Create an Android project named Mylib
2. Enter project Settings Select is Library
3. Create another Android project, named MyProj
4. Enter project settings and add Mylib
5. Add a reference to the activity in the library in the androidmanifest.xml of MyProj
<activity android:name= "Net.devdiv.mylib.MyLib"/>
6. Because the resources in the library and the project resources referencing it are merged together, the resources in the library need to be renamed in order to avoid the problem of duplicate names
1). Change the Main.xml to Mylib.xml and modify the Mylib.java code Setcontentview (r.layout.mylib);
2). Strings.xml modified to
<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
<string name= "Mylibhello" >string fetched from lib!</string>
<string name= "Mylib_app_name" >MyLib</string>
</resources>
7. Referencing Mylib resources in MyProj
Package net.devdiv.myproj;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.widget.TextView;
import net.devdiv.mylib.*;
Import android.content.Intent;
public class MyProj extends activity {
/** called when the activity is first created. */
&NBSP ; @Override
public void onCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
setcontentview (R.layout.main);
textview TV = (TextView) Findviewbyid (R.id.myprojtext);
tv.settext (R.string.mylibhello);
,
intent it = New Intent (this, mylib.class);
startactivity (IT);
}
}