7

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]]

Community
  • 1
  • 1
Tarun Deep Attri
  • 8,174
  • 8
  • 41
  • 56
  • For NotMyApplication, you will have to add the activities that are compiled inside the jar/lib. Like the code snippet you showed, the app should use an Intent to start MyApplication activities and let MyApplication handle the rest. Check this out too: http://stackoverflow.com/questions/16763090/how-to-export-library-to-jar-in-android-studio – KennethLJJ Oct 21 '15 at 09:23
  • 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? – Tarun Deep Attri Oct 21 '15 at 09:53
  • regarding the layout error, try and see if the res files are copied in the jar file, most likely there are not. For the class not found, can you try to get the intent without "getApplicationContext()"? Does it work that way? – KennethLJJ Oct 22 '15 at 01:47

2 Answers2

4

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 details visit the link : http://revisitingandroid.blogspot.in/2017/01/how-to-launch-activity-from-aar-files.html

Tarun Deep Attri
  • 8,174
  • 8
  • 41
  • 56
0

I would like to add a small point with @Tarun's answer:

  1. Copy and Paste all the dependencies from the build.gradle(Module: app) file of the SDK project(say MyApplication) to your Base Project(say NotMyApplication)'s build.gradle(Module: app) file;

  2. Add this:

    flatDir {
         dirs 'libs'
    }
    

    inside your base Project's build.gradle(Project:NotMyApplication) file, like:

    allprojects {
         repositories {
             google()
             jcenter()
             flatDir {
                 dirs 'libs'
             }
         }
     } 
    
  3. Inside your base Project's build.gradle(Module:app) file, use 'implementation' instead of 'compile', like:

    implementation(name: 'name_of_aar_file_without_ext', ext:'aar' )
    

    Since, configuration "compile" is obsolete and has been replaced with "implementation". It will be removed soon. for more information, check here: http://d.android.com/r/tools/update-dependency-configurations.html

Partha Paul
  • 189
  • 4