Using Android NDK is it possible (from native C-code) to get a list of loaded .so libraries? I am looking for an Android-version of the iOS functions _dyld_image_count()/_dyld_get_image_name().
Also is it possible to get the library name of the .so containing a specific function (on iOS this function is dladdr()).
You can read and parse /proc/self/maps file. It contains which address each shared object loaded for your application. self will help your application to read its own info.
nm command line utiliy which is also available in Android NDK can show you which symbols are available from certain shared object files. You can look how it is implemented and try to utilize the same libraries, but it won't be trivial.
Yes you can Get this Here is Sample
try {
Set<String> libs = new HashSet<String>();
String mapsFile = "/proc/" + android.os.Process.myPid() + "/maps";
BufferedReader reader = new BufferedReader(new
FileReader(mapsFile));
String line;
while ((line = reader.readLine()) != null) {
if (line.endsWith(".so")) {
int n = line.lastIndexOf(" ");
libs.add(line.substring(n + 1));
}
}
Log.d("Ldd", libs.size() + " libraries:");
for (String lib : libs) {
Log.d("Ldd", lib);
}
} catch (FileNotFoundException e) {
// Do some error handling...
} catch (IOException e) {
// Do some error handling...
}
try this This Might Help You
Related
Previously I've asked some questions regarding what method I should use to save some data from my app to retrieve at a later time after it's been closed/stopped. I got the answers I was looking for, I think. But since then, my efforts to implement such a feature has fallen way short. I've researched various questions I've had, for which I thought I found answers. But it seems that the answers, while maybe correct, are not a match necessarily for each other. What I mean, is they might work separately, but coming from various sources, they don't work together as a whole, and for me they don't work at all. I'm led to believe I want to use SharedPreferences. That may or may not be the case, but that has been the direction of my efforts lately.
So I'll ask this multi-part question.
How would you go about saving an array of integers(or boolean values)?
Before loading that saved array, how would I check if it exists?
How would I load the array to use its values again?
Those are the basis of my issues right now. Even at this point, as frustrated as I may be, I don't mind doing more research if someone can point me in the right direction, but everywhere I've looked seems to be missing information and I'm unable to really understand/see how to code what I want to do.
If you REALLY want to see some code, I can show you all my broken pieces at the moment (what I haven't deleted), but I don't see it doing you any good. That said, I'll answer any questions you may need to help me out.
Thanks in advance.
EDIT: The array will change very little from app version to version. It should be about 500-2000 integers or boolean values (either/or will work the same for me). The array is basically a set of flags that tells the app to do one thing or another depending on the value. The size will only change if I add or remove items between versions. For this reason, after checking if the file/array exists, I'll compare the saved array with one in the app and act accordingly.
I've had similar issues with data that has to be preserved through a reboot. I found two ways to do it.
1) Data is seldom accessed.
Store data in .../files in some format that can be easily saved/retrieved. I used JSONArrays to hold the data. mContext.getFilesDir() will get you the path, and you can simply see if your file.exists() to determine if the data exists.
You will need to create an object that will:
1) convert your data to the stored format
for(int i = 0; i < mArray.size(); i++ )
{
JSONObject jo = new JSONObject();
jo.put("THINGY", mArray[i]);
ja.put(jo);
}
2) retrieve your data from the store
String js = readFromFile(fileName);
if( !js.isEmpty() )
ja = new JSONArray( js );
for( int i = 0; i < ja.length(); i++
{
// CONVERT THIS ARRAY TO YOUR INT...
}
3) read/write files like this:
private void writeToFile(String fileName, String data)
{
try
{
FileOutputStream fOut = openFileOutput( fileName, MODE_PRIVATE );
OutputStreamWriter osw = new OutputStreamWriter(fOut);
// Log.d(TAG, "Writing output log...");
osw.write(data);
osw.flush();
osw.close();
}
catch( Exception e )
{
Log.e(TAG, "Cannot create " + fileName );
}
}
private String readFromFile(String fileName)
{
String ret = "";
try
{
InputStream inputStream = openFileInput(fileName);
if ( inputStream != null )
{
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String receiveString = "";
StringBuilder stringBuilder = new StringBuilder();
while ( (receiveString = bufferedReader.readLine()) != null )
{
stringBuilder.append(receiveString);
}
inputStream.close();
ret = stringBuilder.toString();
}
}
catch (FileNotFoundException e)
{
Log.e(TAG, "File not found: " + e.toString());
}
catch (IOException e)
{
Log.e(TAG, "Can not read file: " + e.toString());
}
return ret;
}
JSON works well in this case because there are easy methods to convert to/from strings.
2) Frequent Access
I used SQLiteDatabase. I use a Singlton and DBHelper.getInstance() to get access to it. This seems like overkill, but it is a good solution if the amount of data you are saving is increasing beyond a simple array of Integers.
A really basic (but sound) place to start is: http://www.vogella.com/tutorials/AndroidSQLite/article.html. Note his page was written for 4.3.
HTH.
How can I parse /proc/cpuinfo virtual file of my Android tablet to get information of the processor's core and clockspeed?
I don’t need all information provided by the above file; just these two bits.
Can someone please help?
It is not clear if you want this information inside your app, or just for your own use.
you can get this information on with adb:
adb shell cat /proc/cpuinfo
If you want to use this information in your app, create a simple function to return a Map<String,String>, for example,
public static Map<String, String> getCpuInfoMap() {
Map<String, String> map = new HashMap<String, String>();
try {
Scanner s = new Scanner(new File("/proc/cpuinfo"));
while (s.hasNextLine()) {
String[] vals = s.nextLine().split(": ");
if (vals.length > 1) map.put(vals[0].trim(), vals[1].trim());
}
} catch (Exception e) {Log.e("getCpuInfoMap",Log.getStackTraceString(e));}
return map;
}
Note, this will not get multiple cpus information, overwrites. Most of the values are similar anyways. or Modify to create List of CpuInfoMaps.
try,
Log.d("getCpuInfoMap test", getCpuInfoMap().toString());
I hope its not too late for an answer but, this is how i get the current frequency for a specific cpu core:
public class MainActivity extends Activity{
private static final int INSERTION_POINT = 27;
private static String getCurFrequencyFilePath(int whichCpuCore){
StringBuilder filePath = new StringBuilder("/sys/devices/system/cpu/cpu/cpufreq/scaling_cur_freq");
filePath.insert(INSERTION_POINT, whichCpuCore);
return filePath.toString();
}
public static int getCurrentFrequency(int whichCpuCore){
int curFrequency = -1;
String cpuCoreCurFreqFilePath = getCurFrequencyFilePath(whichCpuCore);
if(new File(cpuCoreCurFreqFilePath).exists()){
try {
BufferedReader br = new BufferedReader(new FileReader(new File(cpuCoreCurFreqFilePath)));
String aLine;
while ((aLine = br.readLine()) != null) {
try{
curFrequency = Integer.parseInt(aLine);
}
catch(NumberFormatException e){
Log.e(getPackageName(), e.toString());
}
}
if (br != null) {
br.close();
}
}
catch (IOException e) {
Log.e(getPackageName(), e.toString());
}
}
return curFrequency;
}
}
From here its a piece of cake, you simply call the method :-D
int core1CurrentFreq = getCurrentFrequency(1, this);
Sometimes the cores go offline, in which case the file path will not exist and -1 will be returned
NOTE. the returned value is in KHz
MHz value is core1CurrentFreq / 1e3
GHz value is core1CurrentFreq / 1e6
Some explainations on the getCurFrequencyFilePath() method since it is not all that clear.
Current frequency is usually stored in the file: scaling_cur_freq
The file path is:
"/sys/devices/system/cpu/cpu(XX)/cpufreq/scaling_cur_freq"
where (XX) is substituted for the cpu core number eg:
"/sys/devices/system/cpu/cpu2/cpufreq/scaling_cur_freq"
The INSERTION_POINT variable is nothing more than the index of (XX), the point at which we want to place the number corresponding to the cpu core
I suggest you take a look at some of the other files in the cpufreq folder, you can use them to get other information like maximum and minimum frequency, list of availables frequencies etc.
Click this
Link
and scroll down to heading 3
I want to store 8 integers into a .csv file(the filename will be taken as an input from a EditText) and retrieve them when I want to.
To get the filename you can use this:
EditText fileNameEdit= (EditText) getActivity().findViewById(R.id.fileName);
String fileName = fileNameEdit.getText().toString();
Then write the file on disk:
try {
String content = "Separe here integers by semi-colon";
File file = new File(fileName +".csv");
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
To read the file:
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader(fileName+".csv"));
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
Then to have the Integers you can use split function:
String[] intArray = sCurrentLine.split(";");
Opencsv doesn't work on Android due to JDK issue.
If you see simular to the following:
05-04 16:13:31.821 25829-25829/? E/AndroidRuntime: FATAL EXCEPTION:
main Process: com.example.pk.opencsvpoc, PID: 25829
java.lang.NoClassDefFoundError: Failed resolution of:
Ljava/beans/Introspector;
It is because Android only ported over a subset of java.beans into its
version of java. You can see a list of what is support at
https://developer.android.com/reference/java/beans/package-summary
There are plans to switch over to reflection in opencsv 5.0 but I
doubt it will remove all our dependencies on java.beans. For now the
best suggestion for opencsv is to steer clear of the com.opencsv.bean
classes until Android fully supports java.beans or we are successful
in removing java.beans.
Another possibility is to try another csv library. I checked apache
commons csv and super-csv and apache does not convert to beans but
super-csv does using only reflection.
Source: https://sourceforge.net/p/opencsv/wiki/FAQ/#getting-noclassdeffounderror-when-using-android
CSV is normal text file, where values are divided by character ";" so, you can write it using BufferedWriter for example.
BufferedWriter
I was wondering if it is possible to find all the libraries that are included in the Android project programmatically. I am writing a library which would need info about other libraries included in the project. Any help much appreciated.
i have made a simple function, just make a call to this function when you want to have the details.
public static void checkLibrary(){
try {
Log.e(" i am in","checklibrary");
Set<String> libs = new HashSet<String>();
String mapsFile = "/proc/" + android.os.Process.myPid() + "/maps";
BufferedReader reader = new BufferedReader(new FileReader(mapsFile));
String line;
while ((line = reader.readLine()) != null) {
if (line.endsWith(".so")) {
int n = line.lastIndexOf(" ");
libs.add(line.substring(n + 1));
}
}
Log.e("Ldd", libs.size() + " libraries:");
for (String lib : libs) {
Log.e("Ldd", lib);
}
} catch (FileNotFoundException e) {
// Do some error handling...
} catch (IOException e) {
// Do some error handling...
}
}
For future readers.
You may use Mike Penz's AboutLibraries library
Benefits
Allow to list external, internal libraries used in your project
Out of box activity or fragment with list of libraries used in your project.
i am trying to use pdfbox lib into my android app but im getting
java.lang.NoClassDefFoundError: org.pdfbox.pdmodel.PDDocument
this error .as i'm developing commercial app i can not use other Lib like itext .So my question is can we use PDfBox in android.
here is my code:-
PDFParser parser = null;
String parsedText = null;
PDFTextStripper pdfStripper;
PDDocument pdDoc = null;
COSDocument cosDoc = null;
PDDocumentInformation pdDocInfo;
try {
f =new File(Environment.getExternalStorageDirectory()+File.separator+"Download"+File.separator+"Services.pdf");
if(f.exists()){
System.out.println("---------exists-----------");
}else{
System.out.println("------NOT----exists----------");
}
parser = new PDFParser(new FileInputStream(f));
} catch (Exception e) {
System.out.println("Unable to open PDF Parser.");
System.out.println("-----------------------error|"+e.toString());
}
try {
parser.parse();
cosDoc = parser.getDocument();
pdfStripper = new PDFTextStripper();
pdDoc = new PDDocument(cosDoc);//here i'm getting exception
//pdDoc = PDDocument.load(f, false);
parsedText = pdfStripper.getText(pdDoc);
} catch (Exception e) {
System.out.println("-----------------------error|"+e.toString());
System.out.println("An exception occured in parsing the PDF Document.");
e.printStackTrace();
try {
if (cosDoc != null) cosDoc.close();
if (pdDoc != null) pdDoc.close();
} catch (Exception e1) {
e.printStackTrace();
}
}
System.out.println("Done.");
System.out.println("-----------------------parsedText|"+parsedText);
using PDFBox 0.7.3 jar
It seems that PDFBox is depending on awt and swing classes that are not available on Android devices.
Therefor you can not use PDFBox on Android.
NoClassDefFoundError is thrown when the JVM can't load a class.
As the javadoc says
Have you included the pdfbox library on classpath during compilation?
If you only need to extract text from PDF document in Android , then use this https://github.com/RatheeshRavindran/PDFBoxLight
I recently did the porting of PDFBox to Android but please note that this still in Beta.