Java – Starts an activity from JAR/Lib

Starts an activity from JAR/Lib… here is a solution to the problem.

Starts an activity from JAR/Lib

I want to create an android project, let’s say MyApplication.

MyApplication has an Activity MyActivity .java.

I want some other application let’s say NotMyApplication to start this activity without installing MyApplication on the device.

What I want to

do for this case is I want to somehow convert MyApplication to jar/lib so that I don’t need to install and I don’t need to share code.

Then import MyActivity in NotMyApplication.java and create an intent as follows:

**Intent in = new Intent(MyActivity.class,NotMyActivity.this);
startActivity(in); **

How can I go through the whole process.
Can someone direct Stepwise for android studio?

[edit].

I created a JAR file in Eclipse and copied it to the libs folder in Android Studio.
Than adding complie statements in gradle: import jar as:

Import com.example.testmylibs.MyClassToTest;

than the start activity is:

Intent in = new Intent(getApplicationContext(),com.example.testmylibs.MyClassToTest.class); Start Activity (in);

It still gives FATAL as:

android.content.ActivityNotFoundException: Unable to find explicit activity class {com.myapplication/com.example.testmylibs.MyClassToTest}; Have you declared this activity in AndroidManifest.xml?

[edit].

If I declare it locally in AndroidManifest.xml in NotMyApplication:

    **<activity android:name="com.example.testmylibs.MyClassToTest" >
    </activity>**
    

The following exception occurred:

java.lang.NoClassDefFoundError: parsing failed: Lcom/example/testmylibs/R$layout;
.
.
Reason: java.lang.ClassNotFoundException: Class ‘com.example.testmylibs.R$layout’ 😀 exPathList[[zip file’/data/app/com.myapplication-2/base.apk”] ,nativeLibraryDirectories=[/ vendor/lib64,/system/lib64]]

Solution

I found the answer in the following way

I

don’t know, but the jar file doesn’t work that way, so what I did was make a normal android project (MyApplication) that I wanted to export as lib.
Once I’m done making changes to my activity, say my activity. Go to the build.gradle of the application to make the following changes:

1) change code **"apply plugin: 'com.android.application'" to "apply plugin: 'com.android.library'"**

2) remove attribute "applicationId "com.myapplication"" from defaultConfig.

Now go to the menu bar and click on “Build” > “Clean” and then “Build” – > “Rebuild”.
It will create a “.aar” file in “app\build\outputs\aar”.

This is your library.
Import it into your application, such as NotMyApplication in the libs folder, and follow these steps:
1) Add the following code to the build.gradle of the app:
Repository {
flatDir {
Catalog “Library”

2) Also add following to build.gradle of app : 
    **dependencies {
    ...
    compile (name: 'name_of_aar_file_without_ext', ext:'aar' )
    }**

3) Declare the Activity you want to launch in your apps manifest file : 

**<activity android:name="com.testmylib.MyActivity" >
    </activity>**

4) Launch your activity as :
        **Intent in = new Intent(NotMyActivity.this,com.testmylib.MyActivity.class)
        startActivity(in); **

It will start your activity from lib. One thing to note is that if a resource with the same name exists, the activity will be selected from NotMyApplication. So remember to give unique names to the resources for such activities that you want to export in the library.

I still don’t know why it doesn’t work from Jar. Any help would be appreciated…:)

For more details, please visit the link: http://revisitingandroid.blogspot.in/2017/01/how-to-launch-activity-from-aar-files.html

Related Problems and Solutions