Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Launch an Activity from a JAR/Lib

I want to create and android Project let us say MyApplication.

MyApplication has an activity MyActivity.java.

I want some other application let us say NotMyApplication to launch this activity without installing MyApplication on device.

What i want to do for this scenario is i want to Convert MyApplication to a jar/lib somehow so that i dont need to install and also dont need to share code.

And import MyActivity.java in NotMyApplication and Create an Intent like following :

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

How can i do whole process. Can someone guide Stepwise for android studio??

[EDIT]

I created Jar file in eclipse and than copied same to libs folder in android studio. Than added complie statement in gradle : imported jar as :

import com.example.testmylibs.MyClassToTest;

Than launched activity as :

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

Still it gives FATAL as :

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

[EDIT]

if I declare locally in AndroidManifest.xml of NotMyApplicattion as :

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

than following exception comes :

java.lang.NoClassDefFoundError: Failed resolution of: Lcom/example/testmylibs/R$layout; . . Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.testmylibs.R$layout" on path: DexPathList[[zip file "/data/app/com.myapplication-2/base.apk"],nativeLibraryDirectories=[/vendor/lib64, /system/lib64]]

like image 690
Tarun Deep Attri Avatar asked Oct 21 '15 07:10

Tarun Deep Attri


Video Answer


1 Answers

I found out the answer in following way,

I dont know but Jar files are not working this way, so what i did was i made the normal android project (MyApplication) which i wanted to export as a lib. Once i comepleted making changes to my Activity say My Activity. GO to build.gradle of application make 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 menu bar and click on Build->clean and than build-> rebuild. It will create a ".aar" file in "app\build\outputs\aar".

This is your lib. Import it in you application say NotMyApplication in libs folder and perform following steps : 1) Add following code to build.gradle of app : repositories{ flatDir { dirs 'libs' }

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 launch your activity from lib. One point to note is if Resources of same name are there, than Activity will pick from NotMyApplication. So keep in mind ke give unique name to resources of such activities which you want to export in libs.

I still dont know though why from Jar it is not working. Any help on that will be appreciated...:)

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

like image 179
Tarun Deep Attri Avatar answered Sep 28 '22 17:09

Tarun Deep Attri