美文网首页
Smali语法解析

Smali语法解析

作者: Erich_Godsen | 来源:发表于2021-03-19 09:55 被阅读0次

    Smali语言是什么?

    SmaliDalvik的寄存器语言,Smali代码是安卓Apk反编译而来的,大家可以通过ApkTool这个工具来反编译App得到对应的Smali语言,或者在 Android Studio中安装java2smail这个插件,安装成功后选择Build->Compile to Smali来编译生成对应的Smali文件

    代码示例

    源文件如下

    public class SurfaceRenderView extends SurfaceView implements IRenderView {
        private MeasureHelper mMeasureHelper;
    
        public SurfaceRenderView(Context context) {
            super(context);
            initView(context);
        }
    
        public SurfaceRenderView(Context context, AttributeSet attrs) {
            super(context, attrs);
            initView(context);
        }
    
        public SurfaceRenderView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            initView(context);
        }
    
        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        public SurfaceRenderView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            super(context, attrs, defStyleAttr, defStyleRes);
            initView(context);
        }
    
        private void initView(Context context) {
            mMeasureHelper = new MeasureHelper(this);
            mSurfaceCallback = new SurfaceCallback(this);
            getHolder().addCallback(mSurfaceCallback);
            //noinspection deprecation
            getHolder().setType(SurfaceHolder.SURFACE_TYPE_NORMAL);
        }
    
        @Override
        public View getView() {
            return this;
        }
    
        @Override
        public boolean shouldWaitForResize() {
            return true;
        }
    
        //--------------------
        // Layout & Measure
        //--------------------
        @Override
        public void setVideoSize(int videoWidth, int videoHeight) {
            if (videoWidth > 0 && videoHeight > 0) {
                mMeasureHelper.setVideoSize(videoWidth, videoHeight);
                getHolder().setFixedSize(videoWidth, videoHeight);
                requestLayout();
            }
        }
    
        @Override
        public void setVideoSampleAspectRatio(int videoSarNum, int videoSarDen) {
            if (videoSarNum > 0 && videoSarDen > 0) {
                mMeasureHelper.setVideoSampleAspectRatio(videoSarNum, videoSarDen);
                requestLayout();
            }
        }
    
        @Override
        public void setVideoRotation(int degree) {
            Log.e("", "SurfaceView doesn't support rotation (" + degree + ")!\n");
        }
    
        @Override
        public void setAspectRatio(int aspectRatio) {
            mMeasureHelper.setAspectRatio(aspectRatio);
            requestLayout();
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            mMeasureHelper.doMeasure(widthMeasureSpec, heightMeasureSpec);
            setMeasuredDimension(mMeasureHelper.getMeasuredWidth(), mMeasureHelper.getMeasuredHeight());
        }
    
        //--------------------
        // SurfaceViewHolder
        //--------------------
    
        private static final class InternalSurfaceHolder implements IRenderView.ISurfaceHolder {
            private SurfaceRenderView mSurfaceView;
            private SurfaceHolder mSurfaceHolder;
    
            public InternalSurfaceHolder(@NonNull SurfaceRenderView surfaceView,
                                         @Nullable SurfaceHolder surfaceHolder) {
                mSurfaceView = surfaceView;
                mSurfaceHolder = surfaceHolder;
            }
    
            public void bindToMediaPlayer(IMediaPlayer mp) {
                if (mp != null) {
                    if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) &&
                            (mp instanceof ISurfaceTextureHolder)) {
                        ISurfaceTextureHolder textureHolder = (ISurfaceTextureHolder) mp;
                        textureHolder.setSurfaceTexture(null);
                    }
                    mp.setDisplay(mSurfaceHolder);
                }
            }
    
            @NonNull
            @Override
            public IRenderView getRenderView() {
                return mSurfaceView;
            }
    
            @Nullable
            @Override
            public SurfaceHolder getSurfaceHolder() {
                return mSurfaceHolder;
            }
    
            @Nullable
            @Override
            public SurfaceTexture getSurfaceTexture() {
                return null;
            }
    
            @Nullable
            @Override
            public Surface openSurface() {
                if (mSurfaceHolder == null)
                    return null;
                return mSurfaceHolder.getSurface();
            }
        }
    
        //-------------------------
        // SurfaceHolder.Callback
        //-------------------------
    
        @Override
        public void addRenderCallback(IRenderCallback callback) {
            mSurfaceCallback.addRenderCallback(callback);
        }
    
        @Override
        public void removeRenderCallback(IRenderCallback callback) {
            mSurfaceCallback.removeRenderCallback(callback);
        }
    
        private SurfaceCallback mSurfaceCallback;
    
        private static final class SurfaceCallback implements SurfaceHolder.Callback {
            private SurfaceHolder mSurfaceHolder;
            private boolean mIsFormatChanged;
            private int mFormat;
            private int mWidth;
            private int mHeight;
    
            private WeakReference<SurfaceRenderView> mWeakSurfaceView;
            private Map<IRenderCallback, Object> mRenderCallbackMap = new ConcurrentHashMap<IRenderCallback, Object>();
    
            public SurfaceCallback(@NonNull SurfaceRenderView surfaceView) {
                mWeakSurfaceView = new WeakReference<SurfaceRenderView>(surfaceView);
            }
    
            public void addRenderCallback(@NonNull IRenderCallback callback) {
                mRenderCallbackMap.put(callback, callback);
    
                ISurfaceHolder surfaceHolder = null;
                if (mSurfaceHolder != null) {
                    if (surfaceHolder == null)
                        surfaceHolder = new InternalSurfaceHolder(mWeakSurfaceView.get(), mSurfaceHolder);
                    callback.onSurfaceCreated(surfaceHolder, mWidth, mHeight);
                }
    
                if (mIsFormatChanged) {
                    if (surfaceHolder == null)
                        surfaceHolder = new InternalSurfaceHolder(mWeakSurfaceView.get(), mSurfaceHolder);
                    callback.onSurfaceChanged(surfaceHolder, mFormat, mWidth, mHeight);
                }
            }
    
            public void removeRenderCallback(@NonNull IRenderCallback callback) {
                mRenderCallbackMap.remove(callback);
            }
    
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                mSurfaceHolder = holder;
                mIsFormatChanged = false;
                mFormat = 0;
                mWidth = 0;
                mHeight = 0;
    
                ISurfaceHolder surfaceHolder = new InternalSurfaceHolder(mWeakSurfaceView.get(), mSurfaceHolder);
                for (IRenderCallback renderCallback : mRenderCallbackMap.keySet()) {
                    renderCallback.onSurfaceCreated(surfaceHolder, 0, 0);
                }
            }
    
            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                mSurfaceHolder = null;
                mIsFormatChanged = false;
                mFormat = 0;
                mWidth = 0;
                mHeight = 0;
    
                ISurfaceHolder surfaceHolder = new InternalSurfaceHolder(mWeakSurfaceView.get(), mSurfaceHolder);
                for (IRenderCallback renderCallback : mRenderCallbackMap.keySet()) {
                    renderCallback.onSurfaceDestroyed(surfaceHolder);
                }
            }
    
            @Override
            public void surfaceChanged(SurfaceHolder holder, int format,
                                       int width, int height) {
                mSurfaceHolder = holder;
                mIsFormatChanged = true;
                mFormat = format;
                mWidth = width;
                mHeight = height;
    
                // mMeasureHelper.setVideoSize(width, height);
    
                ISurfaceHolder surfaceHolder = new InternalSurfaceHolder(mWeakSurfaceView.get(), mSurfaceHolder);
                for (IRenderCallback renderCallback : mRenderCallbackMap.keySet()) {
                    renderCallback.onSurfaceChanged(surfaceHolder, format, width, height);
                }
            }
        }
    
        //--------------------
        // Accessibility
        //--------------------
    
        @Override
        public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
            super.onInitializeAccessibilityEvent(event);
            event.setClassName(SurfaceRenderView.class.getName());
        }
    
        @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
        @Override
        public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
            super.onInitializeAccessibilityNodeInfo(info);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                info.setClassName(SurfaceRenderView.class.getName());
            }
        }
    }
    

    编译生成的Smali文件如下

    .class public Lcom/atstudy/player/media/SurfaceRenderView;
    .super Landroid/view/SurfaceView;
    .source "SurfaceRenderView.java"
    
    # interfaces
    .implements Lcom/atstudy/player/media/IRenderView;
    
    
    # annotations
    .annotation system Ldalvik/annotation/MemberClasses;
        value = {
            Lcom/atstudy/player/media/SurfaceRenderView$SurfaceCallback;,
            Lcom/atstudy/player/media/SurfaceRenderView$InternalSurfaceHolder;
        }
    .end annotation
    
    
    # instance fields
    .field private mMeasureHelper:Lcom/atstudy/player/media/MeasureHelper;
    
    .field private mSurfaceCallback:Lcom/atstudy/player/media/SurfaceRenderView$SurfaceCallback;
    
    
    # direct methods
    .method public constructor <init>(Landroid/content/Context;)V
        .registers 2
        .param p1, "context"    # Landroid/content/Context;
    
        .prologue
        .line 47
        invoke-direct {p0, p1}, Landroid/view/SurfaceView;-><init>(Landroid/content/Context;)V
    
        .line 48
        invoke-direct {p0, p1}, Lcom/atstudy/player/media/SurfaceRenderView;->initView(Landroid/content/Context;)V
    
        .line 49
        return-void
    .end method
    
    .method public constructor <init>(Landroid/content/Context;Landroid/util/AttributeSet;)V
        .registers 3
        .param p1, "context"    # Landroid/content/Context;
        .param p2, "attrs"    # Landroid/util/AttributeSet;
    
        .prologue
        .line 52
        invoke-direct {p0, p1, p2}, Landroid/view/SurfaceView;-><init>(Landroid/content/Context;Landroid/util/AttributeSet;)V
    
        .line 53
        invoke-direct {p0, p1}, Lcom/atstudy/player/media/SurfaceRenderView;->initView(Landroid/content/Context;)V
    
        .line 54
        return-void
    .end method
    
    .method public constructor <init>(Landroid/content/Context;Landroid/util/AttributeSet;I)V
        .registers 4
        .param p1, "context"    # Landroid/content/Context;
        .param p2, "attrs"    # Landroid/util/AttributeSet;
        .param p3, "defStyleAttr"    # I
    
        .prologue
        .line 57
        invoke-direct {p0, p1, p2, p3}, Landroid/view/SurfaceView;-><init>(Landroid/content/Context;Landroid/util/AttributeSet;I)V
    
        .line 58
        invoke-direct {p0, p1}, Lcom/atstudy/player/media/SurfaceRenderView;->initView(Landroid/content/Context;)V
    
        .line 59
        return-void
    .end method
    
    .method public constructor <init>(Landroid/content/Context;Landroid/util/AttributeSet;II)V
        .registers 5
        .param p1, "context"    # Landroid/content/Context;
        .param p2, "attrs"    # Landroid/util/AttributeSet;
        .param p3, "defStyleAttr"    # I
        .param p4, "defStyleRes"    # I
        .annotation build Landroid/annotation/TargetApi;
            value = 0x15
        .end annotation
    
        .prologue
        .line 63
        invoke-direct {p0, p1, p2, p3, p4}, Landroid/view/SurfaceView;-><init>(Landroid/content/Context;Landroid/util/AttributeSet;II)V
    
        .line 64
        invoke-direct {p0, p1}, Lcom/atstudy/player/media/SurfaceRenderView;->initView(Landroid/content/Context;)V
    
        .line 65
        return-void
    .end method
    
    .method private initView(Landroid/content/Context;)V
        .registers 4
        .param p1, "context"    # Landroid/content/Context;
    
        .prologue
        .line 68
        new-instance v0, Lcom/atstudy/player/media/MeasureHelper;
    
        invoke-direct {v0, p0}, Lcom/atstudy/player/media/MeasureHelper;-><init>(Landroid/view/View;)V
    
        iput-object v0, p0, Lcom/atstudy/player/media/SurfaceRenderView;->mMeasureHelper:Lcom/atstudy/player/media/MeasureHelper;
    
        .line 69
        new-instance v0, Lcom/atstudy/player/media/SurfaceRenderView$SurfaceCallback;
    
        invoke-direct {v0, p0}, Lcom/atstudy/player/media/SurfaceRenderView$SurfaceCallback;-><init>(Lcom/atstudy/player/media/SurfaceRenderView;)V
    
        iput-object v0, p0, Lcom/atstudy/player/media/SurfaceRenderView;->mSurfaceCallback:Lcom/atstudy/player/media/SurfaceRenderView$SurfaceCallback;
    
        .line 70
        invoke-virtual {p0}, Lcom/atstudy/player/media/SurfaceRenderView;->getHolder()Landroid/view/SurfaceHolder;
    
        move-result-object v0
    
        iget-object v1, p0, Lcom/atstudy/player/media/SurfaceRenderView;->mSurfaceCallback:Lcom/atstudy/player/media/SurfaceRenderView$SurfaceCallback;
    
        invoke-interface {v0, v1}, Landroid/view/SurfaceHolder;->addCallback(Landroid/view/SurfaceHolder$Callback;)V
    
        .line 72
        invoke-virtual {p0}, Lcom/atstudy/player/media/SurfaceRenderView;->getHolder()Landroid/view/SurfaceHolder;
    
        move-result-object v0
    
        const/4 v1, 0x0
    
        invoke-interface {v0, v1}, Landroid/view/SurfaceHolder;->setType(I)V
    
        .line 73
        return-void
    .end method
    
    
    # virtual methods
    .method public addRenderCallback(Lcom/atstudy/player/media/IRenderView$IRenderCallback;)V
        .registers 3
        .param p1, "callback"    # Lcom/atstudy/player/media/IRenderView$IRenderCallback;
    
        .prologue
        .line 180
        iget-object v0, p0, Lcom/atstudy/player/media/SurfaceRenderView;->mSurfaceCallback:Lcom/atstudy/player/media/SurfaceRenderView$SurfaceCallback;
    
        invoke-virtual {v0, p1}, Lcom/atstudy/player/media/SurfaceRenderView$SurfaceCallback;->addRenderCallback(Lcom/atstudy/player/media/IRenderView$IRenderCallback;)V
    
        .line 181
        return-void
    .end method
    
    .method public getView()Landroid/view/View;
        .registers 1
    
        .prologue
        .line 77
        return-object p0
    .end method
    
    .method public onInitializeAccessibilityEvent(Landroid/view/accessibility/AccessibilityEvent;)V
        .registers 3
        .param p1, "event"    # Landroid/view/accessibility/AccessibilityEvent;
    
        .prologue
        .line 277
        invoke-super {p0, p1}, Landroid/view/SurfaceView;->onInitializeAccessibilityEvent(Landroid/view/accessibility/AccessibilityEvent;)V
    
        .line 278
        const-class v0, Lcom/atstudy/player/media/SurfaceRenderView;
    
        invoke-virtual {v0}, Ljava/lang/Class;->getName()Ljava/lang/String;
    
        move-result-object v0
    
        invoke-virtual {p1, v0}, Landroid/view/accessibility/AccessibilityEvent;->setClassName(Ljava/lang/CharSequence;)V
    
        .line 279
        return-void
    .end method
    
    .method public onInitializeAccessibilityNodeInfo(Landroid/view/accessibility/AccessibilityNodeInfo;)V
        .registers 4
        .param p1, "info"    # Landroid/view/accessibility/AccessibilityNodeInfo;
        .annotation build Landroid/annotation/TargetApi;
            value = 0xe
        .end annotation
    
        .prologue
        .line 284
        invoke-super {p0, p1}, Landroid/view/SurfaceView;->onInitializeAccessibilityNodeInfo(Landroid/view/accessibility/AccessibilityNodeInfo;)V
    
        .line 285
        sget v0, Landroid/os/Build$VERSION;->SDK_INT:I
    
        const/16 v1, 0xe
    
        if-lt v0, v1, :cond_12
    
        .line 286
        const-class v0, Lcom/atstudy/player/media/SurfaceRenderView;
    
        invoke-virtual {v0}, Ljava/lang/Class;->getName()Ljava/lang/String;
    
        move-result-object v0
    
        invoke-virtual {p1, v0}, Landroid/view/accessibility/AccessibilityNodeInfo;->setClassName(Ljava/lang/CharSequence;)V
    
        .line 288
        :cond_12
        return-void
    .end method
    
    .method protected onMeasure(II)V
        .registers 5
        .param p1, "widthMeasureSpec"    # I
        .param p2, "heightMeasureSpec"    # I
    
        .prologue
        .line 118
        iget-object v0, p0, Lcom/atstudy/player/media/SurfaceRenderView;->mMeasureHelper:Lcom/atstudy/player/media/MeasureHelper;
    
        invoke-virtual {v0, p1, p2}, Lcom/atstudy/player/media/MeasureHelper;->doMeasure(II)V
    
        .line 119
        iget-object v0, p0, Lcom/atstudy/player/media/SurfaceRenderView;->mMeasureHelper:Lcom/atstudy/player/media/MeasureHelper;
    
        invoke-virtual {v0}, Lcom/atstudy/player/media/MeasureHelper;->getMeasuredWidth()I
    
        move-result v0
    
        iget-object v1, p0, Lcom/atstudy/player/media/SurfaceRenderView;->mMeasureHelper:Lcom/atstudy/player/media/MeasureHelper;
    
        invoke-virtual {v1}, Lcom/atstudy/player/media/MeasureHelper;->getMeasuredHeight()I
    
        move-result v1
    
        invoke-virtual {p0, v0, v1}, Lcom/atstudy/player/media/SurfaceRenderView;->setMeasuredDimension(II)V
    
        .line 120
        return-void
    .end method
    
    .method public removeRenderCallback(Lcom/atstudy/player/media/IRenderView$IRenderCallback;)V
        .registers 3
        .param p1, "callback"    # Lcom/atstudy/player/media/IRenderView$IRenderCallback;
    
        .prologue
        .line 185
        iget-object v0, p0, Lcom/atstudy/player/media/SurfaceRenderView;->mSurfaceCallback:Lcom/atstudy/player/media/SurfaceRenderView$SurfaceCallback;
    
        invoke-virtual {v0, p1}, Lcom/atstudy/player/media/SurfaceRenderView$SurfaceCallback;->removeRenderCallback(Lcom/atstudy/player/media/IRenderView$IRenderCallback;)V
    
        .line 186
        return-void
    .end method
    
    .method public setAspectRatio(I)V
        .registers 3
        .param p1, "aspectRatio"    # I
    
        .prologue
        .line 112
        iget-object v0, p0, Lcom/atstudy/player/media/SurfaceRenderView;->mMeasureHelper:Lcom/atstudy/player/media/MeasureHelper;
    
        invoke-virtual {v0, p1}, Lcom/atstudy/player/media/MeasureHelper;->setAspectRatio(I)V
    
        .line 113
        invoke-virtual {p0}, Lcom/atstudy/player/media/SurfaceRenderView;->requestLayout()V
    
        .line 114
        return-void
    .end method
    
    .method public setVideoRotation(I)V
        .registers 5
        .param p1, "degree"    # I
    
        .prologue
        .line 107
        const-string v0, ""
    
        new-instance v1, Ljava/lang/StringBuilder;
    
        invoke-direct {v1}, Ljava/lang/StringBuilder;-><init>()V
    
        const-string v2, "SurfaceView doesn\'t support rotation ("
    
        invoke-virtual {v1, v2}, Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/lang/StringBuilder;
    
        move-result-object v1
    
        invoke-virtual {v1, p1}, Ljava/lang/StringBuilder;->append(I)Ljava/lang/StringBuilder;
    
        move-result-object v1
    
        const-string v2, ")!\n"
    
        invoke-virtual {v1, v2}, Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/lang/StringBuilder;
    
        move-result-object v1
    
        invoke-virtual {v1}, Ljava/lang/StringBuilder;->toString()Ljava/lang/String;
    
        move-result-object v1
    
        invoke-static {v0, v1}, Landroid/util/Log;->e(Ljava/lang/String;Ljava/lang/String;)I
    
        .line 108
        return-void
    .end method
    
    .method public setVideoSampleAspectRatio(II)V
        .registers 4
        .param p1, "videoSarNum"    # I
        .param p2, "videoSarDen"    # I
    
        .prologue
        .line 99
        if-lez p1, :cond_c
    
        if-lez p2, :cond_c
    
        .line 100
        iget-object v0, p0, Lcom/atstudy/player/media/SurfaceRenderView;->mMeasureHelper:Lcom/atstudy/player/media/MeasureHelper;
    
        invoke-virtual {v0, p1, p2}, Lcom/atstudy/player/media/MeasureHelper;->setVideoSampleAspectRatio(II)V
    
        .line 101
        invoke-virtual {p0}, Lcom/atstudy/player/media/SurfaceRenderView;->requestLayout()V
    
        .line 103
        :cond_c
        return-void
    .end method
    
    .method public setVideoSize(II)V
        .registers 4
        .param p1, "videoWidth"    # I
        .param p2, "videoHeight"    # I
    
        .prologue
        .line 90
        if-lez p1, :cond_13
    
        if-lez p2, :cond_13
    
        .line 91
        iget-object v0, p0, Lcom/atstudy/player/media/SurfaceRenderView;->mMeasureHelper:Lcom/atstudy/player/media/MeasureHelper;
    
        invoke-virtual {v0, p1, p2}, Lcom/atstudy/player/media/MeasureHelper;->setVideoSize(II)V
    
        .line 92
        invoke-virtual {p0}, Lcom/atstudy/player/media/SurfaceRenderView;->getHolder()Landroid/view/SurfaceHolder;
    
        move-result-object v0
    
        invoke-interface {v0, p1, p2}, Landroid/view/SurfaceHolder;->setFixedSize(II)V
    
        .line 93
        invoke-virtual {p0}, Lcom/atstudy/player/media/SurfaceRenderView;->requestLayout()V
    
        .line 95
        :cond_13
        return-void
    .end method
    
    .method public shouldWaitForResize()Z
        .registers 2
    
        .prologue
        .line 82
        const/4 v0, 0x1
    
        return v0
    .end method
    

    具体解析如下

    .class public Lcom/atstudy/player/media/SurfaceRenderView;
    .super Landroid/view/SurfaceView;
    .source "SurfaceRenderView.java"
    

    前三行告诉我们这个类相关的信息
    访问修饰符为public
    全限定名为com/atstudy/player/media/SurfaceRenderView
    继承自android/view/SurfaceView
    源文件名SurfaceRenderView.java
    Note:经过混淆的DEX文件,反编译出来的Smali代码可能没有源文件信息,source可能为空

    # annotations
    .annotation system Ldalvik/annotation/MemberClasses;
        value = {
            Lcom/atstudy/player/media/SurfaceRenderView$SurfaceCallback;,
            Lcom/atstudy/player/media/SurfaceRenderView$InternalSurfaceHolder;
        }
    .end annotation
    

    注解,成员内部类共有两个

    .field private mMeasureHelper:Lcom/atstudy/player/media/MeasureHelper;
    
    .field private mSurfaceCallback:Lcom/atstudy/player/media/SurfaceRenderView$SurfaceCallback;
    

    私有属性mMeasureHelper, mSurfaceCallback说明

    .method public constructor <init>(Landroid/content/Context;)V
        .registers 2
        .param p1, "context"    # Landroid/content/Context;
    
        .prologue
        .line 47
        invoke-direct {p0, p1}, Landroid/view/SurfaceView;-><init>(Landroid/content/Context;)V
    
    
        .line 48
        invoke-direct {p0, p1}, Lcom/atstudy/player/media/SurfaceRenderView;->initView(Landroid/content/Context;)V
    
        .line 49
        return-void
    .end method
    

    构造方法其中

    .registers 指定方法中寄存器总数
    .prologue 表示代码开始
    invoke-direct 表示调用函数`

    Note:``Smali中的函数调用 Smali中的函数和成员变量一样也分为两种类型,但是不同成员变量中的staticinstance之分,而是directvirtual之分。那么direct methodvirtual method有什么区别呢?直白地讲,direct method就是private函数,其余的publicprotected函数都属于virtual method。所以在调用函数时,有invoke-directinvoke-virtual,另外还有invoke-staticinvoke-super以及invoke-interface等几种不同的指令。当然其实还有invoke-XXX/range指令的,这是参数多于4个的时候调用的指令,比较少见,了解下即可

    返回类型如下:

    • V void,只能用于返回值类型
    • Z boolean
    • B byte
    • S short
    • C charI intJ long(64 位)
    • F floatD double(64 位)
    • L Java 类类型
    • [ 数组类型

    分析一个其中方法逻辑

    //onMeasure方法访问域为protected,两个Int类型参数,一个为widthMeasureSpec,
    //另一个为heightMeasureSpec,返回值为Void类型,需要5个寄存器,其中p1存储了widthMeasureSpec, p2存储了heightMeasureSpec
    .method protected onMeasure(II)V
        .registers 5
        .param p1, "widthMeasureSpec"    # I
        .param p2, "heightMeasureSpec"    # I
    
        .prologue //代码开始
        .line 118
        // 获取mMeasureHelper字段并保存到V0寄存器中,p0相当于java中到this关键字
        iget-object v0, p0, Lcom/atstudy/player/media/SurfaceRenderView;->mMeasureHelper:Lcom/atstudy/player/media/MeasureHelper;
        //调用mMeasureHelper的doMeasure方法,其中p1,p2为参数,参数类型为int,返回值为Void
        invoke-virtual {v0, p1, p2}, Lcom/atstudy/player/media/MeasureHelper;->doMeasure(II)V
    
        .line 119
        //获取mMeasureHelper字段并保存到V0寄存器中,p0相当于java中到this关键字
        iget-object v0, p0, Lcom/atstudy/player/media/SurfaceRenderView;->mMeasureHelper:Lcom/atstudy/player/media/MeasureHelper;
        //调用mMeasureHelper的getMeasuredWidth()方法,返回值为Int类型
        invoke-virtual {v0}, Lcom/atstudy/player/media/MeasureHelper;->getMeasuredWidth()I
        //将得到的结果放到V0寄存器中
        move-result v0
        //获取mMeasureHelper字段并保存到V1寄存器中,p0相当于java中到this关键字
        iget-object v1, p0, Lcom/atstudy/player/media/SurfaceRenderView;->mMeasureHelper:Lcom/atstudy/player/media/MeasureHelper;
       //调用mMeasureHelper的getMeasuredHeight方法,返回值为Int类型
        invoke-virtual {v1}, Lcom/atstudy/player/media/MeasureHelper;->getMeasuredHeight()I
        //将得到的结果放到V1寄存器中
        move-result v1
        //调用当前对象的setMeasuredDimension方法,参数为v0和v1寄存器的值,都为Int类型,返回值为Void
        invoke-virtual {p0, v0, v1}, Lcom/atstudy/player/media/SurfaceRenderView;->setMeasuredDimension(II)V
    
        .line 120
        //返回Void类型
        return-void
    .end method//方法结束
    

    相关文章

      网友评论

          本文标题:Smali语法解析

          本文链接:https://www.haomeiwen.com/subject/jjzucltx.html