ClassNotFoundException: org.jacoco.agent.rt.RT - android

I want to use Jacoco to collect code coverage for manually testing my Android apps. What I did was implement a Broadcast Receiver and ask Jacoco to collect coverage data when receiving the intent. Here is my receiver code:
public class CoverageReceiver extends BroadcastReceiver {
public static String TAG = "JacocoInstrumentation:";
private static final String DEFAULT_COVERAGE_FILE_PATH = "/sdcard/coverage.ec";
private static final boolean LOGD = true;
private String mCoverageFilePath;
#Override
public void onReceive(Context context, Intent intent) {
generateCoverageReport();
}
private void generateCoverageReport() {
if (LOGD)
Log.d(TAG, "generateCoverageReport()");
java.io.File coverageFile = new java.io.File(getCoverageFilePath());
OutputStream out = null;
// We may use this if we want to avoid refecltion and we include
// emma.jar
//RT.dumpCoverageData(coverageFile, false, false);
// Use reflection to call emma dump coverage method, to avoid
// always statically compiling against emma jar
try {
Object agent = Class.forName("org.jacoco.agent.rt.RT")
.getMethod("getAgent")
.invoke(null);
out = new FileOutputStream(coverageFile,false);
out.write((byte[]) agent.getClass().getMethod("getExecutionData", boolean.class)
.invoke(agent, false));
} catch (ClassNotFoundException e) {
reportJacocoError("Jacoco.jar not in the class path?", e);
} catch (SecurityException e) {
reportJacocoError(e);
} catch (NoSuchMethodException e) {
reportJacocoError(e);
} catch (IllegalArgumentException e) {
reportJacocoError(e);
} catch (IllegalAccessException e) {
reportJacocoError(e);
} catch (InvocationTargetException e) {
reportJacocoError(e);
} catch (FileNotFoundException e){
reportJacocoError(e);
} catch (IOException e) {
reportJacocoError(e);
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
reportJacocoError(e);
}
}
}
}
public String getCoverageFilePath() {
if (mCoverageFilePath == null) {
return DEFAULT_COVERAGE_FILE_PATH;
} else {
return mCoverageFilePath;
}
}
public boolean setCoverageFilePath(String filePath){
if(filePath != null && filePath.length() > 0) {
mCoverageFilePath = filePath;
return true;
}
return false;
}
private void reportJacocoError(Exception e) {
reportJacocoError("", e);
}
private void reportJacocoError(String hint, Exception e) {
String msg = "Failed to generate Jacoco coverage. " + hint;
Log.e(TAG, msg, e);
}
}
I also add apply the plugin: 'jacoco' and testCoverageEnabled = true in the Gradle file.
I have seen a few people do this, so it should work. However, when I tried to broadcast the triggering intent and my receiver began to work, I got a ClassNotFoundException:
java.lang.ClassNotFoundException: org.jacoco.agent.rt.RT
...
Caused by: java.lang.ClassNotFoundException: Didn't find class "org.jacoco.agent.rt.RT" on path:
...
Suppressed: java.lang.ClassNotFoundException: org.jacoco.agent.rt.RT
...
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available
I have tried to specify the Jacoco version, or add Jacoco to the dependencies, but none of that worked. So what should I do?

Related

How to get Advertising id in android?

