Load external classes in Android - android

I'm trying to load dynamically external classes using DexClassLoader, like #Shlublu proposes here
When I execute my application and the DexClassLoader object try to find the class, it throws a ClassNotFound exception. I have added read and write permissions in the manifest.xml, so it is not the mistake.
I think the problem is the method that I use to make the .jar that I want to load on my application. So I have some questions...
What is the correct method to convert a .java file to .jar using dx
tool?
It is necessary that the package where the external class is loaded be the same that the package of my .jar file? (I think no)
I'm using an Android emulator API 19 (kit-kat)

Since APK is the standard Android package, my suggestion is that you use an APK instead of a JAR. Build an application APK linking the needed JAR (let the Android build tools "dex" the JAR) but without any activity, then install the APK as if it was a normal app. You can then access the APK file itself using the PackageManager to get its path and load it using DexClassLoader.
public static ClassLoader loadAPK(final Context context, final String appName) throws PackageManager.NameNotFoundException {
final String apkPath = context.getPackageManager().getApplicationInfo(appName, 0).sourceDir;
final File tmpDir = context.getDir("tmp", 0);
return new DexClassLoader(apkPath, tmpDir.getAbsolutePath(), null, context.getClassLoader());
}

Related

How to use .so file of another android project to another project

I have decompiled an android signed apk and it has a .so file...
when i am copy paste that .so file in my project's jniLibs/(abi)/*.so
The library load successfully, but it gives following error,
java.lang.UnsatisfiedLinkError: Native method not found: com.***.***.****.****.Decode:([BI[B)I
Here decode is native mathod.In singed apk also the are using same signature for native method...and i am also using the same method signature, then also i am getting the error like this...
please help me
thanks in advance
Let your method com.***.***.****.****.Decode be the com.aaa.bbb.ccc.Ddd.Decode. Then you should create package com.aaa.bbb.ccc, create Ddd Java class in this package, define native method
public native int Decode(byte[] in, int length, byte[] out);
in this class and add a static *.so library by means of:
static {
System.loadLibrary("Ddd");
}
to this class. Make sure if your *.so file has name libDdd.so than you have to write just a Ddd in System.loadLibrary.

Is it necessary to follow same package name as it was for so file while using .so file in new Android project

I had created one project using native c++ support. In this project I am passing int value from activity to c++ code and native code returns whether this is prime number or not. This works perfectly, Now I want to create .so file to use in another project. I had google many post but not got answer how to get different .so file for all devices. So I had rename .apk file to .zip and extract it. After that I got one .so file.
Now I want to use this .so file in another project. therefore I had created new project with different name but package name is same. I had created one directory inside src/main and named it as jniLib in this lib I had copied my .so file directory. In my MainActivity I load so file as static {
System.loadLibrary("native-lib");
}
and call my native method private native String isPrimeNumber(int number);. Here everything is perfect. Now I can get result without having actual c++ code.
Now Again I created new project and follow above steps which was followed by creating second project, but difference is that now I had changed package name of my application. when I run application my application got crashed with error as
FATAL EXCEPTION: main
Process: com.app.androidkt.differentpackage, PID: 16970
java.lang.UnsatisfiedLinkError: No implementation found for java.lang.String com.app.androidkt.differentpackage.MainActivity.isPrimeNumber(int) (tried Java_com_app_androidkt_differentpackage_MainActivity_isPrimeNumber and Java_com_app_androidkt_differentpackage_MainActivity_isPrimeNumber__I)
at com.app.androidkt.differentpackage.MainActivity.isPrimeNumber(Native Method)
at com.app.androidkt.differentpackage.MainActivity.access$000(MainActivity.java:10)
at com.app.androidkt.differentpackage.MainActivity$1.onClick(MainActivity.java:38)
at android.view.View.performClick(View.java:5268)
at android.view.View$PerformClick.run(View.java:21550)
at android.os.Handler.handleCallback(Handler.java:822)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5811)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:681)
So my question are - 1) Is it necessary to use same package name to use .so file in our application as that of .so file.
2) How I can get different .so file directory - currently for time being I had extracted it from apk.
3) To use of .so file is to hide the native code only or there is any other purpose also there?
Thanks in advance.
Your application package name may be anything, but the Java class that consumes native methods implemented in libnative-lib.so must be exactly the same as intended by the authors of this libnative-lib.so file.
The easiest workaround for your setup is to move your com.app.androidkt.differentpackage.MainActivity class to the com.app.androidkt.samplendk package. Android Studio will help you with this refactoring. Note that now you must declare the full path for MainActivity in your AndroidManifest.xml.
Alternatively, you can create a small com.app.androidkt.samplendk.MainActivity
class:
package com.app.androidkt.oldpackage;
public class MainActivity {
static {
System.loadLibrary("native-lib");
}
public native String isPrimeNumber(int number);
}
and add few lines to your MainActivity.java:
package com.app.androidkt.differentpackage;
public class MainActivity extends AppCompatActivity {
private com.app.androidkt.oldpackage.MainActivity pmSolver;
private String isPrimeNumber(int number) {
return pmSolver.isPrimeNumber(number);
}
…
}
If you don't know the exact package name used for this libnative-lib.so, you can find it by parsing its ELF headers: you will see an exported function named similar to Java_com_app_androidkt_ samplendk_MainActivity_isPrimeNumber.
Nitpicker's corner: it is possible to build a JNI library that will hide its designated class name(s), but it is hard to reliably prevent reverse engineering these names; it is also possible to build a JNI library that will seamlessly connect to different packages.
1) Is it necessary to use same package name to use .so file in our
application as that of .so file
No, you can use any package name you desire
2) How I can get different .so file directory - currently for time
being I had extracted it from apk
Copy all .so files to your new project folder: src/main/jniLibs/armeabi
3) To use of .so file is to hide the native code only or there is any
other purpose also there?
.so file is a library. Hence the purpose is to be convenient in reusing implemented features in multiple projects.

