The Android Studio project is packaged into jar

Keywords: Android Gradle Eclipse xml

The Android Studio project is packaged into jar

 

Foreword: In eclipse, we know how to export a project as a jar package. Now Android Stuido is widely developed. Here we introduce the use of AS project packaged as jar, jar and arr step by step.

 

1. Function: To be used by other projects, pack the project into jar

 

Step 2 (Android Studio):

1. Create a new project as usual (step omitted)

2. (Based on Step 1) Click File - > New - > New Module - > Select Android Library - > Click Next (as follows:)

Define Library's project name: librarydemo (as follows:)

Creation completed:

The Model (build. gradle under app) will be automatically introduced into the project (built in step 1):

3. Generate jar:

Before I created it, I built one in the librarydemo project. test Class to facilitate test calls:

Add in build.gradle under the librarydemo project:

Do the following:

(Note: Your as has never generated jar, so the first time it will be very slow, will download some files)

See if the jar was successfully generated:

3. Using jar and arr:

In Android Studio, under the Model project created:

The difference between the two is as follows:

jar: Only class files and manifest files are included, not resource files, such as all res files such as pictures.

aar: Contains jar packages and resource files, such as files in all res, such as images.

Personally, I think it's better to use aar package provided by as to generate aar after compiling, without worrying about resources.

1. aar uses:

Import aar (like jar):

Add the following in build.gradle under app:

Sync Now, look

Resource files and layout files

2. jar import:

Import jar (like arr) - > select Jar - > right-click - > Click Add As Library - > Click Ok

no resource file

 

Some people wonder how to put resources (pictures, layouts, string s, etc.) into jar packages?

For example: jar has an activity that uses layout files and image resources, so what?

Solutions are as follows: Since the packaged jar only contains source code. class files and does not contain resource files, we will put the resources used in the jar package into your use.

It's in the jar project. Then it can be done by reflection. The reflection class is given here.

public class MResource {

    public static int getIdByName(Context context, String className, String resName) {
        String packageName = context.getPackageName();
        int id = 0;
        try {
            Class r = Class.forName(packageName + ".R");
            Class[] classes = r.getClasses();
            Class desireClass = null;
            for (Class cls : classes) {
                if (cls.getName().split("\\$")[1].equals(className)) {
                    desireClass = cls;
                    break;
                }
            }
            if (desireClass != null) {
                id = desireClass.getField(resName).getInt(desireClass);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return id;
    }
}

Here is an example of opening activity in jar in a project

Here is the code for activity in jar:

public class JarActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(MResource.getIdByName(this, "layout", "jar_layout"));
        ImageView mPlayerLogo = (ImageView) this.findViewById(MResource.getIdByName(this,
                "id", "logo"));
        mPlayerLogo.setImageResource(MResource.getIdByName(this, "drawable", "ic_launcher"));
    }
}

Here: Use reflection to get resource ID loading layout + settings based on resource name

The implementation code for opening jar's JarActivity in the project is as follows:

Note: Since jar does not have layout files and resource files, it is necessary to copy the layout (jar_layout) into the project, while aar does not.

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.btn_jar).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClassName(getApplication(), "com.zhh.librarydemo.JarActivity");
                startActivity(intent);
            }
        });
    }
}

jar_layout layout layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/logo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

Register in the manifest file:

 <activity android:name="com.zhh.librarydemo.JarActivity"/>

 

Document download...

Posted by Moharo on Tue, 09 Jul 2019 18:38:31 -0700