I used this code it's working when i called from activity and fragment
import com.google.android.gms.ads.identifier.AdvertisingIdClient.Info;
Info adInfo = null;
try {
adInfo = AdvertisingIdClient.getAdvertisingIdInfo(mContext);
} catch (IOException e) {
e.printStackTrace();
} catch (GooglePlayServicesAvailabilityException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
String AdId = adInfo.getId();
But when i called from pending intent like Package Removed then i want to call the web service at that time i need advertising id but i got null.if you people had done previously please suggest me.thanks in advance.
try use app context
as receivers could use activity context or app context - depends who&how started the receiver
AdvertisingIdClient.getAdvertisingIdInfo(context.getApplicationContext());
You can try this code and call the bellow method on onCreate
public void getAAID()
{
AsyncTask.execute(new Runnable() {
#Override
public void run() {
try {
AdvertisingIdClient.Info adInfo = AdvertisingIdClient.getAdvertisingIdInfo(MyActivity.this);
String myId = adInfo != null ? adInfo.getId() : null;
Log.i("UIDMY",myId);
} catch (Exception e) {
Log.e("error", e);
}
}
});
}
Check complete post: how to get AAID programmatically

Is it possible to test a RoboContentProvider?

I've been struggling around with this test. I want to test my created ContentProvider, using RoboGuice.
Currently the ContentProvider I have extends from RoboContentProvider.
However, I'm not being able to Make It Run.
It says there's no implementation for an Interface. However, the modules are being defined and so one.
Here's the code for the setUp Method on the AndroidTestCase.
#Override
public void setUp() {
Context context = InstrumentationRegistry.getTargetContext();
Application app = null;
try {
app = InstrumentationRegistry
.getInstrumentation()
.newApplication(Application.class.getClassLoader(), Application.class.getName(), context);
RoboGuice.setUseAnnotationDatabases(false);
RoboGuice.overrideApplicationInjector(app,
RoboGuice.newDefaultRoboModule(app),
new DatabaseModule(app)
);
mContentResolver = new MockContentResolver();
mContentProvider = new MobiModelsContentProvider();
mContentProvider.attachInfo(app, null);
mContentResolver.addProvider("com.authority.models.test", mContentProvider);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}

Hold and Unhold sip call using the PJSIP

I developing VOIP android application that make and receive the sip call.I Build the pjsip lilbrary as described in "http://trac.pjsip.org/repos/wiki/Getting-Started/Android".
1. Hold
MainActivity.prm.setOptions(pjsua_call_flag.PJSUA_CALL_UPDATE_CONTACT
.swigValue());
try {
MainActivity.currentCall.setHold(MainActivity.prm);
} catch (Exception e) {
e.printStackTrace();
}
I found this code on pjsip documentation,but this code does not work for put a call on Hold.There is no error message return.
2.Unhold
MainActivity.prm = new CallOpParam(true);
MainActivity.prm.getOpt().setFlag(1);
try {
MainActivity.currentCall.reinvite(MainActivity.prm);
} catch (Exception e) {
e.printStackTrace();
}
Thanks.
Here is my code for hold and unHold:
public void setHold(boolean hold) {
if ((localHold && hold) || (!localHold && !hold)) return;
if(currentCall == null) return;
CallOpParam param = new CallOpParam(true);
try {
if (hold) {
currentCall.setHold(param);
localHold = true;
} else {
CallSetting opt = param.getOpt();
opt.setAudioCount(1);
opt.setVideoCount(0);
opt.setFlag(pjsua_call_flag.PJSUA_CALL_UNHOLD.swigValue());
currentCall.reinvite(param);
localHold = false;
}
} catch (Exception e) {}
}
I hope it's helpful.

Android NFC java.io.IOException: Transceive failed

I am working on an android application which can read and write on an NFC tag.
I have no problem reading a tag which I already wrote something on, but when I use a blank tag I have difficulties reading the UID of the tag in the HEX code.
I am using mifare classic tags and I read the UID directly in the hex with the readblock method. The strange thing is, it works perfectly on debugger mode where I get the UID. But when I am trying without debbuger I get the following exception:
java.io.IOException: Transceive failed
Here's my method to read into the tag :
static String getUID(Intent intent) {
Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
MifareClassic mc = MifareClassic.get(tagFromIntent);
try {
mc.connect();
Log.i("connect", "ok");
} catch (IOException e) {
// TODO Auto-generated catch block
Log.i("connect", "nok");
e.printStackTrace();
}
try {
boolean secA = mc.authenticateSectorWithKeyA(0, mc.KEY_DEFAULT);
Log.i("secA", "ok");
} catch (IOException e) {
Log.i("secA", "nok");
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
boolean secB = mc.authenticateSectorWithKeyB(0, mc.KEY_DEFAULT);
Log.i("secB", "ok");
} catch (IOException e) {
Log.i("secB", "nok");
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] uidBytes = null;
try {
uidBytes = mc.readBlock(0);
Log.i("bytes", "ok");
} catch (IOException e) {
Log.i("bytes", "nok");
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mc.close();
Log.i("close", "ok");
} catch (IOException e) {
Log.i("close", "nok");
// TODO Auto-generated catch block
e.printStackTrace();
}
if (uidBytes != null) {
String uid = HexToString(uidBytes);
return uid;
}
else { return "Repasser le tag";}
}
I have no idea how to fix this, since it works in debug mode.
This code works for me. you have to check the authentication before you can read a block.
MifareClassic mif = MifareClassic.get(detectedTag);
int ttype = mif.getType();
Log.d(TAG, "MifareClassic tag type: " + ttype);
int tsize = mif.getSize();
Log.d(TAG, "tag size: " + tsize);
int s_len = mif.getSectorCount();
Log.d(TAG, "tag sector count: " + s_len);
int b_len = mif.getBlockCount();
Log.d(TAG, "tag block count: " + b_len);
try {
mif.connect();
if (mif.isConnected()){
for(int i=0; i< s_len; i++){
boolean isAuthenticated = false;
if (mif.authenticateSectorWithKeyA(i, MifareClassic.KEY_MIFARE_APPLICATION_DIRECTORY)) {
isAuthenticated = true;
} else if (mif.authenticateSectorWithKeyA(i, MifareClassic.KEY_DEFAULT)) {
isAuthenticated = true;
} else if (mif.authenticateSectorWithKeyA(i,MifareClassic.KEY_NFC_FORUM)) {
isAuthenticated = true;
} else {
Log.d("TAG", "Authorization denied ");
}
if(isAuthenticated) {
int block_index = mif.sectorToBlock(i);
byte[] block = mif.readBlock(block_index);
String s_block = NfcUtils.ByteArrayToHexString(block);
Log.d(TAG, s_block);
}
}
}
mif.close();
} catch (IOException e) {
e.printStackTrace();
}
May be There is Authentication Issue.
You can Authenticate this in this Way....
if (mfc.authenticateSectorWithKeyA(sectorNumber,
MifareClassic.KEY_MIFARE_APPLICATION_DIRECTORY)) {
Log.d("TAG", "Authorized sector with MAD key");
} else if (mfc.authenticateSectorWithKeyA(
sectorNumber, MifareClassic.KEY_DEFAULT)) {
Log.d("TAG",
"Authorization granted to sector with DEFAULT key");
} else if (mfc
.authenticateSectorWithKeyA(sectorNumber,
MifareClassic.KEY_NFC_FORUM)) {
Log.d("TAG",
"Authorization granted to sector with NFC_FORUM key");
} else {
Log.d("TAG", "Authorization denied ");
return false;
}
Here SectorNumber is : The sector you want to authenticate. eg:0,1,2....15 for mifare Classic 1K
When Authentication is done Then you can read or write.

How to retrieve the 'last sync' time for an account?

Is it possible to retrieve the time an account was last synchronized, like the system Settings->Accounts&Sync app does? I'm using Android 2.2.
Looking at the 2.2 source for AccountSyncSettings.java, I see the status is retrieved using:
SyncStatusInfo status = ContentResolver.getSyncStatus(account, authority);
but SyncStatusInfo and getSyncStatus don't seem to be part of the public API (marked with #hide). Is there some other way to get at this info?
You can use reflection to achieve this purpose.Here is my code to implement this
private long getLasySyncTime() {
long result = 0;
try {
Method getSyncStatus = ContentResolver.class.getMethod(
"getSyncStatus", Account.class, String.class);
if (mAccount != null && mSyncAdapter != null) {
Object status = getSyncStatus.invoke(null, mAccount,
mSyncAdapter.authority);
Class<?> statusClass = Class
.forName("android.content.SyncStatusInfo");
boolean isStatusObject = statusClass.isInstance(status);
if (isStatusObject) {
Field successTime = statusClass.getField("lastSuccessTime");
result = successTime.getLong(status);
TLog.d(WeixinSetting.class, "get last sync time %d", result);
}
}
} catch (NoSuchMethodException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
TLog.d(WeixinSetting.class, e.getMessage() + e.getCause().getMessage());
} catch (IllegalArgumentException e) {
} catch (ClassNotFoundException e) {
} catch (NoSuchFieldException e) {
} catch (NullPointerException e) {
}
return result;
}
The Settings app uses ContentResolver.getSyncStatus(account, authority). However, this is not part of the public API. You can use it, but it could break with any future release.

Categories

Resources