android如何用外部类,如何保持jar文件外部但仍然在我的Android项目中使用它的类?...

我需要一个位于Android项目的main / assets目录中的jar文件. jar文件位于那里很重要.

我的主要Android项目是否有一种方法可以在我的代码中引用这个jar文件并使用它的类?

为了清楚起见,我不想在编译后将jar添加到主项目中.

编辑:我已经尝试了下面的链接,它似乎加载我说过的类文件.但我正在努力为动态加载的Class定义构造函数参数.

EDIT2

就快到了.我已经确认该类是从我的classes.jar加载的.我虽然坚持实例化.

在licenseValidatorClazz.getConstructor行上,我收到以下错误.我猜我在接口文件中遗漏了什么?

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,class java .lang.String,类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();

}

我有一个接口,其中包含传递给已加载类的函数.

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();

}

解决方法:

要使用外部jar与您的应用程序关联并在运行时使用它,它需要采用dalvik格式,因为普通的jar不能在dalvikVM下工作.

>使用dx工具转换文件

>使用aapt cmd,将这些classes.dex添加到您的jar文件中.

>现在这个包含dalvik格式文件的jar可以加载到我们的项目中.

Here是一个解释完成它的过程的帖子.

标签:java,import,android,external,jar

来源: https://codeday.me/bug/20190519/1138820.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值