How to keep jar file external, but still use its classes in my Android project?

I need to have a jar file located in the main / assets directory in an Android project. It is important that the jar file is there.

In my main Android project, is there a way to reference this jar file in my code and use its classes?

To be clear, I do not want to add a jar to the main project after compilation.

EDIT . I tried the link below and it seems to be loading the class file I was talking about. But I'm trying to define constructor arguments for a dynamically loaded class.

android-custom-class-loading-sample

EDIT2

Almost there. I confirmed that the class is loaded from my .jar class. I am stuck creating it though.

In the line licenseValidatorClazz.getConstructor, I get the error below. I assume that I have something missing in the interface file?

java.lang.NoSuchMethodException: [interface com.google.android.vending.licensing.Policy, interface com.google.android.vending.licensing.DeviceLimiter, interface com.google.android.vending.licensing.LicenseCheckerCallback, int, java class .lang.String, class java.lang.String]

public Class licenseValidatorClazz = null; public LicenseValidator validator; ... // Initialize the class loader with the secondary dex file. DexClassLoader cl = new DexClassLoader(dexInternalStoragePath.getAbsolutePath(), optimizedDexOutputPath.getAbsolutePath(), null, mContext.getClassLoader()); try { // Load the library class from the class loader. licenseValidatorClazz = cl.loadClass("com.google.android.vending.licensing.LicenseValidator"); validator = (LicenseValidator) licenseValidatorClazz.getConstructor(Policy.class,DeviceLimiter.class,LicenseCheckerCallback.class,int.class,String.class,String.class).newInstance(ddd, new NullDeviceLimiter(), callback, generateNonce(), mPackageName, mVersionCode); } catch (Exception exception) { // Handle exception gracefully here. exception.printStackTrace(); } 

I have an interface that contains functions for navigating to a loaded class.

 public interface LicenseValidator { public LicenseCheckerCallback getCallback(); public int getNonce(); public String getPackageName(); public void verify(PublicKey publicKey, int responseCode, String signedData, String signature); public void handleResponse(int response, ResponseData rawData); public void handleApplicationError(int code); public void handleInvalidResponse(); } 
+5
source share
3 answers

To use an external bank to communicate with your application and use it at runtime, it must be in dalvik format, as ordinary banks cannot work under dalvikVM.

  • Convert files with the dx tool
  • using aapt cmd, add these classes.dex to the jar file.
  • Now this jar, which contains files in the dalvik format, can be uploaded to our project.

Here is a message that explains how to execute it.

+4
source

There are steps to achieve this.

  • You must make a copy of your JAR file in the private internal storage of your application.

    • Using the dx tool inside the android folder, you should generate the classes.dex file associated with the JAR file. The dx tool will be located at / android -sdks / build-tools / 19.0.1 (this file is needed by the Dalvik virtual machine, just jar cannot be read by dalvik VM))

    • Using the aapt tool command , which is also inside the same location, you must add the classes.dex file to the JAR file.

This JAR file can be loaded dynamically using DexClassLoader.

If you are creating a JAR from any of your own libraries, you need to do the following steps (1-4) every time you change the source code of your library. This way you can automate these steps by creating a shell script (on Mac / Linux / Ubuntu) or batch scripts (on Windows). You can link to this link to understand how to write shell scripts.

Note. . One of the situations for implementing this method is the impossibility of adding JAR files directly to the build path of the main project and it must be dynamically loaded at run time. In normal cases, JAR files can be added to the build path.

please check this link for detailed code and implementation.

How to load jar file at runtime

Android: how to dynamically load classes from a JAR file ?

Hope this helps!

+1
source

You should try the API services - java.util.ServiceLoader

You define the interface of the service and its implementation in your bank.

  package com.my.project; public interface MyService { ... } public class MyServiceBarImpl implements MyService { ... } public class MyServiceFooImpl implements MyService { ... } 

Then you define the services contained in the jar file in the META-INF / services / directory. For example, in the file "META-INF / services / com.my.project.MyService" you list the provider classes.

 # Known MyService providers. com.my.project.MyServiceBarImpl # The original implementation for handling "bar"s. com.my.project.MyServiceFooImpl # A later implementation for "foo"s. 

Then, in the main code base, you can create an instance of MyService using ServiceLoader:

 for (MyService service : ServiceLoader.load(MyService.class)) { //Perform some test to determine which is the right MyServiceImpl //and then do something with the MyService instance } 

These examples are taken more or less directly from the API, although I changed the package names to make them a little less annoying to read.

0
source

Source: https://habr.com/ru/post/1205182/


All Articles