What are differences between DexClassLoader and PathClassLoader?

I put a .jar file containing .dex file to directory "/sdcard", then I try to load the class in the .jar file using DexClassLoader and PathClassLoader respectively. Both of them can load the class successfully. What are differences between them?
Here is my code:
String dexPath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "test.jar";
PathClassLoader classLoader1 = new PathClassLoader(dexPath, getClassLoader());
DexClassLoader classLoader2 = new DexClassLoader(dexPath, getDir("dex", 0).getAbsolutePath(), null, getClassLoader());
try {
Class clazz1 = classLoader1.loadClass("com.focans.loader.Peter");
Class clazz2 = classLoader2.loadClass("com.focans.loader.Peter");
} catch (Exception e) {
e.printStackTrace();
}
You should read official Guideline about
DexClassLoader
A class loader that loads classes from .jar and .apk files containing
a classes.dex entry. This can be used to execute code not installed as
part of an application.
PathClassLoader
Provides a simple ClassLoader implementation that operates on a list
of files and directories in the local file system, but does not
attempt to load classes from the network. Android uses this class for
its system class loader and for its application class loader(s).
DexClassLoader is instantiated to load the library from the extracted secondary dex file.
PathClassLoader Used to load classes within ant with a different classpath from that used to start ant. Note that it is possible to force a class into this loader even when that class is on the system classpath by using the forceLoadClass method. Any subsequent classes loaded by that class will then use this loader rather than the system class loader.
For Android 8.1 (API 27) and up, DexClassLoader and PathClassLoader are essentially identical. They both extend BaseDexClassLoader, and immediately call super() when constructed. There are no implementation differences or side effects (at least in the AOSP versions I've referenced here).
In prior versions (8.0 and earlier) DexClassLoader accepted an argument for String optimizedDirectory, which allowed the caller to specify the directory to store optimized Dex code (ODEX files), for the Dex that was loaded by the class loader. This argument still exists in newer versions of Android, but it has no effect.

How can I load a json file from an android unit test that is run with PowerMockRunner?

I am using PowerMockRunner to run my unit tests. I want to load some canned network response json files from my assets folder.
I am using this method to try to get the file.
private static File getFileFromPath(Object obj, String fileName) {
ClassLoader classLoader = obj.getClass().getClassLoader();
URL resource = classLoader.getResource(fileName);
return new File(resource.getPath());
}
I call the method like this from my class which has these annotations at the top.
#RunWith(PowerMockRunner.class)
#PrepareForTest(Network.class)
File file = getFileFromPath(this, "mock_response.json");
However, when I evaluate this expression.
classLoader.getResource(".");
It shows that I am currently in this the directory below while running this test.
/Users/tylerpfaff/Library/Android/sdk/platforms/android-23/data/res/
Seeing as I'm in the platforms resource directory, there is no hope of me successfully loading my resources from my project's resource directory. What do I need to do to access my resource directory of my project?
You have two options:
try to get system classloader via ClassLoader.getSystemClassLoader()
Get classloader from the class that has been ignored by PowerMock. By default:
"org.hamcrest.", "java.",
"javax.accessibility.", "sun.", "org.junit.", "org.testng.",
"junit.", "org.pitest.", "org.powermock.modules.junit4.common.internal.",
"org.powermock.modules.junit3.internal.PowerMockJUnit3RunnerDelegate",
"org.powermock.core*", "org.jacoco.agent.rt.*"
Or you may use #PowerMockIgnore.

Android access to resources outside of activity

I would like to know - are there ways to access android resources and/or assets files (or files from any other folder) outside of an Activity (without passing context)?
Can't access without context:
getResources().getIdentifier("resource_name", "drawable", getPackageName());
getAssets().open("file_in_assets_folder_name");
Throws Exception if not in Activity:
try {
Class class = R.drawable.class;
Field field = class.getField("resource_name");
Integer i = new Integer(field.getInt(null));
} catch (Exception e) {}
Also tried the below, but it complains file doesn't exist (doing something wrong here?):
URL url = new URL("file:///android_asset/file_name.ext");
InputSource source = new InputSource(url.openStream());
//exception looks like so 04-10 00:40:43.382: W/System.err(5547): java.io.FileNotFoundException: /android_asset/InfoItems.xml (No such file or directory)
If the folders are included in the Project Build Path, you can use ClassLoader access files under them outside the android.content.Context, for instance, from a POJO (in case if you don't want to pass a reference of android.content.Context):
String file = "res/raw/test.txt";
InputStream in = this.getClass().getClassLoader().getResourceAsStream(file);
The res folder is included into Project Build Path by default by all Android SDK version so far.
The assets folder was included into Project Build Path by default before Android SDK r14.
To add folders into Project Build Path, right click your project -- Build Path -- Configure Build Path, add your folder (for example, assets if using later SDK version) as a Source folder in build path.
Check out the similar question I answered before at here.
Android framework when compile your app create static class call:
your.namespace.project.R
you can statically call this class like:
your.namespace.project.R.string.my_prop
this.context.findViewById(your.namespace.project.R.id.my_id_prop);
Maybe you can access dynamic resources this way:
Resources res = this.context.getResources();
int id = res.getIdentifier("bar"+Integer.toString(i+1), "id", this.context.getPackageName());

Categories

Resources