我需要一个位于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